grunt-closure-tools配置多个目标的问题

时间:2015-02-20 04:17:02

标签: javascript gruntjs google-closure-compiler

我想使用闭包编译器,所以我在我的Grunt配置中添加了grunt-closure-tools,但是我的配置错误了:

Verifying property closureCompiler.loader exists in config...ERROR

以下是grunt-closure-tools的参考资料:

https://www.npmjs.com/package/grunt-closure-tools

https://github.com/thanpolas/grunt-closure-tools

这是我的GruntFile.js:

module.exports = function(grunt) {
        var path = require('path');

    require('load-grunt-config')(grunt, {
        //pkg: grunt.file.readJSON('package.json'),
        configPath: path.join(process.cwd(), 'grunt'), //path to task.js files, defaults to grunt dir
        init: true, //auto grunt.initConfig
        data: { //data passed into config.  Can use with <%= test %>
             pkg: require('./package.json')
        },
        loadGruntTasks: { //can optionally pass options to load-grunt-tasks.  If you set to false, it will disable auto loading tasks.
            pattern: ['grunt-contrib-*', 'grunt-jslint', 'grunt-newer', 'imagemin-*','grunt-closure-tools'],
            scope: 'devDependencies'
        },
        postProcess: function(config) {} //can post process config object before it gets passed to grunt
    }); 
    //require('load-grunt-tasks')(grunt);
    grunt.registerTask("default", ["newer:jslint", "newer:concat", "closureCompiler:loader", "newer:sass"]);
};

我正在使用load-grunt-config将我的配置分成多个部分。这是我的closure.js文件,主要以grunt-closure-tools github页面示例为模型:

module.exports = {

  options: {
    compilerFile: '/usr/local/Cellar/closure-compiler/20141023/libexec/build/compiler.jar',
    checkModified: true,

    compilerOpts: {
       create_source_map: null,
       compilation_level: 'ADVANCED_OPTIMIZATIONS',
    },
    d32: true, // will use 'java -client -d32 -jar compiler.jar' 
  },

    util: {
            src: 'includes/javascript/util/util.js',
            dest: 'includes/javascript/build/util.min.js'

    },  
    loader: {
            src : 'includes/javascript/loaders/loader.js',
            dest: 'includes/javascript/build/loader.min.js'

    }
};

对此错误的任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

经过一些挖掘和不小的牙齿捶打后,我设法让我的配置正常工作。我的Gruntfile.js:

module.exports = function(grunt) {
        var path = require('path');

    require('load-grunt-config')(grunt, {
        //pkg: grunt.file.readJSON('package.json'),
        configPath: path.join(process.cwd(), 'grunt'), //path to task.js files, defaults to grunt dir
        init: true, //auto grunt.initConfig
        data: { //data passed into config.  Can use with <%= test %>
             pkg: require('./package.json')
        },
        loadGruntTasks: { //can optionally pass options to load-grunt-tasks.  If you set to false, it will disable auto loading tasks.
            pattern: ['grunt-contrib-*', 'grunt-jslint', 'grunt-newer', 'imagemin-*','grunt-closure-tools'],
            scope: 'devDependencies'
        },
        postProcess: function(config) {} //can post process config object before it gets passed to grunt
    }); 
    //require('load-grunt-tasks')(grunt);
    grunt.registerTask("default", ["newer:jslint", "newer:concat", "closureCompiler:loader", "newer:sass"]);
};

和我的closureCompiler.js文件(为了清楚起见缩短了):

module.exports = {

  options: {
    compilerFile: '/usr/local/Cellar/closure-compiler/20141023/libexec/build/compiler.jar',

    checkModified: true,

    compilerOpts: {

      // create_source_map: null
    },
    execOpts: {

       maxBuffer: 999999 * 1024
    },

    TieredCompilation: true // will use 'java -server -XX:+TieredCompilation -jar compiler.jar' 
  },

    loader: {
        TEMPcompilerOpts: {
            create_source_map: 'includes/javascript/build/loader.min.js.map'
        },
            src : 'includes/javascript/loaders/loader.js',
            dest: 'includes/javascript/build/loader.min.js'
    }
};

我的配置中出现了很多错误:

  1. 我不得不将closure.js重命名为closureCompiler.js,因此load-grunt-config可以找到该文件。哎呀,我的坏。

  2. Closure Compiler上的d32:true开关意味着使用32位JVM,我在我的系统上没有。我切换到TieredCompilation:而不是。

  3. create_source_map:选项中的null对我不起作用。我在grunt-closure-tools的源代码中找到了引用,但没有花时间来解决错误。相反,我使用我希望Closure制作的地图文件将TEMPcompilerOpts添加到目标配置中。

  4. 我希望通过此解决方案的经验可以获得其他好处。从这一点开始,我将使用Closure Compiler的功能来创建源映射,以将我的dev和prod构建目标合并到一个目标中。