我正在关注这个hackernoon gulp自动化教程。
不幸的是,我卡在了介绍 gulp-inject 使用的第 6 步。
尽管逐字执行这些步骤,但我在 Windows Powershell 中收到以下错误(我正在 Windows 机器上开发):
gulp-inject Nothing to inject into index.html.
它还会生成一个 HTML 文档,其中包含一些未知的(至少对我而言)编码字符。以下是 index.html 中的代码:
��<�!DOCTYPE html>
<�html>
<�head>
<�!-- src/index.html -->
<�!-- inject:css -->
<�!-- endinject -->
<�/head>
<�body>
<�!-- inject:js -->
<�!-- endinject -->
<�/body>
<�/html>
我已经调查了此事,到目前为止,我遇到了其他几个问题,这些问题也产生了“Nothing to injection”消息,但它们似乎要么是流问题,要么忘记设置文件目标,要么不需要相关的 gulp 包。
我还将在下面提供相关代码,以确保:
gulpfile.js
// gulpfile.js
var gulp = require('gulp');
var inject = require('gulp-inject');
var paths = {
src: 'src/**/*',
srcHTML: 'src/**/*.html',
srcCSS: 'src/**/*.css',
srcJS: 'src/**/*.js',
tmp: 'tmp',
tmpIndex: 'tmp/index.html',
tmpCSS: 'tmp/**/*.css',
tmpJS: 'tmp/**/*.js',
dist: 'dist',
distIndex: 'dist/index.html',
distCSS: 'dist/**/*.css',
distJS: 'dist/**/*.js'
};
gulp.task('default', function () {
console.log('Hello World!');
});
gulp.task('html', function () {
return gulp.src(paths.srcHTML).pipe(gulp.dest(paths.tmp));
});
gulp.task('css', function () {
return gulp.src(paths.srcCSS).pipe(gulp.dest(paths.tmp));
});
gulp.task('js', function () {
return gulp.src(paths.srcJS).pipe(gulp.dest(paths.tmp));
});
gulp.task('copy', ['html', 'css', 'js']);
gulp.task('inject', ['copy'], function () {
var css = gulp.src(paths.tmpCSS);
var js = gulp.src(paths.tmpJS);
return gulp.src(paths.tmpIndex)
.pipe(inject( css, { relative:true } ))
.pipe(inject( js, { relative:true } ))
.pipe(gulp.dest(paths.tmp));
});
package.json 中的相关片段
"devDependencies": {
"gulp": "^3.9.1",
"gulp-debug": "^3.2.0",
"gulp-header": "^2.0.5",
"gulp-inject": "^4.3.2",
"gulp-load-plugins": "^1.5.0"
},
"dependencies": {}
index.html(来自 src 目录)
<!DOCTYPE html>
<html>
<head>
<!-- src/index.html -->
<!-- inject:css -->
<!-- endinject -->
</head>
<body>
<!-- inject:js -->
<!-- endinject -->
</body>
</html>
任何线索将不胜感激,并感谢您花时间阅读此问题。