我正在编写 grunt 来动态连接文件,因为我的 grunt.config 变量中有文件数组。如何在 grunt concat 中使用它。
我正在从动态文本替换函数编写 grunt.config('jsResources', targetConfig);
。它作为数组返回。如何在 grunt concat 中使用它。我尝试过这种方式,但那不值得。
我的jsResources
是数组。我的咕噜声就像
concat: {
js: {
//Concatenate all of the files in the jsResources configuration property
src: ['app/<%= jsResources %>'],
dest: 'build/views/js/combined.js',
options: {
separator: ';\n'
}
}
}
它替换了内容,但无法读取内容,并在我的combine.js中连接
我的 'jsResources' 就像 ['scripts/modules/allModules.js','scripts/config/constants.js','...']
它创建空文件 combine.js
.
最佳答案
所以我又试了一次,结果是这样的:
在将路径放入模板变量之前,您需要生成路径。模板化变量这里是一个对象,但可以是任何有效的js
more info 。在其中您可以设置以数组为值的属性。
module.exports = function(grunt) {
var myFiles = {
jsResources: ['file1.js', 'file2.js']
};
myFiles.jsResources = myFiles.jsResources.map(function(item) { return 'app/' + item; });
// var jsres = myFiles.jsResources; // another way
grunt.initConfig({
// myFiles: myFiles, // this is also possible instead of grunt.config() below
concat: {
dist: {
src: ['<%= myFiles.jsResources %>'], // src: ['<%= jsres %>'],
dest: 'dest.js',
},
options: {
separator: '\n'
}
}
});
grunt.config('myFiles', myFiles);
// grunt.config('jsres', jsres); // another way
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat:dist']);
};
这会生成包含内容的 dest.js
。
关于javascript - Grunt Task Runner 连接文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128966/