我有一个像这样的JavaScript文件script.js
:
var myArray = {"INSERT":"ARRAY"};
此外我还有array.json
这样的文件:
{
"Item1": "Text1",
"Item2": "Text2"
}
我希望gulp
将script.js
中的数组替换为array.json
文件中的内容。我怎么能这样做?
我一直在关注gulp-replace
,并举了这个例子:
gulp.task('my-task', [], function () {
return gulp.src(['script.js'])
.pipe(replace('{"INSERT":"ARRAY"}', '{"MY":"OTHER_ARRAY"}'))
.pipe(gulp.dest("dist.js"));
});
我成功替换了文本,但我不是替换为静态数组,而是需要读取array.json
文件而是使用它。我不确定如何做到这一点,或者是否有更好的解决方案?我一直在关注gulp-inject
,但我不确定这是否可以用于我的情况?
答案 0 :(得分:5)
刚看完文件内容?
gulp.task('my-task', [], function () {
var replacement = fs.readFileSync('path/to/file');
return gulp.src(['script.js'])
.pipe(replace('{"INSERT":"ARRAY"}', replacement))
.pipe(gulp.dest("dist.js"));
});