I came across the following on the official gulp-uglify npm page.
Note the recommendation:
To help properly handle error conditions with Node streams, this
project recommends the use of pump. For more information, see Why Use
Pump?.
My implementation of the example on the above page is:
gulpfile.js
var gulp = require('gulp');
var pump = require('pump');
// the npm page states:
// can be a git checkout or another module
// (such as `uglify-js-harmony` for ES6 support)
var uglifyjs = require('uglify-js-harmony');
var minifier = require('gulp-uglify/minifier');
var concat = require('gulp-concat');
gulp.task('compress', function (cb) {
// the 'options' argument is included in the example,
// but i don't know what it does or how it can be removed
var options = {
preserveComments: 'license'
};
pump([
gulp.src('my_scripts/*.js'), // gets all js scripts in this folder
minifier(options, uglifyjs),
concat('my_bundled_script.js'), // concatenates all scripts into this file
gulp.dest('dist') // puts the file into this folder
],cb
);
});
package.json
{
"devDependencies": {
"gulp": "latest",
"pump": "latest",
"gulp-concat": "latest",
"gulp-uglify": "latest",
"uglify-js-harmony": "latest"
},
"dependencies": {}
}
Results
Without using uglify-js-harmony
:
pump
was helpful in locating where the error came from (when using gulp-uglify
)
- a file that had a
let
statement caused errors
When using uglify-js-harmony
, the errors did not occur.
Other considerations:
The above page currently shows:
// can be a git checkout or another module
// (such as `uglify-js-harmony` for ES6 support)
var uglifyjs = require('uglify-js');
But when installing uglify-js-harmony
I got the warning message:
npm WARN deprecated [email protected]: deprecated in favour of
uglify-es
When I tried to use uglify-es
instead of uglify-js-harmony
, however, I got an error message, similar to that documented here:
https://github.com/terinjokes/gulp-uglify/issues/287
I tried to follow the issue thread from there but got lost and couldn't find a definitive solution of how to implement uglify-es
.
Unexpected token name «i», expected punc «;»
when it encountered alet
keyword in a for loop declaration.for (let i = ca.length - 1; i >= 0; i--)
. Changinglet
tovar
seems to solve the issue. – Cyte