如何将外部 JSON 数据从上一个任务中生成的文件传递到任务
How to pass external JSON data to the task from file that generating in previous task?
我被困在这里。我有这种类型的任务的咕噜声文件:
grunt.initConfig({
shell: {
// stub task; do not really generate anything, just copy to test
copyJSON: {
command: 'mkdir .tmp && cp stub.json .tmp/javascripts.json'
}
},
uglify: {
build: {
files: {
'output.min.js': grunt.file.readJSON('.tmp/javascripts.json')
}
}
},
clean: {
temp: {
src: '.tmp'
}
}
});
grunt.registerTask('build', [
'shell:copyJSON',
'uglify:build',
'clean:temp'
]);
而且,当然,这是行不通的,因为没有.tmp/javascripts.json
文件:
Error: Unable to read ".tmp/javascripts.json" file (Error code: ENOENT).
我尝试做额外的任务,在生成文件后创建变量,尝试将其存储在globals.javascript
和grunt.option("JSON")
,如下所示:
grunt.registerTask('exportJSON', function() {
if (grunt.file.exists('.tmp/javascripts.json')) {
grunt.log.ok("JSON with set of javascripts exist");
grunt.option("JSON", grunt.file.readJSON('.tmp/javascripts.json'));
}
else {
grunt.fail.warn("JSON with set of javascripts does not exist");
};
});
grunt.initConfig({
uglify: {
build: {
files: {
'output.min.js': grunt.option("JSON")
}
}
}
});
grunt.registerTask('build', [
'shell:copyJSON',
'exportJSON',
'uglify:build',
'clean:temp'
]);
并且总是有相同的错误Warning: Cannot call method 'indexOf' of undefined Use --force to continue.
真的不知道如何弄清楚这一点。有什么想法吗?
如果要填充仅在运行时解析的配置选项,则需要使用模板:
http://gruntjs.com/configuring-tasks#templates
因此,只需将您需要将uglify
任务的files
配置更改为以下内容:
files: {
'output.min.js': "<%= grunt.option('JSON') %>"
}
还可以使用以下grunt.config.set
更改uglify
任务的配置:
grunt.registerTask('exportJSON', function() {
if (grunt.file.exists('.tmp/javascripts.json')) {
grunt.log.ok("JSON with set of javascripts exist");
files = grunt.file.readJSON('.tmp/javascripts.json');
grunt.config.set(
['uglify', 'build', 'files', 'output.min.js'], files
);
} else {
grunt.fail.warn("JSON with set of javascripts does not exist");
}
});
在这种情况下,uglify
任务的files
选项需要如下所示:
files: {
'output.min.js': ''
}
相关文章:
- 吞噬任务多个和共享的 scss 文件
- 如何在Grunt.js中观察多个文件,但只在更改的文件上运行任务
- 如何在Gulp任务中拆分文件
- 从另一个grunt文件加载grunt任务
- 如何在gruntjs中使用watch将多个任务分配给多个文件
- 如何访问任务文件中的 Sails 配置变量
- 使用 gruntjs 自定义初始化任务重命名模板文件夹
- 正在新文件中创建自定义任务
- GulpJS任务在文件更改时自动缩小
- Grunt Copy任务不复制父文件夹's文件保存到正确的文件夹中
- Grunt:如何只对修改后的文件运行任务
- 运行“监视”任务 等待中..警告:EMFILE,打开的文件“./.”太多
- 在使用 GULP 任务将所有 JS 连接到单个 JS 文件中时,在 AngularJS 中出错
- 仅对 Gulp 任务中文件夹中的文件子集使用 Browserify
- 如何使 Gulp 任务将项目的每个 jsx 文件转换为不同的.js文件
- 如何将外部 JSON 数据从上一个任务中生成的文件传递到任务
- 如何在 Gulp 任务中间添加 CSS 文件.js
- 当没有等级库文件或等级库文件为空时,Grunt-contrib-jasmine 中止任务
- 我的 gulp usemin 任务缺少一个 js 文件
- 通过函数填充 Grunt 任务配置的“文件”属性