Git Product home page Git Product logo

gulp-uglify's Introduction

gulp-uglify

Minify JavaScript with UglifyJS3.

Installation

Install package with NPM and add it to your development dependencies:

npm install --save-dev gulp-uglify

Usage

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;

gulp.task('compress', function () {
  return pipeline(
        gulp.src('lib/*.js'),
        uglify(),
        gulp.dest('dist')
  );
});

To help properly handle error conditions with Node streams, this project recommends the use of pipeline, from readable-stream.

Options

Most of the minify options from the UglifyJS API are supported. There are a few exceptions:

  1. The sourceMap option must not be set, as it will be automatically configured based on your Gulp configuration. See the documentation for Gulp sourcemaps.

Errors

gulp-uglify emits an 'error' event if it is unable to minify a specific file. The GulpUglifyError constructor is exported by this plugin for instanceof checks. It contains the following properties:

  • fileName: The full file path for the file being minified.
  • cause: The original UglifyJS error, if available.

Most UglifyJS error messages have the following properties:

  • message (or msg)
  • filename
  • line

To see useful error messages, see Why Use Pipeline?.

Using a Different UglifyJS

By default, gulp-uglify uses the version of UglifyJS installed as a dependency. It's possible to configure the use of a different version using the "composer" entry point.

var uglifyjs = require('uglify-js'); // can be a git checkout
                                     // or another module (such as `uglify-es` for ES6 support)
var composer = require('gulp-uglify/composer');
var pump = require('pump');

var minify = composer(uglifyjs, console);

gulp.task('compress', function (cb) {
  // the same options as described above
  var options = {};

  pump([
      gulp.src('lib/*.js'),
      minify(options),
      gulp.dest('dist')
    ],
    cb
  );
});

gulp-uglify's People

Contributors

amksd avatar ciceropablo avatar eowino avatar floridoo avatar greenkeeperio-bot avatar henrygau avatar ilkome avatar jordaaash avatar marcamos avatar mems avatar odino avatar oltodo avatar phated avatar robpalme avatar robrich avatar terinjokes avatar tschaub avatar vladimirmartsul avatar zaygraveyard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gulp-uglify's Issues

What about being able to change output name?

Hi there,

Thanks for the amazing work!

I am using it like this:

gulp.task("scripts", function() {

    gulp.src(["js/first.js", "js/second.js"])
        .pipe(
            concat("all.js")
        )
        .pipe(
            uglify()
        )
        .pipe(
            gulp.dest("js")
        )
        .pipe(
            livereload(server)
        );

});

any way I could save the file with another name?
like saving it as all.js and then on "all.js" change, uglify it to "all.min.js"...
maybe just like concat...having option to parse argument with new name....

cheers!

RFC: Do source maps correctly

As of 0.2.0 source maps are emitted as Vinyl objects on the main gulp stream. However I'm unsure if this causes issues with other downstream gulp plugins, who might not expect non-JavaScript files on the stream.

Is this a real problem actually affecting users, and if so would you be interested in source maps being emitted on a source map substream?

option beautify ascii_only not work

