Using Gulp to Concatenate and Uglify files

后端 未结 6 2183
一生所求
一生所求 2020-11-27 09:39

I\'m trying to use Gulp to:

  1. Take 3 specific javascript files, concatenate them, then save the result to a file (concat.js)
  2. Take this concatenated file
6条回答
  •  醉梦人生
    2020-11-27 10:20

    Jun 10 2015: Note from the author of gulp-uglifyjs:

    DEPRECATED: This plugin has been blacklisted as it relies on Uglify to concat the files instead of using gulp-concat, which breaks the "It should do one thing" paradigm. When I created this plugin, there was no way to get source maps to work with gulp, however now there is a gulp-sourcemaps plugin that achieves the same goal. gulp-uglifyjs still works great and gives very granular control over the Uglify execution, I'm just giving you a heads up that other options now exist.


    Feb 18 2015: gulp-uglify and gulp-concat both work nicely with gulp-sourcemaps now. Just make sure to set the newLine option correctly for gulp-concat; I recommend \n;.


    Original Answer (Dec 2014): Use gulp-uglifyjs instead. gulp-concat isn't necessarily safe; it needs to handle trailing semi-colons correctly. gulp-uglify also doesn't support source maps. Here's a snippet from a project I'm working on:

    gulp.task('scripts', function () {
        gulp.src(scripts)
            .pipe(plumber())
            .pipe(uglify('all_the_things.js',{
                output: {
                    beautify: false
                },
                outSourceMap: true,
                basePath: 'www',
                sourceRoot: '/'
            }))
            .pipe(plumber.stop())
            .pipe(gulp.dest('www/js'))
    });
    

提交回复
热议问题