【问题标题】:Using grunt for front end with php在 php 中使用 grunt 作为前端
【发布时间】:2015-08-18 04:55:00
【问题描述】:

请原谅,我是 Grunt 的新手,我通常不会编写 PHP 代码。这对我来说是一个新项目。我正在尝试使用 Grunt,因为它很棒,其中一些 html 文件中包含最少的 php。 我最初安装了常规的 grunt,而不是 php grunt。现在我意识到也许我应该安装 grunt-php。但是,我尝试删除 gruntfile.js,安装 grunt-php,然后将新配置添加到新的 gruntfile.js 但终端不断给我一个“默认”未找到错误,即使默认任务肯定存在。我知道我做错了什么,但我不知道是什么。 将 php 添加到我原来的 grunt 文件中是否更容易?我不知道我会怎么做。 这是原始文件:

module.exports = function(grunt){
   require("matchdep").filterDev("grunt-*").forEach(grunt.loadNpmTasks);
   grunt.initConfig({
       htmlhint: {
            build: {
               options: {
                    'tag-pair': true,
                    'tagname-lowercase': true,
                    'attr-lowercase': true,
                    'attr-value-double-quotes': true,
                    'doctype-first': true,
                    'spec-char-escape': true,
                    'id-unique': true,
                    'head-script-disabled': true,
                    'style-disabled': true
                 },
                 src: ['index.php']
             }
         },
         watch: {
             html: {
                 files: ['index.php'],
                 tasks: ['htmlhint']
            },
            js: {
                 files: ['assets/js/**/*.js'],
                 tasks: ['uglify']
             },
             css: {
                 files: ['assets/sass/**/*.scss'],
                 tasks: ['buildcss']
             }
         },
         sass: {
             build: {
                 files: {
                     'build/css/master.css': 'assets/sass/master.scss'
                 }
             }
         },
         browserSync: {
            /*bsFiles: {
                src : ['assets/css/*.css', '*.html'],
            },*/
             files: ['*.html', 'assets/templates/*.html'],
            options: {
                server: {
                    baseDir: "./"
                }
            }
        },
        cssc: {
            build: {
                options: {
                    consolidateViaDeclarations: true,
                    consolidateViaSelectors:    true,
                    consolidateMediaQueries:    true
                },
                files: {
                    'build/css/master.css': 'build/css/master.css'
                }
            }
        },

        cssmin: {
             build: {
                 src: 'build/css/master.css',
                 dest: 'build/css/master.css'
             }
        },

        uglify: {
            build: {
                files: {
                    'build/js/base.min.js':  ['bower_components/jquery/dis/jquery.min.js', 'bower_components/angular/angular.min.js', 'assets/js/**/*.js']
                 }
             }
         },
         pkg: grunt.file.readJSON('package.json'),
         phpunit:{
            test:{
                 dir:'',
                 options:{
                     bin: 'bin/phpunit',
                     configuration:'app/phpunit.xml'
                 }
             }
         },
         'sf2-cache-clear':{
              options: {},
              dev: {},
              prod: {}
          }
      }); 
      grunt.registerTask('buildcss',  ['sass', 'cssc', 'cssmin']);
      grunt.loadNpmTasks('grunt-contrib-sass');
      grunt.loadNpmTasks('grunt-contrib-watch');
      grunt.loadNpmTasks('grunt-browser-sync');
      grunt.loadNpmTasks('grunt-phpunit');
      grunt.loadNpmTasks('grunt-symfony2');
      grunt.registerTask('default', ['uglify', 'buildcss', 'browserSync','watch']);
     grunt.registerTask('test', ['phpunit:test']);

   };

这是我尝试为 grunt-php 添加的 grunt 代码:

require('load-grunt-tasks')(grunt);
    grunt.initConfig({
        php: {
        dist: {
             options: {
                 hostname: '127.0.0.1',
                 port: 9000,
                 base: 'dist', // Project root 
                 keepalive: false,
                 open: false
            }
        }
     },
     browserSync: {
         dist: {
             bsFiles: {
                  src: [
                      // Files you want to watch for changes 
                  ]
             },
            options: {
                proxy: '<%= php.dist.options.hostname %>:<%=php.dist.options.port %>',
                watchTask: true,
                notify: true,
                open: true,
                logLevel: 'silent',
                ghostMode: {
                     clicks: true,
                     scroll: true,
                     links: true,
                     forms: true
                 }
             }
         }
    },
       watch: {
          // Your watch tasks 
       }
   });

   grunt.registerTask('serve', [
     'php:dist',         // Start PHP Server 
     'browserSync:dist', // Using the php instance as a proxy 
     'watch'             // Any other watch tasks you want to run 
   ]);
   grunt.registerTask('default', ['php']);

【问题讨论】:

    标签: php html gruntjs


    【解决方案1】:

    将 grunt-php 添加到现有的 Gruntfile 会更容易。 首先在你的项目中安装它:

    npm install grunt-php --save-dev
    

    然后在你的 Gruntfile 中添加任务配置(例如在 pkg 和 php-unit 之间):

    php: {
        dist: {
            options: {
                base: 'build'
            }
        }
    },
    

    最后定义任务运行服务端,默认构建+服务:

    grunt.registerTask('serve', [
        'php:dist',
        'watch'
    ]);
    
    grunt.registerTask('default', ['uglify', 'buildcss', 'serve', 'watch']);
    

    【讨论】:

    • 好的,我已经安装了 php-unit 并且我已经安装了 grunt-php。但是,当我将 index.html 文件更改为 index.php 文件并运行 grunt 时,它运行时没有错误,但页面只显示“Cannot GET /”。应该这样做吗?
    • 您的 php 文件是否在您的 build 文件夹中?这就是 grunt-php 所服务的。如果不是,则需要将其复制到那里。另外我在我的 php: 上面的任务定义中犯了一个错误,我编辑了我的评论。
    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    相关资源
    最近更新 更多