GulpUglifyError:无法缩小JavaScript

时间:2017-03-08 18:12:46

标签: javascript gulp

我收到这个让我疯狂的错误:

events.js:154
      throw er; // Unhandled 'error' event
      ^
GulpUglifyError: unable to minify JavaScript
    at createError (/Users/berretan/Documents/Projects/promo-brochure/node_modules/gulp-uglify/lib/create-error.js:6:14)
    at wrapper (/Users/berretan/Documents/Projects/promo-brochure/node_modules/gulp-uglify/node_modules/lodash/_createHybrid.js:87:15)
    at trycatch (/Users/berretan/Documents/Projects/promo-brochure/node_modules/gulp-uglify/minifier.js:26:12)
    at DestroyableTransform.minify [as _transform] (/Users/berretan/Documents/Projects/promo-brochure/node_modules/gulp-uglify/minifier.js:76:19)
    at DestroyableTransform.Transform._read (/Users/berretan/Documents/Projects/promo-brochure/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/Users/berretan/Documents/Projects/promo-brochure/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at doWrite (/Users/berretan/Documents/Projects/promo-brochure/node_modules/readable-stream/lib/_stream_writable.js:347:64)
    at writeOrBuffer (/Users/berretan/Documents/Projects/promo-brochure/node_modules/readable-stream/lib/_stream_writable.js:336:5)
    at DestroyableTransform.Writable.write (/Users/berretan/Documents/Projects/promo-brochure/node_modules/readable-stream/lib/_stream_writable.js:274:11)
    at DestroyableTransform.ondata (/Users/berretan/Documents/Projects/promo-brochure/node_modules/readable-stream/lib/_stream_readable.js:546:20)

我不知道错误的来源。但看看我的文件夹结构:

enter image description here

现在:我有这4个文件:

<script src="assets/scripts/vendor/siteconfig.js"></script>
<script src="assets/scripts/vendor/responsive-tags.js"></script>
<script src="assets/scripts/vendor/tealium-video.js"></script>
<script src="assets/scripts/vendor/tealium.js"></script>

这些html引用块:

enter image description here

正如您所看到的,有2个参考块。如果我将这4个文件放在这些块中,当我运行gulp build时一切顺利,但在实时环境中,我在这些文件上获得404。但是,如果我将这些文件放在这两个引用块中,我会得到上面提到的错误。

这是我的gulp congif的一部分:

//Require specific modules
var gulp = require('gulp');
var del = require('del');
var get = require('get-value');
var $ = require('gulp-load-plugins')();
var multimatch = require('multimatch');
var bs = require("browser-sync").create();
var assemble = require('assemble');
var app = assemble();
var guppy = require('git-guppy')(gulp);
var gutil = require('gulp-util');
var watch = require('gulp-watch');
var helpers = require('handlebars-helpers')();
var runSequence = require('run-sequence');

gulp.task('captions:dist', function() {
   gulp.src('app/assets/captions/**/*')
   .pipe(gulp.dest('dist/assets/captions'));
});

//Copy Task for assets that needs to be moved to /dist on build task
gulp.task('copy:vendorjs', ['clean:dist'], function(){
  return gulp.src(['app/assets/scripts/vendor/jquery-2.1.4.min.js'])
      .pipe(gulp.dest('dist/assets/scripts/vendor'));
});

//Lint task
gulp.task('eslint', function() {
  return gulp.src(['app/{assets,components,pages}/**/*.js', '!app/assets/scripts/vendor/**'])
    .pipe($.eslint())
    .pipe($.eslint.format())
    .pipe($.eslint.failAfterError());
});

gulp.task('pre-commit', function () {

  var filterVendorJs = ['app/**/*.js', '!app/assets/scripts/vendor/*'];
  var filesToTest = multimatch(guppy.src('pre-commit'), filterVendorJs);

  return gulp.src(filesToTest)
    .pipe($.plumber())
    .pipe($.eslint({ rulePaths: ['/'], configFile: '.eslintrc'}))
    .pipe($.eslint.format())
    .pipe($.eslint.failAfterError());
});

//Assemble load task
gulp.task('load', function(cb) {

  //Set main assemble options
  app.option('layout', 'default');
  app.layouts('app/layouts/*.hbs');
  app.pages('app/pages/**/*.hbs');
  app.partials('app/components/**/*.hbs');
  app.engine('hbs', require('engine-handlebars'));
  app.data(['app/{pages,components,data}/**/*.json']);

  cb();
});

//Assemble main task
gulp.task('assemble', ['load'], function(){
    return app.toStream('pages')
      .pipe(app.renderFile())
      .pipe($.extname())
      .pipe($.flatten())
      .pipe($.plumber())
      .pipe(app.dest('.tmp'));
});

gulp.task('assemble:dist', ['load', 'clean:dist'], function(){
    return app.toStream('pages')
      .pipe(app.renderFile())
      .pipe($.extname())
      .pipe($.flatten())
      .pipe($.plumber())
      .pipe(app.dest('dist'));
});

gulp.task('usemin', ['assemble:dist', 'sass:dist'], function() {
  return gulp.src('dist/**/*.html')
    .pipe($.foreach(function (stream) {
      return stream
        .pipe($.plumber())
        .pipe($.usemin({
          cssvendor: [$.cssnano()],
          css: [$.cssnano()],
          jsvendor: [$.uglify()],
          js: [$.uglify()]
        }))
        .pipe(gulp.dest('dist/'));
    }));

});

//Serve the site from .tmp directory
gulp.task('default', ['assemble', 'sass', 'fonts', 'notify:server'], function(){

    var globs = [
      '.tmp/**/*.html',
      '.tmp/assets/styles/{,*/}*.css',
      '.tmp/assets/**/*.js',
      'app/assets/images/{,*/}*.*'
    ];
});


//Serve the site from the dist directory
gulp.task('default:dist', function(){
  bs.init({
    notify: false,
    port: 9000,
    server: {
        baseDir: ['dist']
    }
  });
});

gulp.task('build', ['sass:dist', 'usemin', 'images', 'fonts:dist', 'copy:vendorjs','captions:dist'], function(){
});

任何建议?

1 个答案:

答案 0 :(得分:-1)

您可能在javascript文件中存在语法错误。例如,请使用jslint.com进行检查。