Gulp-Inject不工作

Mik*_*her 12 build gulp gulp-inject

我有一个简单的Gulp构建过程设置用于测试.我已多次阅读文档,但我似乎无法通过Gulp-inject将我想要的脚本注入index.html文件中.

我的Gulp文件如下所示:

gulp.task('inject1', function() {
   return gulp.src('app/index.html')
        .pipe(inject(gulp.src('./app/scripts/app.js', {read : false}))) // Not necessary to read the files (will speed up things), we're only after their paths
        .pipe(gulp.dest("dist"));
});

gulp.task('inject2', function() {
    return gulp.src('app/scripts/**/*.js', {read : false}) // Not necessary to read the files (will speed up things), we're only after their paths
        .pipe(inject("./app/index.html"))
        .pipe(gulp.dest("./dist"));
});
Run Code Online (Sandbox Code Playgroud)

这是我的Index.html的一部分:

<!-- inject:js -->

<!-- endinject-->
Run Code Online (Sandbox Code Playgroud)

这两个都是从github上的文档中复制的.

当我运行这些任务中的任何一个时,控制台只是说"已启动"注入'完成'注入''

在我的./dist文件夹中,它创建了一个Index.html文件但没有注入js文件.

我已经尝试在src中键入并以许多不同的方式注入属性但没有运气.知道我做错了什么吗?

Gil*_*leg 10

首先,你的endinject标签有错误:

<!-- endinject-->
Run Code Online (Sandbox Code Playgroud)

应该

<!-- endinject -->
Run Code Online (Sandbox Code Playgroud)

这个插件在我和其他各种设置中都很有用,所以问题可能出在你的配置中.因为当您使用流式传输时,您不能确定自己管道的文件,总是尝试使用插件来确切地查看您正在管道的文件.我建议使用gulp-using.试试这个来调试你的设置:

var debug = require('gulp-debug');

gulp.task('inject2', function() {
return gulp.src('app/scripts/**/*.js', {read : false})
    .pipe(debug())
    .pipe(inject("./app/index.html"))
    .pipe(gulp.dest("./dist"));
});
Run Code Online (Sandbox Code Playgroud)

另外,请确保使用相同的方法来验证您是否也匹配您的html文件.除此之外 - 它只是试验和错误,直到您了解管道以获得具有正确路径的正确文件.

如果gulp-inject没有注入任何文件,这意味着您没有正确地管道它们,或者您的目标inject不正确.该插件有效,对我来说非常有用.

如果您需要查看一个工作gulp文件的示例,请查看此gulpfile.js要点

  • gulp-using被列入黑名单作为gulp插件,建议使用gulp-debug:https://github.com/gulpjs/plugins/blob/master/src/blackList.json#L50 (3认同)