MikeKovarik / gulp-better-rollup

📦 Better Gulp plugin for Rollup ES6 module bundler
MIT License
49 stars 16 forks source link

How to minify the script? #27

Open verlok opened 6 years ago

verlok commented 6 years ago

Hey, how can I minify the script using uglify or other similar?

I'm currently doing:

gulp.task("default", function() {
    process.env.NODE_ENV = "release";
    return (
        gulp.
            src("./src/lazyload.js").
            pipe(sourcemaps.init()).
            // ----------- linting --------------
            pipe(eslint()).
            pipe(eslint.format()).
            pipe(eslint.failAfterError()). // --> failing if errors
            // ----------- rolling up --------------
            pipe(rollup({ plugins: [babel()] }, "umd")).
            pipe(sourcemaps.write("")).
            pipe(gulp.dest(destFolder)). // --> writing not uglified
    );
});

But if I try to add the following lines afterwards, the code I get in destFolder is ES6 code.

            pipe(uglify()).
            pipe(rename("lazyload.min.js")).
            pipe(gulp.dest(destFolder)) // --> writing uglified

What am I doing wrong?

ekfuhrmann commented 5 years ago

I know this has been open for awhile, but there are rollup plugins for minifying.

If you are using es5 you can use the following plugin, or if you're using es6 you can use this plugin.

Simply add the plugin to your rollup plugins option and you should be good to go.

Example:

gulp.task('scripts', () => {
  return gulp
    .src('main.js')
    .pipe(sourcemaps.init())
    .pipe(rollup({ plugins: [uglify(), babel()] }, { format: 'cjs' }))
    .pipe(sourcemaps.write(''))
    .pipe(gulp.dest('dist'));
});