通过grunt任务更新json文件中的文件引用
Updating file references in a json file via a grunt task
我是一名JavaScript开发人员,对从头开始创建构建过程相当陌生。我选择在我目前的项目中使用Grunt,并创建了一个GruntFile,它可以完成我需要它做的90%的事情,而且它工作得很好,除了这个问题。我有几个JavaScript文件,我引用,而我正在开发一个chrome扩展的manifest.json
文件。对于我的构建过程,我将所有这些文件连接起来,并将其缩小为一个文件,以便包含在manifest.json
中。在构建过程中是否有更新manifest.json
文件中的文件引用,因此它指向缩小的版本?
下面是src manifest文件的一个片段:
{
"content_scripts": [{
"matches": [
"http://*/*"
],
"js": [
"js/lib/zepto.js",
"js/injection.js",
"js/plugins/plugin1.js",
"js/plugins/plugin2.js",
"js/plugins/plugin3.js",
"js/injection-init.js"
]
}],
"version": "2.0",
}
我有一个grunt任务,它将上面列出的所有js文件连接并最小化到一个名为injection.js
的文件中,并且想要一个grunt任务,可以修改清单文件,使其看起来像这样:
{
"content_scripts": [{
"matches": [
"http://*/*"
],
"js": [
"js/injection.js"
]
}],
"version": "2.0",
}
我现在所做的是有2个版本的清单文件,一个用于开发,一个用于构建,在构建过程中,它复制构建版本。这意味着我需要维护两个版本,我宁愿不这样做。有没有什么方法可以让Grunt做得更优雅?
Grunt
提供了自己的api来读取和写入文件,我觉得这比其他依赖如fs
要好:把这个任务放在你的gruntjs文件后,使用grunt命令grunt updatejson:key:value
编辑/更新json文件
grunt.registerTask('updatejson', function (key, value) {
var projectFile = "path/to/json/file";
if (!grunt.file.exists(projectFile)) {
grunt.log.error("file " + projectFile + " not found");
return true;//return false to abort the execution
}
var project = grunt.file.readJSON(projectFile);//get file as json object
project[key]= value;//edit the value of json object, you can also use projec.key if you know what you are updating
grunt.file.write(projectFile, JSON.stringify(project, null, 2));//serialize it back to file
});
我做了类似的事情-您可以加载您的清单,更新内容,然后再次序列化它。比如:
grunt.registerTask('fixmanifest', function() {
var tmpPkg = require('./path/to/manifest/manifest.json');
tmpPkg.foo = "bar";
fs.writeFileSync('./new/path/to/manifest.json', JSON.stringify(tmpPkg,null,2));
});
我不同意其他的答案。
1)为什么用grunt.file.write
代替fs
?grunt.file.write
是只是 fs.writeFilySync
的包装(见这里的代码)。
2)当grunt使非常容易异步做事情时,为什么要使用fs.writeFileSync
?毫无疑问,在构建过程中你不需要异步,但是如果这很容易做到,为什么不呢?(实际上,它只比writeFileSync
实现长几个字符。)
我的建议如下:
var fs = require('fs');
grunt.registerTask('writeManifest', 'Updates the project manifest', function() {
var manifest = require('./path/to/manifest'); // .json not necessary with require
manifest.fileReference = '/new/file/location';
// Calling this.async() returns an async callback and tells grunt that your
// task is asynchronous, and that it should wait till the callback is called
fs.writeFile('./path/to/manifest.json', JSON.stringify(manifest, null, 2), this.async());
// Note that "require" loads files relative to __dirname, while fs
// is relative to process.cwd(). It's easy to get burned by that.
});
- ng-view 中的脚本在从文件引用时不起作用(JQuery 在角度之前加载)
- Javascript没有输入带有.php文件引用的函数
- MRAID文件引用
- 如何仅将js文件引用到特定的脚本块
- 如何在没有元素的情况下获取文件引用对象
- 尽管 js 文件引用在标头中,但未定义函数
- 文件引用从 js 浏览,模拟闪存中的按键解决方法
- 如何从外部 js 文件引用 id
- Javascript-外部库/文件引用
- 如何在asp.net中将javascript文件引用为UserControl
- 从extJavaScript文件引用PHP
- 如何从node_modules到html文件引用lodash 3.10.1
- 使用外部js文件引用td标记
- js文件引用末尾的花括号是什么意思
- 通过grunt任务更新json文件中的文件引用
- 如何在自定义控件中添加.js文件引用
- 从另一个HTML文件引用元素id
- requireJS优化器能用MD5校验和更新静态文件引用和名称吗?
- 在Phonegap中本地文件引用错误
- Ng-grid不能与文件引用的单元格模板一起工作