简体   繁体   English

gruntjs:从命令行配置要连接的文件

[英]gruntjs: Configure which files to concat from command line

In my project I have JS-files-modules that should be packed in one file. 在我的项目中,我有JS文件模块,应该打包在一个文件中。

Folder structure: 资料夹结构:

| src
| - start.js
| - module1.js
| - module2.js
| - module3.js
| - end.js

I want to configure which modules to include in file from command line. 我想从命令行配置要包含在文件中的模块。

For example, if I run grunt -module1-module3 , generated file should be made of start.js, module1.js, module3.js, end.js. 例如,如果我运行grunt -module1-module3 ,则生成的文件应由start.js,module1.js,module3.js,end.js组成。

Is there a built in support for this? 有内置的支持吗? Or maybe is there any plugin that can help me with this? 或者,也许有任何插件可以帮助我解决这个问题?

Thank you! 谢谢!

You could pass a comma separated list of files from which you can build a files object. 您可以传递用逗号分隔的文件列表,从中可以构建文件对象。 Here's a quick example: 这是一个简单的示例:

module.exports = function(grunt) {

  function buildFiles() {
    var buildFiles = grunt.option('build-modules').split(',');
    buildFiles.unshift('start');
    buildFiles.push('end');
    for (var i = 0, l = buildFiles.length; i < l; i += 1){
      buildFiles[i] = buildFiles[i] + ".js";
    }
    return buildFiles;
  }

  // Project configuration.
  grunt.initConfig({
   concat: {
      dist: {
        src: buildFiles(), 
        dest: 'dist/out.js'
      }
    },
  });

};

You can call grunt with: grunt concat --build-modules=module1,module2 您可以使用以下命令调用grunt: grunt concat --build-modules=module1,module2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM