Reputation: 20099
I know there are a lot of questions on this already, but I'm posting because things were working totally fine until I changed from UglifyJS to Terser as I wanted to be able to compress ES6 code.
I'm returning the streams and using done()
where relevant as well, yet I am getting this error when running gulp
on the command line?
Here is the relevant section where I am using Terser:
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;
}));
Here is the majority of the code:
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'));
Edit: I did do a npm install
when I added terser as well, so it may have updated gulp, but I have been using 4.* for a while now, specifically this is the version line from my package.json: ^4.0.2
Edit 2: Seems 4.0.2
still is the latest version and hasn't been updated in at least a year, so I would still be on the same version of Gulp.
Edit3: I just updated the gulp.js
file on another project to use terser and it seemed to work fine. The gulp.js
files are exactly the same.
What is the issue here?
Upvotes: 0
Views: 168
Reputation: 20099
Ok, strange one but figured out the issue.
Unsure why it happened, but it seems it was having issues and giving this error because my js/dev
directory was empty.
Not sure how the files got deleted, but fortunately I was able to restore them, but at the end of the day it wasn't a very helpful error message.
If you're getting this error and it doesn't appear to be for the reasons stated by the error message, check your source directory/files actually exist.
Now it works fine.
Upvotes: 1