最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - gulp-minify without rename original files - Stack Overflow

programmeradmin3浏览0评论

It is possible minify js files without add -min suffix?

I want keep original name.

gulp.task("js-external-release", function() {
    gulp.src(config.vendorJs)
        .pipe(minify())
        .pipe(gulp.dest(config.dist + "/scripts"));
});

It is possible minify js files without add -min suffix?

I want keep original name.

gulp.task("js-external-release", function() {
    gulp.src(config.vendorJs)
        .pipe(minify())
        .pipe(gulp.dest(config.dist + "/scripts"));
});
Share Improve this question asked Nov 23, 2015 at 21:53 Ninja.TurtleNinja.Turtle 1371 silver badge6 bronze badges 3
  • What's the actual package name? gulp-uglify does not rename js files. – nathanhleung Commented Nov 23, 2015 at 22:21
  • I used gulp-minify but I changed to gulp-uglify. You are right, gulp-uglify doesn’t rename files. – Ninja.Turtle Commented Nov 24, 2015 at 21:00
  • Ok, I'll make that an answer. – nathanhleung Commented Nov 25, 2015 at 17:57
Add a ment  | 

2 Answers 2

Reset to default 15

This is a very old question, but it still pops up quite high on Google, and since Uglify is no longer actively maintained for ES6+ and gulp-minify is, I thought I’d add that it is (now) perfectly possible to do this with gulp-minify.

If you specify the extension of minified files to be just .js (or whatever your input file extension is) in your gulp-minify options, the minified file will have the same file name as the original. The problem is that gulp-minify by default also piles a non-minified copy of the input file to the output directory – and that non-minified copy gets added after the minified copy, thus overwriting it and leaving you with just a copy of the original.

To get around this, set the noSource option to true – then no copy of the input file is output:

gulp.task("js-external-release", function() {
    gulp.src(config.vendorJs)
    .pipe(minify({
        ext: {
            min: '.js' // Set the file extension for minified files to just .js
        },
        noSource: true // Don’t output a copy of the source file
    }))
    .pipe(gulp.dest(config.dist + '/scripts'));
});

Use gulp-uglify instead - it doesn't rename files, which is the default behavior for most gulp plugins.

var gulp = require('gulp');
var uglify = require('gulp-uglify');

gulp.task("js-external-release", function() {
    gulp.src(config.vendorJs)
        .pipe(uglify())
        .pipe(gulp.dest(config.dist + "/scripts"));
});

Alternatively, you could use gulp-rename, but switching to uglify will eliminate the need for such a plugin and is less plex than adding a new plugin in my opinion.

发布评论

评论列表(0)

  1. 暂无评论