问题
I'm using gulp-uglify to minify my code, and there's a thing I would like to change .
I have the next gulpfile.js
. When I do gulp calendar-uglify
I got the compile inside /compile/ directory and with the name calendar.min.js
. If I change the name inside uglify
and I run again the command, gulp generates the name before again.
I mean, it seems that uglify cannot compile the file with the name I wrote. Gulp-uglify allways takes the filename of the concat file.
What can I do to change that?
TY,
Jaster
gulpfile.js
// Dependencies
var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');
// Private callbacks
/**
* Calendar js concat
* @private
**/
var _calendarConcat = function(){
return gulp.src(['./src/olympics/Calendar.js', './src/entry/calendar.js'])
.pipe(concat('calendar.min.js'))
.pipe(gulp.dest('./dist'));
};
/**
* Calendar js uglify
* @private
**/
var _calendarUglify = function(){
return gulp.src('./dist/calendar.min.js')
.pipe(uglify('calendar.min.js', {
mangle: true,
output: {
beautify: true
}
}))
.pipe(gulp.dest('./compile/'));
};
// Task def
gulp.task('calendar-concat', _calendarConcat);
gulp.task('calendar-uglify', _calendarUglify);
回答1:
The gulp-uglify documentation doesn't explicitly state the method signature for uglify()
anywhere, but you can look at the source code to figure it out. It's essentially this:
uglify([opts, [uglify]])
Where
opts
is an optional map of options.uglify
is an optional instance of uglifyJs
So there's no way specify a different file name using uglify()
.
There's no need for that anyway. Gulp plugins are supposed to do one thing and one thing well. That way you can combine them into more complex pipelines.
In your case you're looking for the gulp-rename plugin which let's you rename a file:
var rename = require('gulp-rename');
var _calendarUglify = function(){
return gulp.src('./dist/calendar.min.js')
.pipe(uglify({
mangle: true,
output: {
beautify: true
}
}))
// renames the file to calendar2.min.js
.pipe(rename({basename:'calendar2.min'}))
.pipe(gulp.dest('./compile/'));
};
来源:https://stackoverflow.com/questions/38244655/gulp-uglify-cannot-change-the-name