如何使用 gulp-inject 在我的 index.html 中插入文件内容?
How can I insert the contents of a file in my index.html with gulp-inject?
这是我目前尝试过的方法:
gulp.task('watch-index-html', function () {
gulp.watch(files, function (file) {
return gulp.src('index/index.html')
.pipe(inject(gulp.src(['index/base3.html']), {
starttag: '<!-- inject:base3:html -->'
}))
.pipe(print(function (file) {
return "Processing " + file;
}))
.pipe(gulp.dest('./'));
});
});
事情是这样的:
之前:
<body>
abcd
<!-- inject:base3:html -->
<!-- endinject -->
efgh
之后:
<body>
abcd
<!-- inject:base3:html -->
<link rel="import" href="/index/base3.html">
<!-- endinject -->
efgh
我希望看到的是文件的实际内容,而不是行:
<link rel="import" href="/index/base3.html">
任何人都可以告诉我我是否可以用 gulp-inject
做这个 "inserting contents of a file" 或者我是否应该使用其他东西。请注意,过去我尝试使用 gulp-mustache
但我在插入字符 (65279) 时遇到了问题。我认为这与以下问题有关,但我从来没有让小胡子为我工作:
我希望有人可以给我一些建议,使用 gulp-inject
或 gulp-mustache
或任何其他将文件内容插入另一个文件的方法。
使用custom transform function注入文件内容:
gulp.task('watch-index-html', function () {
gulp.watch(files, function (file) {
return gulp.src('index/index.html')
.pipe(inject(gulp.src(['index/base3.html']), {
starttag: '<!-- inject:base3:html -->',
transform: function(filepath, file) {
return file.contents.toString();
}
}))
.pipe(print(function (file) {
return "Processing " + file;
}))
.pipe(gulp.dest('./'));
});
});
这是我目前尝试过的方法:
gulp.task('watch-index-html', function () {
gulp.watch(files, function (file) {
return gulp.src('index/index.html')
.pipe(inject(gulp.src(['index/base3.html']), {
starttag: '<!-- inject:base3:html -->'
}))
.pipe(print(function (file) {
return "Processing " + file;
}))
.pipe(gulp.dest('./'));
});
});
事情是这样的:
之前:
<body>
abcd
<!-- inject:base3:html -->
<!-- endinject -->
efgh
之后:
<body>
abcd
<!-- inject:base3:html -->
<link rel="import" href="/index/base3.html">
<!-- endinject -->
efgh
我希望看到的是文件的实际内容,而不是行:
<link rel="import" href="/index/base3.html">
任何人都可以告诉我我是否可以用 gulp-inject
做这个 "inserting contents of a file" 或者我是否应该使用其他东西。请注意,过去我尝试使用 gulp-mustache
但我在插入字符 (65279) 时遇到了问题。我认为这与以下问题有关,但我从来没有让小胡子为我工作:
我希望有人可以给我一些建议,使用 gulp-inject
或 gulp-mustache
或任何其他将文件内容插入另一个文件的方法。
使用custom transform function注入文件内容:
gulp.task('watch-index-html', function () {
gulp.watch(files, function (file) {
return gulp.src('index/index.html')
.pipe(inject(gulp.src(['index/base3.html']), {
starttag: '<!-- inject:base3:html -->',
transform: function(filepath, file) {
return file.contents.toString();
}
}))
.pipe(print(function (file) {
return "Processing " + file;
}))
.pipe(gulp.dest('./'));
});
});