Grunt杀手表会议&在grunt文件名中使用变量

时间:2016-02-02 06:46:09

标签: gruntjs grunt-contrib-watch

我正在使用grunt-contrib-watch和这个崇高的包sublime-grunt

  1. 我正在使用sublime-grunt软件包启动观看会话并使用Chrome扩展程序进行实时重新加载。一旦观看了一切,一切都很棒。会话启动什么是停止/终止/取消手表的命令。我尝试在sublime-grunt包中使用一个名为' Grunt Kill Running Processes'并且它告诉我某些内容已被取消但是如果我更改了我的style.scss文件并保存它,它会编译并更新到我的html页面,所以' watch'仍然有效。现在我必须关闭Sublime Text来杀死'观看'会话。

  2. 我正在尝试将变量用于我的主题根目录的路径,但是当我尝试使用字符串连接变量时,我收到了此错误。我应该使用什么类型的语法,我是否需要在package.json文件中创建变量?

  3. 代码

        var theme_path  = 'wp-content/themes/twentyfifteen/';
    
        // Sass plugin
        sass: {
            dist: {
                files: {
                    theme_path + 'style.css': theme_path + 'sass/style.scss',
                }
            }
        },
    
        // Error
        Loading "Gruntfile.js" tasks...ERROR
        >> SyntaxError: C:\wamp\www\grunt\Gruntfile.js:31
        >>                                      theme_path + 'style.css': 'wp-content/th
        emes/twentyfifteen/sass/style.scs
        >>                                                 ^
        >> Unexpected token +
        Warning: Task "default" not found. Use --force to continue.
    

    这是我的Gruntfile.js:

    module.exports = function(grunt) {
    
        // Theme Directory Path
        var theme_path  = 'wp-content/themes/twentyfifteen/';
    
        // Configure main project settings
        grunt.initConfig({
    
            // Basic settings and info about our plugins
            pkg: grunt.file.readJSON('package.json'),
    
            // Watch Plugin
            watch: {
              sass: {
                files: 'wp-content/themes/twentyfifteen/sass/*.scss',
                tasks: ['sass','postcss','uglify'],
                options: {
                  livereload: true,
                },
              },
            },
    
            // Sass plugin
            sass: {
                dist: {
                    files: {
                        'wp-content/themes/twentyfifteen/style.css': 'wp-content/themes/twentyfifteen/sass/style.scss',
                    }
                }
            },
    
            // Postcss plugin
            postcss: {
                options: {
                  map: true, // inline sourcemaps
    
                  // or
                  map: {
                      inline: false, // save all sourcemaps as separate files...
                  },
    
                  processors: [
                    require('pixrem')(), // add fallbacks for rem units
                    require('autoprefixer')({browsers: 'last 2 versions'}), // add vendor prefixes
                    require('cssnano')() // minify the result
                  ]
                },
                dist: {
                  src: 'wp-content/themes/twentyfifteen/style.css'
                }
            },
    
            // Minify JS Uglify
            uglify: {
                dist: {
                    files: {
                        'wp-content/themes/twentyfifteen/js/functions.min.js': ['wp-content/themes/twentyfifteen/js/functions.js']
                    }
                }
            }
    
        });
    
        // Load the plugin
        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.loadNpmTasks('grunt-contrib-sass');
        grunt.loadNpmTasks('grunt-postcss');
        grunt.loadNpmTasks('grunt-contrib-uglify');
    
        // Run plugin
        grunt.registerTask('default', ['watch'] );
    }
    

1 个答案:

答案 0 :(得分:0)

我认为任务选项不能分配变量,目前我没有任何来源,但我可以提出另一种方法:

你可以使用任务参数:

watch: {
    sass: {
        files: '<%= grunt.task.current.args[0] %>/sass/*.scss',
        tasks: ['sass:<%= grunt.task.current.args[0] %>','postcss','uglify'],
        options: {
          livereload: true,
        },
    },
},
sass: {
        dist: {
            files: {
                '<%= grunt.task.current.args[0] %>/style.css': '<%= grunt.task.current.args[0] %>/sass/style.scss',
            }
        }
},

而不是

grunt.registerTask('default', ['watch:'+theme_path] );

这是我已经遇到的用于制作动态监视任务的问题