11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gulp-uglifyでコメントを残す方法が変わったの?

Last updated at Posted at 2017-06-22

お題

新しいPCにgulp周り一式を導入したところ、ライセンスコメントを残すところで引っかかったのでメモ。

ライセンスコメントを残すには

今まで

gulpfile.js
// ggっても大体こんな感じ
gulp.task('uglify', function() {
    return gulp.src('./dev/app.js')
        .pipe(plumber())
        .pipe(uglify({preserveComments: 'some'}))
        .pipe(rename('app.min.js'))
        .pipe(gulp.dest('./'));
});

↓ 実行したら

GulpUglifyError: unable to minify JavaScript
Caused by: DefaultsError: `preserveComments` is not a supported option

怒られた。

どうなってるの!

2017/6/22時点でgulp-uglifyをインストールすると、
https://github.com/mishoo/UglifyJS2
がついてくるらしい。
ちゃんと読んでないけど、もしかしてつい最近切り替わったんだろうか?

usageを見てみると、コマンドラインで実行する場合
コメントを残したりは--comments オプションでやるらしい。
実際に

uglifyjs --compress --mangle --comments -o app.min.js -- app.js

を実行すると、きちんとライセンスコメントが残ったまま書き出される。

じゃあ

gulpfile.js
gulp.task('uglify', function() {
    return gulp.src('./dev/app.js')
        .pipe(plumber())
        .pipe(uglify({comments: 'some'})) // キーを変えた
        .pipe(rename('app.min.js'))
        .pipe(gulp.dest('./'));
});

↓ 実行したら

GulpUglifyError: unable to minify JavaScript
Caused by: DefaultsError: `comments` is not a supported option

怒られた。

どうなってるの!!

コマンドラインは動いているので、コマンドラインの際の実行ファイルを見てみる。

uglifyjs-line.82
if (program.comments) {
    if (typeof options.output != "object") options.output = {};
    options.output.comments = typeof program.comments == "string" ? program.comments : "some";
}

outputオブジェクトのメンバになってるじゃないですか…

これでどうだ

gulpfile.js
gulp.task('uglify', function() {
    return gulp.src('./dev/app.js')
        .pipe(plumber())
        .pipe(uglify({output: {comments: 'some'}})) // outputオブジェクトの中に入れた
        .pipe(rename('app.min.js'))
        .pipe(gulp.dest('./'));
});

↓ 実行したら

怒られなかった!

まとめ

ggって解決しないとちょっと泣きそうになる。

11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?