gulp-uglify won't preserve files order

不羁岁月 提交于 2019-12-10 10:56:46

问题


When I use gulp-uglify to minify the Javascript files the order gets messed up.

Lets say I have this task working as expected:

var gulp = require('gulp');
var rename = require('gulp-rename');
var gp_concat = require('gulp-concat');

gulp.task('js', function() {
    gulp.src([
            './public/bower_components/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('combined.js'))
        .pipe(gulp.dest(path.js + '/dist'))
});

Adding the uglify line to it changes the order of the jquery and functions files and places functions.js above jquery.

var gulp = require('gulp');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var gp_concat = require('gulp-concat');

gulp.task('js', function() {
    gulp.src([
            './public/bower_components/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('combined.js'))
        .pipe(gulp.dest(path.js + '/dist'))
        .pipe(uglify({
            preserveComments: 'license'
        }))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(path.js + '/dist'))
});

Any possible solution to it ? Of course, functions.js is just a plane Javascript file with funtions in it and are not wrapped in an IIFE.


回答1:


Karol Klepacki's answer is correct about using hoist_funs, but that's an option for the UglifyJS compressor. As such it needs to be provided within the compress option:

.pipe(uglify({
   preserveComments: 'license',
   compress: { hoist_funs: false }
}))



回答2:


Please try disabling functions hoisting:

var gulp = require('gulp');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var gp_concat = require('gulp-concat');

gulp.task('js', function() {
    gulp.src([
            './public/bower_components/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('combined.js'))
        .pipe(gulp.dest(path.js + '/dist'))
        .pipe(uglify({
            preserveComments: 'license',
            hoist_funs: false
        }))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(path.js + '/dist'))
});


来源:https://stackoverflow.com/questions/39510601/gulp-uglify-wont-preserve-files-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!