I need to escape Unicode characters in my javascript file
so I set this option
it work in grunt but gulp do`t work

beautify: {
                ascii_only: true
            }

Errors when using output options

Hi,

I am trying to pass additional options to output and get some strange fire back.

[12:16:33] Using gulpfile ~/combatboots/gulpfile.js
[12:16:33] Starting 'compress'...
[12:16:33] Finished 'compress' after 17 ms

/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/gulp-util/lib/PluginError.js:72
  if (!this.message) throw new Error('Missing error message');
                           ^
Error: Missing error message
    at new PluginError (/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/gulp-util/lib/PluginError.js:72:28)
    at DestroyableTransform.minify [as _transform] (/Users/drugdealer/combatboots/node_modules/gulp-uglify/index.js:55:20)
    at DestroyableTransform.Transform._read (/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (/Users/drugdealer/combatboots/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)
    at write (/Users/drugdealer/combatboots/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/Users/drugdealer/combatboots/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)
    at Transform.pipeOnReadable (/Users/drugdealer/combatboots/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:664:5)

I use the following config:

    // uglify options
    var options = {
        mangle: true,
        compress: true,
        preserveComments: 'some',
        output: {
            exportAll: true
        }
    };

gulp-uglify stops working after npm update of sub-package

Hi,

As of today, gulp-uglify started crashing without error. I traced the problem to an updated dependency of uglify-js. If I change the package.json of uglify-js so it has all fixed versions, then it works again (see below). However uglify-js no longer accepts issues as they've moved to UglifyJS2.
Therefore I think that gulp-uglify should have its dependency upgraded to UglifyJS2. Is there a reason we're still on uglify-js?

  "dependencies": {
    "deepmerge": "0.2.7",
    "gulp-util": "3.0.0",
    "through2": "0.6.1",
    "uglify-js": "2.4.15",
    "vinyl-sourcemaps-apply": "0.1.1"
  },
  "devDependencies": {
    "argg": "0.0.1",
    "codeclimate-test-reporter": "0.0.3",
    "gulp-concat": "2.3.4",
    "gulp-sourcemaps": "1.1.1",
    "istanbul": "0.3.0",
    "rimraf": "2.2.8",
    "tape": "2.12.3",
    "vinyl": "0.3.2"
  },

Kind regards,
Rogier

slow

Hi,
running

gulp.src(paths.js.extlib)
            .pipe($.changed(jsDest))
            .pipe($.ngmin())
            .pipe($.uglify())
            .pipe($.concat('extlib.js'))
            .pipe(gulp.dest(jsDest))
            .pipe($.size({ title: 'extlib.js' }))

takes ~1min. It seems ngmin() takes ~30sec and uglify() as well. The output file is 1MB+ though.
Any tips on how to make this faster?

Cheers

handle generator functions

Error caught from uglify: Unexpected token: operator (*)

This maybe forward thinking, but I caught this error when trying to uglify an ES6 generator style function.

gulp.task('scripts', function() {
    return gulp.src('src/*.js')
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('public/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest('public/js'))
        .pipe(livereload(server))
        .pipe(notify({ message: 'Scripts task complete' }));
});

the relevant piece of main.js

...
function *list() {
    this.body = yield render('index', { posts: posts });
}
...

If you know of a workaround please let me know. Thanks!

Add semicolons in doc

Seems like you fogot to add return and semicolons after latest pipe

gulp.task('compress', function() {
return gulp.src('lib/*.js')
.pipe(uglify())
.pipe(gulp.dest('dist'));
});

Support streaming

events.js:72
        throw er; // Unhandled 'error' event
              ^
[gulp] Error in plugin 'gulp-uglify': Streaming not supported
    at uglifyError (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/lib/error.js:6:9)
    at Transform.minify [as _transform] (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/index.js:18:20)
    at Transform._read (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at Transform._write (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:238:10)
    at writeOrBuffer (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:228:5)
    at Transform.Writable.write (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:195:11)
    at write (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/vinyl-source-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:605:24)
    at flow (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/vinyl-source-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:614:7)
    at Transform.pipeOnReadable (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/vinyl-source-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:646:5)

I have to wrap your plugin with streamify to make it work :(

Sourcemaps error when used before gulp-concat, but not after

.pipe(sourcemap.init())
    .pipe(uglify({mangle:false}))
    .pipe(concat('release.js'))
.pipe(sourcemap.write('./'))

I get the following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: no writecb in Transform class
    at afterTransform (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:95:33)
    at TransformState.afterTransform (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:79:12)
    at /Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/index.js:72:12
    at minify (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/index.js:18:3)
    at DestroyableTransform.uglify [as _transform] (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/index.js:70:3)
    at DestroyableTransform.Transform._read (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (/Users/eatstreet/Repos/UOrder/web/WebContent/redesign/node_modules/gulp-uglify/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)

This, however, does not cause an error:

.pipe(sourcemap.init())
    .pipe(concat('release.js'))
    .pipe(uglify({mangle:false}))
.pipe(sourcemap.write('./'))

Uglify not working with gulp-sourcemaps

When I try and uglify my code without concatenating it and then generate source maps I get an error message like:

gulp-sourcemap-write: source file not found:C:\Rob\Dropbox\Repos\gulpTest\?

my gulp task looks like this

gulp.task('js', function () {
    return gulp.src('app.js')
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('build'));
});

I'm using the following versions of npm modules

  "dependencies": {
    "gulp": "3.8.8",
    "gulp-uglify": "1.0.1",
    "gulp-sourcemaps": "1.1.5"
  }

There is a zipped up version of the project here https://dl.dropboxusercontent.com/u/20066539/Forum%20Links/Other/gulpTest.zip

This issue is related to gulp-sourcemaps/gulp-sourcemaps#34

Source mapping concatenated files

I'm getting some unexpected behavior when using the source map option. Here is my code:

gulp
  .src([
    'source/scripts/**/*.js'
  ]))
  .pipe(gulp.dest('public/js'))
  .pipe(concat('app-' + build.version + '.min.js'))
  .pipe(uglify({
    outSourceMap: true
  }))
  .pipe(footer('\n//# sourceMappingURL=app-' + build.version + '.min.js.map'))
  .pipe(gulp.dest('public/js'));

I'm expecting the source map to include all of the files, but there seems to be something wrong with the source map. In the sources property of the map, I just get "sources":["?"]. I should also point out that the same thing happens when I just have one file that I'm minifying. The source map gets requested by the browser, but does nothing with it so the source mapping is useless. (Also note that I have to manually add the sourceMappingURL comment to the end of the output file)

Please, if there is something I'm doing wrong, let me know and close the issue. I will do some more testing and report my findings here.

Unkown property in compress, throws error: "Missing error message"

If I have drop_console: true in compress, I get this error: "Missing error message".
I guess it's because drop_console is no longer supported? I could not find it in the Uglify docs.

If I remove it, it works fine. But maybe there should be a better error message?

Options:

var uglifyOptions =
{
    mangle: false,
    compress:
    {
        drop_console: true,
        global_defs:
        {
            DEBUG: false
        }
    }
};

Error output:

c:\Work\node_modules\gulp-uglify\node_modules\gulp-util\lib\PluginError.js:72
  if (!this.message) throw new Error('Missing error message');
                           ^
Error: Missing error message
    at new PluginError (c:\Work\node_modules\gulp-uglify\node_modules\gulp-util\lib\PluginError.js:72:28)
    at DestroyableTransform.minify [as _transform] (c:\Work\node_modules\gulp-uglify\index.js:55:20)
    at DestroyableTransform.Transform._read (c:\Work\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_s
tream_transform.js:184:10)
    at DestroyableTransform.Transform._write (c:\Work\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_
stream_transform.js:172:12)
    at doWrite (c:\Work\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (c:\Work\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5
)
    at DestroyableTransform.Writable.write (c:\Work\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_st
ream_writable.js:194:11)
    at write (_stream_readable.js:601:24)
    at flow (_stream_readable.js:610:7)
    at _stream_readable.js:578:7

Output fail message

How can I get error message when gulp-uglify fail to compress javascript due to the error in the javascript file?

Get error cannot find module 'gulp-uglify'

Here is the error that i get, but before I paste that I've already done the following:

  1. uninstalled package globally
  2. tried re-installing package
  3. uglify NOT showing up in package.json which means it shouldn't even be looked at
  4. removed .pipe(uglify()) from my compress/scripts gulp task function
  5. finally reinstalled npm install --save-dev gulp-uglify and it saved to my package.json

Still get this error though

events.js:72
throw er; // Unhandled 'error' event
^
Error
at new JS_Parse_Error (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:189:18)
at js_error (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:197:11)
at croak (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:657:9)
at token_error (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:665:9)
at unexpected (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:671:9)
at semicolon (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:691:43)
at /Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:819:38
at /Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:704:24
at block_ (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:978:20)
at ctor.body (/Applications/AMPPS/www/SCC-blog/wp-content/themes/sccblog/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:951:25)

Gulp not showing the error message, only the stack.

Which provides absolutely no value, see the console log of the error, and the output:

[16:10:22] Starting 'build-core-uglify'...
{ message: 'Unexpected token: punc ())',
  line: 12116,
  col: 1,
  pos: 326346,
  stack: 'Error\n    at new JS_Parse_Error (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:196:18)\n    at js_error (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:204:11)\n    at croak (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:663:9)\n    at token_error (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:671:9)\n    at unexpected (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:677:9)\n    at semicolon (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:697:43)\n    at simple_statement (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:871:73)\n    at /Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:744:19\n    at /Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:710:24\n    at block_ (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:984:20)' }

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error
    at new JS_Parse_Error (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:196:18)
    at js_error (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:204:11)
    at croak (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:663:9)
    at token_error (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:671:9)
    at unexpected (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:677:9)
    at semicolon (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:697:43)
    at simple_statement (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:871:73)
    at /Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:744:19
    at /Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:710:24
    at block_ (/Users/mashape/git/mashape-web/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:984:20)

I've found the source of the problem, gulpjs/gulp-util#49, which currently the only solution is to have message as an object with only the message in the object, I've currently constructed my own message from the stack, filename, file line, etc... but I would rather it be cured at the gulp-util level.

Also, the way you are using PluginError appears to not be even required:

    return new PluginError('gulp-uglify', message, options);

Is sufficient enough.

Currently I am doing this:

module.exports = function uglifyError (message, options) {
    return new PluginError('gulp-uglify', {
        message: message,
        stack: options.stack.replace(
            'Error\n',
            'Error [' + message + ']' +
            '\n    in: ' + options.fileName +
            '\n    at line: ' + options.lineNumber +
            '\n'
        )
    }, options);
};

Error: SourceMapGenerator.prototype.applySourceMap when combining with ngAnnotate

I get the following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: SourceMapGenerator.prototype.applySourceMap requires either an explicit source file, or the source map's "file" property. Both were omitted.
    at SourceMapGenerator_applySourceMap [as applySourceMap] (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\vinyl-sourcemaps-apply\node_modules\source-map\lib\source-map\source-map-generator.js:171:17)
    at applySourceMap (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\vinyl-sourcemaps-apply\index.js:11:15)
    at C:\development\angular-gulp\node_modules\gulp-uglify\index.js:78:5
    at minify (C:\development\angular-gulp\node_modules\gulp-uglify\index.js:16:3)
    at DestroyableTransform.uglify [as _transform] (C:\development\angular-gulp\node_modules\gulp-uglify\index.js:70:3)
    at DestroyableTransform.Transform._read (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:12)
    at doWrite (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (C:\development\angular-gulp\node_modules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:194:11)

when using the following workflow:

'use strict';

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();

gulp.task('default', function() {
    gulp.src([
        'src/app.js'
    ])
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.concat('bundle.js'))
    .pipe(plugins.ngAnnotate({
        add: true,
        single_quotes: true
    }))
    .pipe(plugins.uglify())
    .pipe(plugins.sourcemaps.write('.'))
    .pipe(gulp.dest('./src'));
});

If I comment out the pipe to uglify it works, same if I comment out the pipe to ngAnnotate. It appears that the combination of both is causing an issue.

For completeness, the source of src/app.is is

(function() {
    'use strict';

    angular.module('app', []);

    function TestService() 
    {
    }

    function MainCtrl(testService) 
    {

    }

    angular
        .module('app')
        .service('testService', TestService)
        .controller('MainCtrl', MainCtrl);
})();

Renaming minified file after uglify also changes the source-reference in source map

I am using the following Gulp-task:

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

gulp.task('package', function () {
    return gulp.src('./src/**/*.js')
            .pipe(concat('myfile.js'))
            .pipe(gulp.dest('./dist/'))
            .pipe(uglify({ outSourceMap: true }))
            .pipe(rename(function (path) {
                if(path.extname === '.js') {
                    path.basename += '.min';
                }
            }))
            .pipe(gulp.dest('./dist/'));
});

I was expecting this to generate a source map for the minified file, referencing the concatenated file 'myfile.js' in the sources-array of the source map, i.e.

{
    "version": 3,
    "file": "myfile.min.js.map",
    "sources": ["myfile.js"] // <-- This here
    /* SNIP */
}

However, the file referenced is the renamed minified file, i.e. "myfile.min.js". I am not sure if this behavior is intended or not. The reason appears to be that when calling this.push for the minified content all other pipes in the chain are executed synchronously, thus changing the file.relative-property of the file before the source map file is created. I've managed to fix this issue in two different ways, and I am not sure which would be preferrable:

  • Output a new file with the mangled code instead of changing the contents of the existing, so that file.relative never changes. The source map file generates its filename from the newly created file, but adds the old file as a source.
  • Cache the filename before calling this.push, and use the cached name in the sources-array.

I could easily make a pull request with a fix, but I would like some input before I do anything.

gulp-uglify messing with code order

I have a stream of javascript files that include jQuery, another vendor library and some "in-house" javascript.

When I use uglify() on that stream, the other vendor library jumps on top of jQuery.

Why and how that happens, and how to fix it?

gulp.task('scripts', function () {

    var vendorFiles = gulp.src(mainBowerFiles())
        .pipe(gulpFilter('*.js'))
        .pipe(order([
            "**/jquery.js",
            "*.js"
        ]))
        .pipe(concat('vendor.js'))
        .pipe(debug({verbose:true}));

    var appFiles = gulp.src('app/js/*.js')
        // .pipe(jshint())
        // .pipe(jshint.reporter('default'))
        .pipe(concat('app.js'));

    return es.concat(vendorFiles, appFiles)
        .pipe(order([
            "vendor.js",
            "app.js"
        ]))
        .pipe(concat('app.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./dist/js'));
});

Passing multiple files minifies each one separately

When I run a task with this configuration, the output is each file individually compressed instead of all the files being compressed into one and then minified.

gulp.task('uglify', function() {
  var files = [
    'public/js/**/*.js',
    'bower_components/**/*.js'
  ];

  gulp.src(files)
    .pipe(uglify())
    .pipe(gulp.dest('./testing'));

This also happens when a glob pattern matches more than one file:

var files = 'public/js/**/*.js'; // matches multiple files

// same stuff

Using source maps causes task to run twice

gulp.task('scripts', function() {
    return gulp.src('js/app.js')
        .pipe(plumber())
        .pipe(concat('app.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify({ outSourceMap: true }))
        .pipe(gulp.dest('../js'))
        .pipe(notify('Scripts task complete'));
});

When running this task:

[gulp] Running 'scripts'...
[gulp] gulp-notify: [Gulp notification] Scripts task complete
[gulp] gulp-notify: [Gulp notification] Scripts task complete
[gulp] Finished 'scripts' in 623 ms

When I remove { outSourceMap: true }:

[gulp] Running 'scripts'...
[gulp] gulp-notify: [Gulp notification] Scripts task complete
[gulp] Finished 'scripts' in 335 ms

Any idea how I can prevent this from happening? As you can see, the build time doubles.

Extremely slow

Uglifying a file with angular.min.js, angular-material.min.js, and about another 10-15 much smaller files takes almost 5 seconds. This is ridiculously slow.

On JShint fail, Uglify throws errors.

This is my set up currently (New to Gulp so I presume it's correct):

gulp.task('scripts', function() {
    gulp.src('./src/scripts/*.js', {ignore: ['vendor']})
        .pipe(jshint( fs.readFile('./.jshintrc') ))
        .pipe(jshint.reporter('default'))
        .pipe(concat('main.js'))
        .pipe(uglify()) 
        .pipe(gulp.dest('./dist/assets/js'));
});

It all works when there are no JShint errors, however when the task is ran and there is an error in the JS, I get this error:

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error
    at new JS_Parse_Error (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:196:18)
    at js_error (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:204:11)
    at croak (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:636:9)
    at token_error (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:644:9)
    at unexpected (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:650:9)
    at expr_atom (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1131:9)
    at maybe_unary (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1287:19)
    at expr_ops (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1322:24)
    at maybe_conditional (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1327:20)
    at maybe_assign (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1351:20)

Is there any way to fix this issue? I presume it's an uglify issue as all the errors mention it.

Cheers
Mark

Cannot set correct File and Sources when writing source maps

What I'm trying to do is take in a file and output a minified version next to it with a source map.

Example:

file-debug.js
file-min.js
file-min.js.map

The problem is that no combination of rename that I use produces the correct result. The source maps "Sources" is updated to point at the output file (which is never correct). In my opinion the sources should always point at the input file (renaming further down the pipeline should not change this).

Ideas?

!+'\v1'>>>!1

!+'\v1' >>> !1

var ua = navigator.userAgent;
var c = function (r){
return r.test(ua);
},
isIE = !+'\v1',
isTrident = isIE || !!global.XDomainRequest,
isGecko = !!document.getBoxObjectFor || global.mozInnerScreenX != null,
isWebkit = !isTrident && !navigator.taintEnabled && !c(/ Firefox/);

Uglify stream mutates its input

gulp-uglify mutates its input, which means means that changes propagate backwards in the stream pipeline. This issue is analogous to this bug in gulp-rename
Consider following code:

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

var scripts = gulp.src([
    'src/script.js'
]);

var minified = scripts
.pipe(uglify())
.pipe(addFileSuffix('.min'));

mergeStreams(scripts, minified)
.pipe(gulp.dest('build'));

mergeStreams is a function that pipes the sources into one destination stream and closes it when all sources end, and addFileSuffix just returns a stream that adds a suffix to file base names.

This pipeline should write two files: build/script.js - an unchanged copy of source file, and build/script.min.js - a minified source file. But instead, it writes minified output to both destination files.

If gulp-uglify made a shallow copy of the input file and didn't mutate the input file itself, this problem wouldn't have occurred.

1.1.0 unhandled error

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error
    at new JS_Parse_Error (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:189:18)
    at js_error (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:197:11)
    at croak (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:657:9)
    at token_error (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:665:9)
    at unexpected (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:671:9)
    at semicolon (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:691:43)
    at simple_statement (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:865:73)
    at node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:724:47
    at node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:704:24
    at block_ (node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:978:20)

Enclose Option to Uglify

Is there a way to pass the enclose option into UglifyJS2? I would like my outputted file to be wrapped in a function to prevent polluting the global namespace.

If this is something an additional plugin solves, what plugin would that be? I thought maybe "concat" and just do the wrapping myself but that seems a little hacky.

Compress: false, does not work

Hi,

Gulp-uglify I use to build my webapp. For the development environment, I need to uglify not compress the JS file, so use the compress option: false, and this option does not work. I tried to use all options UglifyJS to false, and neither works.

What happen?

Here's the code:

gulp.task ('compress', function () {
   gulp.src ('app / scripts / ** / *. js')
     .pipe (uglify ({
          preserveComments: 'some',
          mangle: false,
          compress: false,
          beautify: true
     }))
     .pipe (gulp.dest (dist / scripts'))
});

gulp-uglify shouldn't also compress

@jonathanepollack: I appreciate the fork to add support for options, I've had similar written locally, and just been too busy to finish it up and commit myself.

It's recommended, however, that gulp plugins only do one thing, in this case, mangle input JavaScript. Therefore, I ask that you remove support for the compress option from your fork before you make a pull request.

Name expected?

I implemented this:

.pipe(uglify().on('error', function(e) { console.log('\x07',e.message); return this.end(); }))

to get more describing error messages.
But now I get:
Name expected

My task is as simple as this:

gulp.task('orders_js', function () {
    gulp.src(orders_js) // Javascript files source defined else where as an array (top of file)
//      .pipe(uglify())
        .pipe(uglify().on('error', function(e) { console.log('\x07',e.message); return this.end(); }))
        .pipe(gulp.dest('assets/js/pages'));
});

Everything works fine when out commenting the uglify line. As in, the file does output to the right place, so path etc. is all working. But what "Name" is it that uglify "Expects"?

Task does not finish

On my (windows) machine the task starts and uglifies as expected but never finishes. Thus, when using gulp.watch, it runs only once.

Allow piping to the same directory with a different filename

e.g for build.js I simply want to ceate a build.min.js sitting next to it...

gulp.src('./build/build.js')
    .pipe(uglify({
        newFile: 'build.min.js'
     }))
    .pipe(gulp.dest('./build')

Would love to submit a PR if this seems an acceptable option.

Uglify throws Parse error

this is the error

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error
    at new JS_Parse_Error (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:189:18)
    at js_error (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:197:11)
    at croak (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:656:9)
    at token_error (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:664:9)
    at expect_token (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:677:9)
    at expect (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:680:36)
    at /home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1222:13
    at /home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:703:24
    at expr_atom (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1152:35)
    at maybe_unary (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1327:19)
    at expr_ops (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1362:24)
    at maybe_conditional (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1367:20)
    at maybe_assign (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1391:20)
    at expression (/home/rkmax/my-project/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1410:20)

this is my scripts task

gulp.task('scripts', function() {

    var bundler = browserify({
        entries: ['./src/scripts/main.js'],
        debug: debug
    }).transform(stringify()); // the error persist even without this transformation

    bundler
        .bundle()
        .on('error', handleErrors)
        .pipe(source(getBundleName() + '.js'))
        .pipe(jshint())
        .pipe(jshint.reporter('default', { verbose: true }))
        .pipe(jshint.reporter('fail'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(uglify())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./web/js'));
});
```js

