Gruntjs 在外部文件中定义生成源列表
我是一个gruntjs新手,正在尝试为JS前端编写一个构建。设置了一个要求,以便必须在外部文件中定义进入生成过程(连接、缩小)的所有源文件:Gruntjs 在外部文件中定义生成源列表,gruntjs,Gruntjs,我是一个gruntjs新手,正在尝试为JS前端编写一个构建。设置了一个要求,以便必须在外部文件中定义进入生成过程(连接、缩小)的所有源文件: |-config |- js.json |-src |- js |- a.js |- b.js |- Gruntfile.js |- package.json 我简化了项目结构以说明问题。config/js.json如下所示: [ "<%=js_dir%>/a.js", "<%=js_dir%&g
|-config
|- js.json
|-src
|- js
|- a.js
|- b.js
|- Gruntfile.js
|- package.json
我简化了项目结构以说明问题。config/js.json
如下所示:
[
"<%=js_dir%>/a.js",
"<%=js_dir%>/b.js"
]
module.exports = function(grunt) {
grunt.initConfig({
concat: {
"options": {"separator": ";"},
"build": {
"src": "<%= grunt.template.process(grunt.file.read('./config/js.json'),{data: {js_dir: './src/js'}})%>"
,
"dest": "build/app.js"
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat']);
};
"src": "<%= grunt.file.write('out',grunt.template.process(grunt.file.read('./config/js.json'),{data: {js_dir: './src/js'}}))%>"
当我运行它时,会生成一个空的输出文件,因为源列表是空的:
...
Reading ./config/js.json...OK
Files: [no src] -> build/app.js
Reading ./config/js.json...OK
Writing out...OK
Writing build/app.js...OK
File "build/app.js" created.
Done, without errors.
为了验证我的逻辑,我通过如下更改src
属性转储了已处理源列表:
[
"<%=js_dir%>/a.js",
"<%=js_dir%>/b.js"
]
module.exports = function(grunt) {
grunt.initConfig({
concat: {
"options": {"separator": ";"},
"build": {
"src": "<%= grunt.template.process(grunt.file.read('./config/js.json'),{data: {js_dir: './src/js'}})%>"
,
"dest": "build/app.js"
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat']);
};
"src": "<%= grunt.file.write('out',grunt.template.process(grunt.file.read('./config/js.json'),{data: {js_dir: './src/js'}}))%>"
由于src
属性,我猜想源列表的绑定是在模板完成之前完成的
gruntjs详细输出显示了连接前后对config/js.json
的读取,这让我很困惑
我试图重写config/js.json
文件,以便所有json数组都放在一行中,但没有用
如果这可以做到,请告诉我怎么做。如果做不到,请告诉我原因
我的环境:
- grunt:grunt cli v0.1.9,grunt v0.4.1
- 节点:v0.11.6-pre
- 操作系统:Linux localhost 3.2.0-23-generic#36 Ubuntu x86_64 GNU/Linux
grunt.template.process
。假设config/js.json
是有效的json,只需执行以下操作:
module.exports = function(grunt) {
grunt.initConfig({
js_dir: 'src/js',
concat: {
options: {separator: ";"},
build: {
src: require('./config/js.json'),
dest: "build/app.js"
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat']);
};
始终记住Gruntfiles是JavaScript而不是JSON。Grunt配置将在读取配置时处理模板。因此,您不需要额外的
grunt.template.process
。假设config/js.json
是有效的json,只需执行以下操作:
module.exports = function(grunt) {
grunt.initConfig({
js_dir: 'src/js',
concat: {
options: {separator: ";"},
build: {
src: require('./config/js.json'),
dest: "build/app.js"
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat']);
};
永远记住Gruntfiles是JavaScript而不是JSON