Javascript 咕噜咕噜-;您是否忘记发出异步完成的信号;从uglifyjs切换到terser后
我知道在这方面已经有很多问题了,但我之所以发帖,是因为在我从UglifyJS改为Terser之前,一切都很顺利,因为我希望能够压缩ES6代码 我返回流并在相关的地方使用Javascript 咕噜咕噜-;您是否忘记发出异步完成的信号;从uglifyjs切换到terser后,javascript,gulp,Javascript,Gulp,我知道在这方面已经有很多问题了,但我之所以发帖,是因为在我从UglifyJS改为Terser之前,一切都很顺利,因为我希望能够压缩ES6代码 我返回流并在相关的地方使用done(),但是在命令行上运行gulp时,我遇到了这个错误 这里是我使用简洁的相关部分: var terser_options = { compress: { keep_fnames: true }, mangle: false, keep_classnames: true,
done()
,但是在命令行上运行gulp
时,我遇到了这个错误
这里是我使用简洁的相关部分:
var terser_options = {
compress: {
keep_fnames: true
},
mangle: false,
keep_classnames: true,
keep_fnames: true
};
gulp.task('uglify', gulp.series('scripts', function() {
let stream;
for (const bundle of bundleFolders) {
// Get just the last directory of 'js/dev/bootstrap', 'js/dev/lib`, etc.
let thisBundle = path.basename(bundle);
if (thisBundle === 'bootstrap') {
stream = merge2(
gulp.src(bootstrap_files)
.pipe(terser(terser_options))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'))
);
} else {
stream = gulp.src(bundle + "**/*.js")
.pipe(gulpif('!**/*.min.js', terser(terser_options)))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'));
}
}
return stream;
}));
var bootstrap_files = [
// We have to set the bootstrap lines separately as some need to go before others
'js/dev/bootstrap/alert.js',
'js/dev/bootstrap/collapse.js',
'js/dev/bootstrap/tooltip.js',
'js/dev/bootstrap/popover.js',
'js/dev/bootstrap/tab.js',
'js/dev/bootstrap/transition.js'
];
const bundleFolders = glob.sync('js/dev/*/'); // returns an array of folders
var terser_options = {
compress: {
keep_fnames: true
},
mangle: false,
keep_classnames: true,
keep_fnames: true
};
gulp.task('scripts', () => {
let stream;
for (const bundle of bundleFolders) {
// Get just the last directory of 'js/dev/bootstrap', 'js/dev/lib`, etc.
let thisBundle = path.basename(bundle);
if (thisBundle === 'bootstrap') {
stream = merge2(
gulp.src(bootstrap_files)
.pipe(sourcemaps.init())
.pipe(concat(thisBundle + '.js'))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./js'))
);
} else {
stream = gulp.src(bundle + "**/*.js")
.pipe(sourcemaps.init())
.pipe(concat(thisBundle + '.js'))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./js'));
}
}
return stream;
});
gulp.task('uglify', gulp.series('scripts', function() {
let stream;
for (const bundle of bundleFolders) {
// Get just the last directory of 'js/dev/bootstrap', 'js/dev/lib`, etc.
let thisBundle = path.basename(bundle);
if (thisBundle === 'bootstrap') {
stream = merge2(
gulp.src(bootstrap_files)
.pipe(terser(terser_options))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'))
);
} else {
stream = gulp.src(bundle + "**/*.js")
.pipe(gulpif('!**/*.min.js', terser(terser_options)))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'));
}
}
return stream;
}));
// create a task that ensures the `uglify` task is complete before
// reloading browsers
gulp.task('js-watch', gulp.series('uglify', function (done) {
browserSync.reload();
done();
}));
/* Creates the standard version */
gulp.task('styles', function() {
return gulp.src('scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./css/'))
.pipe(browserSync.stream());
});
/* Creates the minified version */
gulp.task('css-minify', gulp.series('styles', function() {
return gulp.src('scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compact' // Options: nested, expanded, compact, compressed
}).on('error', sass.logError))
.pipe(postcss([
autoprefixer({
cascade: false
}),
]))
.pipe(cleanCSS({
advanced: false,
aggressiveMerging: false
}))
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./css/'));
}));
gulp.task('browser-sync', function(done) {
browserSync.init({
open: 'external',
proxy: site_url,
host: site_url,
// port: 5000,
browser: "chrome",
});
done();
});
gulp.task('watch', gulp.series('browser-sync', function() {
gulp.watch('scss/**/*.scss', gulp.series('css-minify'));
gulp.watch('js/dev/**/*.js', gulp.series('js-watch'));
}));
gulp.task('default', gulp.series('js-watch', 'css-minify'));
以下是大部分代码:
var terser_options = {
compress: {
keep_fnames: true
},
mangle: false,
keep_classnames: true,
keep_fnames: true
};
gulp.task('uglify', gulp.series('scripts', function() {
let stream;
for (const bundle of bundleFolders) {
// Get just the last directory of 'js/dev/bootstrap', 'js/dev/lib`, etc.
let thisBundle = path.basename(bundle);
if (thisBundle === 'bootstrap') {
stream = merge2(
gulp.src(bootstrap_files)
.pipe(terser(terser_options))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'))
);
} else {
stream = gulp.src(bundle + "**/*.js")
.pipe(gulpif('!**/*.min.js', terser(terser_options)))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'));
}
}
return stream;
}));
var bootstrap_files = [
// We have to set the bootstrap lines separately as some need to go before others
'js/dev/bootstrap/alert.js',
'js/dev/bootstrap/collapse.js',
'js/dev/bootstrap/tooltip.js',
'js/dev/bootstrap/popover.js',
'js/dev/bootstrap/tab.js',
'js/dev/bootstrap/transition.js'
];
const bundleFolders = glob.sync('js/dev/*/'); // returns an array of folders
var terser_options = {
compress: {
keep_fnames: true
},
mangle: false,
keep_classnames: true,
keep_fnames: true
};
gulp.task('scripts', () => {
let stream;
for (const bundle of bundleFolders) {
// Get just the last directory of 'js/dev/bootstrap', 'js/dev/lib`, etc.
let thisBundle = path.basename(bundle);
if (thisBundle === 'bootstrap') {
stream = merge2(
gulp.src(bootstrap_files)
.pipe(sourcemaps.init())
.pipe(concat(thisBundle + '.js'))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./js'))
);
} else {
stream = gulp.src(bundle + "**/*.js")
.pipe(sourcemaps.init())
.pipe(concat(thisBundle + '.js'))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./js'));
}
}
return stream;
});
gulp.task('uglify', gulp.series('scripts', function() {
let stream;
for (const bundle of bundleFolders) {
// Get just the last directory of 'js/dev/bootstrap', 'js/dev/lib`, etc.
let thisBundle = path.basename(bundle);
if (thisBundle === 'bootstrap') {
stream = merge2(
gulp.src(bootstrap_files)
.pipe(terser(terser_options))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'))
);
} else {
stream = gulp.src(bundle + "**/*.js")
.pipe(gulpif('!**/*.min.js', terser(terser_options)))
.pipe(concat(thisBundle + '.min.js'))
.pipe(gulp.dest('./js'));
}
}
return stream;
}));
// create a task that ensures the `uglify` task is complete before
// reloading browsers
gulp.task('js-watch', gulp.series('uglify', function (done) {
browserSync.reload();
done();
}));
/* Creates the standard version */
gulp.task('styles', function() {
return gulp.src('scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./css/'))
.pipe(browserSync.stream());
});
/* Creates the minified version */
gulp.task('css-minify', gulp.series('styles', function() {
return gulp.src('scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compact' // Options: nested, expanded, compact, compressed
}).on('error', sass.logError))
.pipe(postcss([
autoprefixer({
cascade: false
}),
]))
.pipe(cleanCSS({
advanced: false,
aggressiveMerging: false
}))
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest('./css/'));
}));
gulp.task('browser-sync', function(done) {
browserSync.init({
open: 'external',
proxy: site_url,
host: site_url,
// port: 5000,
browser: "chrome",
});
done();
});
gulp.task('watch', gulp.series('browser-sync', function() {
gulp.watch('scss/**/*.scss', gulp.series('css-minify'));
gulp.watch('js/dev/**/*.js', gulp.series('js-watch'));
}));
gulp.task('default', gulp.series('js-watch', 'css-minify'));
编辑:我在添加terser时也安装了npm
,因此它可能已经更新了gulp,但我已经使用4.*有一段时间了,特别是这是我的软件包的版本行。json:^4.0.2
编辑2:似乎4.0.2
仍然是最新版本,并且至少一年没有更新,因此我仍然使用同一版本的Gulp
Edit3:我刚刚更新了另一个项目中的gulp.js
文件,使其使用更简洁,而且似乎效果很好。gulp.js
文件完全相同
这里的问题是什么?好的,奇怪的一个,但解决了问题 不确定为什么会发生这种情况,但它似乎有问题,并给出了这个错误,因为我的
js/dev
目录是空的
我不确定这些文件是如何被删除的,但幸运的是我能够恢复它们,但最终这并不是一条非常有用的错误消息
如果您收到此错误,并且它似乎不是由于错误消息所述的原因,请检查您的源目录/文件是否实际存在
现在它工作正常。您在哪里/何时收到错误?@ChrisYungmann我在尝试在命令行上运行
gulp
时收到错误;我也用一个新发现更新了这篇文章。