soucemaps on browserify output only work with compress = false

I've been pulling my hair out with this one, but it would seem that the uglifyjs setting compress influences whether sourcemaps are maintained when using gulp-uglify.

The only defining factor in my setup that i can see is the fact that the js input for uglify is coming from a browserify step further up my gulp task list (yes i know browserify has its own options for sourcemaps but i can't seem to get any of them to work effectively).

I've isolated the problem to the following:

this works

return gulp.src('./bin/js/app.js')
        .pipe(sourcemaps.init({loadMaps:true}))
        .pipe(uglify({compress:false}))
        .pipe(sourcemaps.write('./',{sourceRoot:'./'}))
        .pipe(gulp.dest('./bin/js/'));

this doesn't work

return gulp.src('./bin/js/app.js')
        .pipe(sourcemaps.init({loadMaps:true}))
        .pipe(uglify())
        .pipe(sourcemaps.write('./',{sourceRoot:'./'}))
        .pipe(gulp.dest('./bin/js/'));

in the second setup, the sourcemaps appear to work but when i set breakpoints and step through the code, everything after the main entry point source file is mapped incorrectly

Whats surprising is the lack of effectiveness of the compress option - leaving it on only knocks about 10k off a 580k file with it off so I'm happy to leave it like this, but was wondering if anyone else had experienced similar issues

Adds "sourceMappingURL" comment when used with "gulp-sourcemaps"

UglifyJS adds a source mapping comment to the mangled source. When used with gulp-sourcemaps, gulp-uglify should remove this comment. Otherwise there are two //# sourceMappingURL comments at the end.

Example output for a file named main.min.js:

function onLoad(){var o="hello",l="world";console.log(o+l)}
//# sourceMappingURL=main.min.js
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJza[...]

can not preserve comments rightly

such as:

gulp.task('script',function(){
   gulp.src('js/**/*.js')
     .pipe(uglify({
       preserveComments:'some'
     }))
     .pipe(gulp.dest(PATH.js));
})
/* origin:*/
/*!
  do not delete
*/
var a = String(function(){
    /*!
    <div class="foo">{{bar}}</div>
    */
    }).replace(/^[^\{]*\{\s*\/\*!?|\*\/[;|\s]*\}$/g, '');

output:

var a = String(function(){}).replace(/^[^\{]*\{\s*\/\*!?|\*\/[;|\s]*\}$/g, '');

Comments with a bang(!) or @preserve, @license, @cc_on,should not be deleted,as README said.

Source maps: wrong mapping to sources

Uglify seems to change the order of the sources in the source map. Since gulp-uglify replaces the sources and sourcesContent properties with the original ones at the and, the mapping to the source files is not correct anymore.

Project for reproducing the bug:
https://github.com/floridoo/gulp-sourcemap-test/tree/gulp-uglify-bug

Possible resolutions:

  • reorder the sourcesContent according to the sources output of Uglify
  • or use vinyl-sourcemaps-apply instead of relying on Uglify to combine the source maps

it did not throw detail information when error occurs

when js has syntax error, this plugin output nothing but error stack.

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error
    at new JS_Parse_Error (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:189:18)
    at js_error (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:197:11)
    at croak (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:656:9)
    at token_error (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:664:9)
    at unexpected (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:670:9)
    at /Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:754:17
    at /Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:703:24
    at block_ (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:977:20)
    at ctor.body (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:950:25)
    at function_ (/Users/dongming/dev/gitlab/wrench/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:955:15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.