问题描述
当我使用来缩小Javascript文件时,订单会变得混乱。
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'))
});
将uglify行添加到它会更改 jquery
和函数
文件和地点 functions.js
上面 jquery
$ b
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'))
});
任何可能的解决方案?
当然,functions.js只是一个带有功能的平面Javascript文件,并不包含在IIFE中。
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.
推荐答案
对于使用 hoist_funs
是正确的,但这是的选项。因此,它需要在 compress
选项中提供:
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 }
}))
这篇关于gulp-uglify不会保留文件顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!