GulpUglifyError:Unable to minify JavaScript

前端 未结 8 1827
感情败类
感情败类 2021-01-31 02:06

I am trying to minify my script files for which i am using gulp task runner And I am trying gulp-uglify plugin

Code:

 gulp.task(\'concat\', function() {         


        
相关标签:
8条回答
  • 2021-01-31 02:10

    To see the error in console:

    var gutil = require('gulp-util');
    
    gulp.task('concat', function() {
    return gulp.src('app/**/*.js')
        // .pipe(concat('script.js'))
        .pipe(uglify())
        .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
        .pipe(gulp.dest('./dist/'))
    });
    

    To find the exact file, with line number of error register and run this task:

    var pump = require('pump');
    
    gulp.task('uglify-error-debugging', function (cb) {
      pump([
        gulp.src('app/**/*.js'),
        uglify(),
        gulp.dest('./dist/')
      ], cb);
    });
    
    0 讨论(0)
  • 2021-01-31 02:10

    Have you used ES6 format in your script file? If so try ES5 now because when you do gulp-uglify it doesnt understand ES6 format as of now and after that try your code

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

    and run the task gulp concat it will work

    0 讨论(0)
  • 2021-01-31 02:17

    you may have syntax error or used ES6 syntax. you can try https://skalman.github.io/UglifyJS-online/ firstly.

    0 讨论(0)
  • 2021-01-31 02:19

    For me, it was a deprecated option "preserveComments" that generated the error (and completely crashed the script).

    Found the issue using:

    gulp.task('concat', function() {
    return gulp.src('app/**/*.js')
        .pipe(uglify())
        .on('error', function (err) { console.log( err ) })
        .pipe(gulp.dest('./dist/'))
    });
    
    0 讨论(0)
  • 2021-01-31 02:22

    The main error to Unable to minifies JavaScript is the path not found. You can use the task usemin For this you need:

    $ sudo npm install gulp-usemin --save-dev
    $ sudo npm install gulp-livereload --save-dev
    $ sudo npm install gulp-util --save-dev
    

    and requires :

    var usemin = require('gulp-usemin');
    var livereload = require('gulp-livereload');
    var gutil = require('gulp-util'); //gutil for error display
    
    gulp.task('usemin',['jshint'], function(){
        return gulp.src('./app/index.html')
        .pipe(usemin({
            css: [minifycss(),rev()],
            scripts: [uglify().on('error', function(err) {gutil.log(gutil.colors.red('[Error]'), err.toString());this.emit('end');}),rev()]
        })) 
        .pipe(gulp.dest('dist'))
        .pipe(livereload());
    });
    
    Change the js: [uglify(), rev()] to scripts: [uglify(), rev()]
    
    0 讨论(0)
  • 2021-01-31 02:27

    The main error is generated when you're using ES6 format. Use the gulp-uglify-es module instead of 'gulp-uglify' to overcome this error.

    var uglify = require('gulp-uglify-es').default;
    

    Note: gulp-uglify-es is no longer being maintained. You may want to use terser/gulp-terser instead:

    0 讨论(0)
提交回复
热议问题