Running gulp.series programmatically from Node app does not run all tasks.

huangapple go评论103阅读模式
英文:

Running gulp.series programmatically from Node app does not run all tasks

问题

以下是翻译好的部分:

目标: 从Node应用程序内部以编程方式运行gulp任务,以在应用程序运行时使用。

问题: 仅运行gulp.series中的前两个任务(cleanhandlebars),完全跳过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.

huangapple
  • 本文由 发表于 2023年6月9日 05:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定