【问题标题】:Display javascript error when using gulp使用 gulp 时显示 javascript 错误
【发布时间】:2014-10-31 09:36:51
【问题描述】:

大家好,我正在使用 gulp uglify 和 concat 来缩小 js 代码。

但是,我想有一种方法来检测原始 dev js 代码中的任何编码错误,以便我可以检查原始代码,而不仅仅是在缩小后通知。

我可以知道我该怎么做吗?

下面是我的 gulp 代码。

gulp.task('frontend.js', function() {  
  return gulp.src([paths.dev.js + 'jquery.js', paths.dev.js + 'bootstrap.js', paths.dev.js + 'basic.js'])
    .pipe(jsconcat('script.js'))
    .pipe(uglify())
    .pipe(gulp.dest(paths.assets.js))  // output: script.js
    .pipe( notify({message: 'frontend.js converted'}));   
});

【问题讨论】:

    标签: javascript node.js gulp gulp-concat gulp-uglify


    【解决方案1】:

    这就是源地图的用途。

    var sourcemaps = require('gulp-sourcemaps');
    
    gulp.task('frontend.js', function() {  
      return gulp.src([paths.dev.js + 'jquery.js', paths.dev.js + 'bootstrap.js', paths.dev.js + 'basic.js'])
        .pipe(jsconcat('script.js'))
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(paths.assets.js))  // output: script.js
        .pipe( notify({message: 'frontend.js converted'}));   
    });
    

    它将源映射(即将每个缩小的行映射到原始行)附加到frontend.js

    现在,如果您使用 Chrome 或 Firefox 等现代浏览器,您将看到原始代码。

    【讨论】:

      【解决方案2】:

      您的意思是希望在连接和缩小源文件之前收到有关 javascript 错误的通知,或者您想要一种能够将缩小文件上的运行时错误与包含违规代码的原始源文件相匹配的方法?

      在后一种情况下,如果完美,则可以回答,否则您应该在 jsconcat 之前通过管道调用 gulp-jshint,可能使用 jshint-stylish 作为 jshint 记者。

      【讨论】:

        猜你喜欢
        • 2016-06-30
        • 2015-05-31
        • 2011-05-07
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        相关资源
        最近更新 更多