Gulp:将文件中的内容插入脚本

dhr*_*hrm 3 gulp

我有一个script.js像这样的JavaScript文件:

var myArray = {"INSERT":"ARRAY"};
Run Code Online (Sandbox Code Playgroud)

我还有一个array.json像这样的文件:

{
    "Item1": "Text1",
    "Item2": "Text2"
}
Run Code Online (Sandbox Code Playgroud)

我想用文件中的内容gulp替换数组中的数组.我怎样才能做到这一点?script.jsarray.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"));
});
Run Code Online (Sandbox Code Playgroud)

我成功替换文本的地方,但我不是用静态数组替换,而是需要读取array.json文件并使用它.我不确定如何做到这一点,或者是否有更好的解决方案?我一直在看gulp-inject,但我不确定这是否可以在我的情况下使用?

Fel*_*ing 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"));
});
Run Code Online (Sandbox Code Playgroud)