英文:
Running gulp.series programmatically from Node app does not run all tasks
问题
以下是翻译好的部分:
目标: 从Node应用程序内部以编程方式运行gulp任务,以在应用程序运行时使用。
问题: 仅运行gulp.series
中的前两个任务(clean
和handlebars
),完全跳过inline
任务。
问题: 有人能告诉我为什么似乎只运行前两个任务吗?
以下代码位于名为app.js
的文件中。我通过在终端中键入node app.js
来运行该文件。
以下是我的代码: 请滚动查看完整代码。
const gulp = require('gulp');
const rename = require('gulp-rename');
const inlineCSS = require('gulp-inline-css');
const clean = require('gulp-clean');
const panini = require('panini');
// 编译Handlebars模板
gulp.task('handlebars', async function () {
return gulp
.src('views/pages/*.hbs')
.pipe(
panini({
root: 'views/pages',
layouts: 'views/layouts',
partials: 'views/partials',
})
)
.pipe(
rename(function (path) {
path.basename += '-send';
path.extname = '.html';
})
)
.pipe(gulp.dest('dist/inline-html'));
});
// 将app.css中的CSS内联到编译后的HTML文件中
gulp.task('inline', async function () {
return gulp
.src('dist/inline-html/*.html')
.pipe(inlineCSS())
.pipe(gulp.dest('dist/send-html'));
});
// 清理dist文件夹
gulp.task('clean', async function () {
return gulp.src('dist/**/*', { read: false }).pipe(clean());
});
async function gulpTaskRunner() {
return new Promise(function (resolve, reject) {
gulp.series('clean', 'handlebars', 'inline', (done) => {
console.log('Gulp任务已完成');
resolve();
done();
})();
});
}
async function app() {
console.log('开始');
await gulpTaskRunner();
console.log('结束');
}
app();
希望这对你有所帮助。如果你需要进一步的解释或有其他问题,请随时提出。
英文:
The objective: Run gulp tasks programmatically from within a node application to be used at run time of application.
The problem: Only the first two tasks in gulp.series
are being run (clean
and handlebars
), inline
task is being completely skipped.
The Question: Can anyone tell me why only the first two tasks seem to run?
The following code is in a file called app.js
. I run the file by typing node app.js
in the terminal.
Here is my code: Please scroll to see full code.
const gulp = require('gulp');
const rename = require('gulp-rename');
const inlineCSS = require('gulp-inline-css');
const clean = require('gulp-clean');
const panini = require('panini');
//Compile Handlebars templates
gulp.task('handlebars', async function () {
return gulp
.src('views/pages/*.hbs')
.pipe(
panini({
root: 'views/pages',
layouts: 'views/layouts',
partials: 'views/partials',
})
)
.pipe(
rename(function (path) {
path.basename += '-send';
path.extname = '.html';
})
)
.pipe(gulp.dest('dist/inline-html'));
});
//Inline CSS from app.css into compiled HTML files
gulp.task('inline', async function () {
return gulp
.src('dist/inline-html/*.html')
.pipe(inlineCSS())
.pipe(gulp.dest('dist/send-html'));
});
//Clean dist folder
gulp.task('clean', async function () {
return gulp.src('dist/**/*', { read: false }).pipe(clean());
});
async function gulpTaskRunner() {
return new Promise(function (resolve, reject) {
gulp.series('clean', 'handlebars', 'inline', (done) => {
console.log('Gulp task completed');
resolve();
done();
})();
});
}
async function app() {
console.log('start');
await gulpTaskRunner();
console.log('end');
}
app();
答案1
得分: 1
I guess the problem could be in your inline
task:
// 将 app.css 中的内联 CSS 编译到 HTML 文件中
gulp.task('inline', function () {
return gulp
.src('dist/inline-html/*.html')
.pipe(inlineCSS())
.pipe(gulp.dest('dist/send-html'));
});
它立即返回一个已完成的 gulp 流的已解决承诺。因此,该任务被视为已完成,应用程序在不等待流的情况下结束。只需从中删除 async
即可。
英文:
I guess the problem could be in your inline
task:
//Inline CSS from app.css into compiled HTML files
gulp.task('inline', async function () {
return gulp
.src('dist/inline-html/*.html')
.pipe(inlineCSS())
.pipe(gulp.dest('dist/send-html'));
});
It returns immediately with a fulfilled promise of a gulp stream. So the task is considered as complete and the app finishes without waiting your streams. Just remove async
from it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论