通过咕噜任务更新一个JSON文件文件引用 [英] Updating file references in a json file via a grunt task
问题描述
我是一名JavaScript开发人员和相当新的从头开始创建构建过程。我选择使用咕噜我目前的项目,并创建了一个GruntFile,做什么,我需要做的约90%,除了这一个问题,它的伟大工程。我有我而我开发的的manifest.json
文件浏览器扩展程序引用了几个JavaScript文件。对于我的构建过程中,我连接所有这些文件,并将其与缩小成一个文件被列入的manifest.json
。反正在生成过程中,以更新的manifest.json
文件的文件引用,使其指向缩小的版本?
I'm a JavaScript developer and fairly new to creating a build process from scratch. I chose to use Grunt for my current project and have created a GruntFile that does about 90% of what I need it to do and it works great, except for this one issue. I have several JavaScript files that I reference while I'm developing a chrome extension in the manifest.json
file. For my build process I am concatenating all of these files and minifying it into one file to be included in manifest.json
. Is there anyway to update the file references in the manifest.json
file during the build process so it points to the minified version?
下面是SRC清单文件的一个片段:
Here is a snippet of the src manifest file:
{
"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",
}
我上面列出到名为 injection.js
一个文件连接在一起,minifies所有js文件咕噜任务,想一个咕噜的任务,可以修改清单文件所以它看起来是这样的:
I have a grunt task that concatenates and minifies all the js files listed above into one file called injection.js
and would like a grunt task that can modify the manifest file so it looks like this:
{
"content_scripts": [{
"matches": [
"http://*/*"
],
"js": [
"js/injection.js"
]
}],
"version": "2.0",
}
我已经完蛋了,现在是有2个版本的清单文件,一个用于开发,一个用于构建,在构建过程中,它会将内部版本代替。这意味着我需要保持2个版本,我宁愿不做。反正是有与步兵更优雅做到这一点?
What I've done for now is have 2 versions of the manifest file, one for dev and one for build, during the build process it copies the build version instead. This means I need to maintain 2 versions which I'd rather not do. Is there anyway to do this more elegantly with Grunt?
推荐答案
步兵
给出了自己的API,用于读取和写入文件,我觉得比其他依赖更好的像 FS
:
使用咕噜用命令编辑/更新JSON文件咕噜updatejson:键:值
把这个任务在你gruntjs文件后,
Grunt
gives its own api for reading and writing files, i feel that better than other dependencies like fs
:
Edit/update json file using grunt with command grunt updatejson:key:value
after putting this task in your gruntjs file
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
});
这篇关于通过咕噜任务更新一个JSON文件文件引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!