javascript - gulp-uglify won't preserve files order - Stack Overflow

When I use gulp-uglify to minify the Javascript files the order gets messed up.Lets say I have this tas

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_ponents/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('bined.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_ponents/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('bined.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.

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_ponents/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('bined.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_ponents/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('bined.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.

Share Improve this question asked Sep 15, 2016 at 12:04 AlvaroAlvaro 41.6k31 gold badges172 silver badges348 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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

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

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_ponents/jquery/dist/jquery.min.js',
            './public/js/functions.js',
        ])
        .pipe(gp_concat('bined.js'))
        .pipe(gulp.dest(path.js + '/dist'))
        .pipe(uglify({
            preserveComments: 'license',
            hoist_funs: false
        }))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(path.js + '/dist'))
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745619451a4636433.html

相关推荐

  • javascript - gulp-uglify won't preserve files order - Stack Overflow

    When I use gulp-uglify to minify the Javascript files the order gets messed up.Lets say I have this tas

    8小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信