gpt4 book ai didi

javascript - Gulp Plumber 或 PrettyError 无法在循环中工作

转载 作者:行者123 更新时间:2023-12-03 06:38:39 26 4
gpt4 key购买 nike

我有一个 gulp watch 的问题,错误后会中断。然后我找到了一个不错的reference使用水管工及其扩展,gulp-prettyerror .

然后我创建这个gulpfile.js

const gulp          = require('gulp'),
babel = require('gulp-babel')
changed = require('gulp-changed'),
prettyError = require('gulp-prettyerror');


////////////////////////// START SQUAREBOOK ////////////////////////////////
const reactSquarebookSource = './common/modules/squarebook/web/jsx/*.{js,jsx}';
const reactSquarebookDest = './common/modules/squarebook/web/js';

// run babel on squarebook
gulp.task('babel:squarebook', function () {
return gulp.src(reactSquarebookSource)
.pipe(prettyError())
.pipe(changed(reactSquarebookDest)) // make sure only changed source
.pipe(babel()) // do the babel
.pipe(gulp.dest(reactSquarebookDest));
});

gulp.task('watch:squarebook', function () {
gulp.watch(reactSquarebookSource, ['babel:squarebook']);
});
////////////////////////// FINISH SQUAREBOOK ///////////////////////////////


///////////////////////// START FRONTEND ///////////////////////////////////
const reactFrontendSource = './frontend/web/jsx/*.{js,jsx}';
const reactFrontendDest = './frontend/web/js';

// run babel on frontend
gulp.task('babel:frontend', function () {
return gulp.src(reactFrontendSource)
.pipe(prettyError())
.pipe(changed(reactFrontendDest)) // make sure only changed source
.pipe(babel()) // do the babel
.pipe(gulp.dest(reactFrontendDest));
});

gulp.task('watch:frontend', function () {
gulp.watch(reactFrontendSource, ['babel:frontend']);
});
///////////////////////// FINISH FRONTEND //////////////////////////////////

// all babel react
gulp.task('babel', [
'babel:squarebook',
'babel:frontend'
])

// all watchers
gulp.task('watch', [
'watch:squarebook',
'watch:frontend'
]);

gulp.task('default', [
'babel',
'watch'
]);

prettyError 工作得很好。我喜欢。但这段代码相当多余。我仍然需要创建更多模块,这将使我每次创建模块时都复制粘贴任务。所以我决定将其重构为:

// require all the libraries
const gulp = require('gulp'),
babel = require('gulp-babel')
changed = require('gulp-changed'),
prettyError = require('gulp-prettyerror');

// react source map
const moduleSources = {
squarebook: {
src : './common/modules/squarebook/web/jsx/*.{js,jsx}',
dest : './common/modules/squarebook/web/js'
},
frontend: {
src : './frontend/web/jsx/*.{js,jsx}',
dest : './frontend/web/js'
}
}

gulp.task('babel', function () {
for(var moduleName in moduleSources) {
var sourceMap = moduleSources[moduleName];
var taskName = 'babel:' + moduleName;

// create the task
gulp.task(taskName, function () {
return gulp.src(sourceMap.src)
.pipe(changed(sourceMap.dest)) // make sure only changed source
.pipe(prettyError())
.pipe(babel()) // do the babel
.pipe(gulp.dest(sourceMap.dest));
});
// do the watcher
gulp.watch(sourceMap.src, [taskName]);
}
});

gulp.task('default', [
'babel'
]);

现在我尝试在 './common/modules/squarebook/web/jsx/*.{js,jsx}' 上创建错误,但错误未显示。看起来 PrettyError 只显示最后一个循环中的错误。观察者没有中断,但没有显示错误。知道为什么会发生这种情况吗?

我想知道循环是否出错。

最佳答案

问题是我在匿名函数内使用sourceMap,它将更新直到循环结束。所以,我的解决方案是:

// require all the libraries
const gulp = require('gulp'),
babel = require('gulp-babel')
changed = require('gulp-changed'),
prettyError = require('gulp-prettyerror');

// react source map
const moduleSources = {
squarebook: {
src : './common/modules/squarebook/web/jsx/*.{js,jsx}',
dest : './common/modules/squarebook/web/js'
},
frontend: {
src : './frontend/web/jsx/*.{js,jsx}',
dest : './frontend/web/js'
}
}

// http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
// create function to ensure the right closure
function processBabel(src, dest) {
console.log(src);
return gulp.src(src)
.pipe(changed(dest)) // make sure only changed source
.pipe(prettyError())
.pipe(babel()) // do the babel
.pipe(gulp.dest(dest));
}

var babelTasks = [];
gulp.task('babel', function () {
for(var moduleName in moduleSources) {
var sourceMap = moduleSources[moduleName];
var taskName = 'babel:' + moduleName;

// create the task
gulp.task(taskName, processBabel.bind(this, sourceMap.src, sourceMap.dest));

// do the watcher
gulp.watch(sourceMap.src, [taskName]);
}
});


gulp.task('default', [
'babel'
]);

因此,我创建了其他函数来处理srcdest,这样它就不会通过引用进行更新。

关于javascript - Gulp Plumber 或 PrettyError 无法在循环中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38073287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com