I am:
bundling with browserify
transforming ES6 to ES5 with babel
minifying the ES5 with uglifyjs
Which previously worked. However recently I've been getting errors from uglifyjs complaining about ES6 syntax, as if babelify hasn't actually run:
gulp.task('js', function() {
// Browserify/bundle the JS.
browserify({
entries: './public/js/src/index.js',
insertGlobals : true,
fullPaths: true, // For discify
debug: ! isProduction
}).transform(babelify)
.bundle()
.pipe(source('index.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./public/js/dist'))
});
Why isn't transform(babelify) transforming the code?
Please give actual answers, rather than cut and pasted gulpfiles.
The issue was using npm modules: babel ignores npm modules by default. So if the modules are ES6, they'll still be in ES6 when uglify runs.
Upgrading babel and using the global option has fixed things:
gulp.task('js', function() {
browserify({
entries: './public/js/src/index.js',
insertGlobals : true,
fullPaths: true, // For discify
debug: ! isProduction
}).transform(babelify, {
presets: ['es2015'],
compact: false,
global: true
})
.bundle()
.pipe(source('index.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./public/js/dist'))
})
Another option is to put this in package.json in your private modules. Note the syntax is strange, and uses arrays rather than objects to match items to their options:
{
"browserify": {
"transform": [
[
"babelify",
{ "presets": ["es2015"] }
]
]
}
}
See the babelify docs for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With