GulpUglifyError:Unable to minify JavaScript

前端 未结 8 1837
感情败类
感情败类 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:28

    Try using this

    var concat = require('gulp-concat');
    var uglify = require('gulp-uglify');
    var minifyJS = require('gulp-minify');
    
    gulp.task('concat', function() {
        return gulp.src('app/**/*.js')
            .pipe(minifyJS())
            .pipe(concat('bundle.min.js'))
            .pipe(uglify({ mangle: false }))
            .pipe(gulp.dest('./dist/'));
    });
    
    0 讨论(0)
  • 2021-01-31 02:33

    I think the top answers here are not explaining how to get the error. The docs have a section on error handling:

    gulp-uglify emits an 'error' event if it is unable to minify a specific file

    So, just capture the error and do whatever you want with it (such as logging to console) to see the filename, line number, and additional info:

    uglify().on('error', console.error)
    

    or in a larger context:

    gulp.task('foo', () => {
    
        return gulp.src([
            'asset/src/js/foo/*.js',
            'asset/src/js/bar/*.js',
        ])
        .pipe(uglify().on('error', console.error))
        .pipe(concat('bundle.js'))
        .pipe(gulp.dest('./'));
    
    });
    

    This gives you a super helpful error!

    { GulpUglifyError: unable to minify JavaScript
        at [stack trace])
      cause:
       { SyntaxError: Continue not inside a loop or switch
           [stack trace]
         message: 'Continue not inside a loop or switch',
         filename: 'ProductForm.js',
         line: 301,
         col: 37,
         pos: 10331 },
      plugin: 'gulp-uglify',
      fileName:
       '/asset/src/js/foo/ProductForm.js',
      showStack: false }
    
    0 讨论(0)
提交回复
热议问题