【问题标题】:Gulp + browserify + 6to5 + source mapsGulp + browserify + 6to5 + source maps
【发布时间】:2015-01-22 11:29:49
【问题描述】:

我正在尝试编写一个 gulp 任务,允许我在 JS 中使用模块(CommonJS 很好),使用 browserify + 6to5。我还希望源映射能够工作。

所以: 1. 我使用 ES6 语法编写模块。 2. 6to5 将这些模块转换为 CommonJS(或其他)语法。 3. Browserify 捆绑模块。 4. Source maps 引用原始 ES6 文件。

这样的任务怎么写?

编辑:这是我目前所拥有的:

gulp 任务

gulp.task('browserify', function() {
    var source = require('vinyl-source-stream');
    var browserify = require('browserify');
    var to5ify = require('6to5ify');

    browserify({
        debug: true
    })
    .transform(to5ify)
    .require('./app/webroot/js/modules/main.js', {
        entry: true
    })
    .bundle()
    .on('error', function(err) {
        console.log('Error: ' + err.message);
    })
    .pipe(source('bundle.js'))
    .pipe(gulp.dest(destJs));
});

模块/A.js

function foo() {
    console.log('Hello World');

    let x = 10;

    console.log('x is', x);
}

export {
    foo
};

模块/B.js

import {
    foo
}
from './A';

function bar() {
    foo();
}

export {
    bar
};

modules/main.js

import {
    bar
}
from './B';

bar();

代码似乎可以工作,但它没有被缩小,并且源映射是内联的(这实际上不适用于生产)。

【问题讨论】:

    标签: gulp browserify source-maps babeljs


    【解决方案1】:

    以此为起点:

    var gulp = require('gulp');
    var gutil = require('gulp-util');
    var sourcemaps = require('gulp-sourcemaps');
    var source = require('vinyl-source-stream');
    var buffer = require('vinyl-buffer');
    var browserify = require('browserify');
    var to5ify = require('6to5ify');
    var uglify = require('gulp-uglify');
    
    gulp.task('default', function() {
      browserify('./src/index.js', { debug: true })
        .transform(to5ify)
        .bundle()
        .on('error', gutil.log.bind(gutil, 'Browserify Error'))
        .pipe(source('bundle.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
        .pipe(uglify())
        .pipe(sourcemaps.write('./')) // writes .map file
        .pipe(gulp.dest('./build'));
    });
    

    【讨论】:

    • 这很好用,谢谢。我注意到在 Chrome 开发工具的源映射文件中设置断点是不可能的 - 在这个阶段是不可能的吗?
    • 算了,我也在使用 uglify,它混淆了变量名。
    • @lamplightdev 是的,我刚刚添加了调整它以停止修改名称 .pipe(uglify({ mangle: false })) 并且效果很好。我还在使用.add(require.resolve('6to5/polyfill')) 进行转换之前添加了 6to5 polyfills,以获得更好的跨浏览器兼容性:) hth!
    • 有没有办法对 grunt 做同样的事情?
    • @lamplightdev 您还可以使用“gulpif”有条件地禁用 uglify 进程。例如您可以让它仅用于生产配置,例如.pipe(gulpif(config.isProduction, uglify()))
    【解决方案2】:

    我不明白为什么我们必须使用某些东西才能让它工作,所以我在这里添加我自己的答案。对于那些通过babelify 寻找解决方案的人,我在下面添加了一个。我还认为最好谈谈每行的作用。

    对于那些想在他们的 Gulpfile 中使用 ES6 的人,你可以查看 here,但如果你从 Gulp 3.9 起将文件重命名为 Gulpfile.babel.js,Gulp 支持它

    需要注意的一件大事是您需要使用vinyl-source-stream with Browserify in order to convert the output into something Gulp can understand。从那里有很多gulp plugins require vinyl buffers,这就是我们缓冲源流的原因。

    对于那些不熟悉 sourcemap 的人来说,它们本质上是一种将 minifed 捆绑文件映射到主源文件的方法。 ChromeFirefox 支持它,所以当你调试时你可以查看你的 ES6 代码以及它失败的地方。

    import gulp          from 'gulp';
    import uglify        from 'gulp-uglify';
    import sourcemaps    from 'gulp-sourcemaps';
    import source        from 'vinyl-source-stream';
    import buffer        from 'vinyl-buffer';
    import browserify    from 'browserify';
    import babel         from 'babelify';
    
    gulp.task('scripts', () => {
      let bundler = browserify({
        entries: ['./js/main.es6.js'], // main js file and files you wish to bundle
        debug: true,
        extensions: [' ', 'js', 'jsx']
      }).transform(babel.configure({
        presets: ["es2015"] //sets the preset to transpile to es2015 (you can also just define a .babelrc instead)
      }));
    
      // bundler is simply browserify with all presets set
      bundler.bundle()
        .on('error', function(err) { console.error(err); this.emit('end'); })
        .pipe(source('main.es6.js')) // main source file
        .pipe(buffer())
        .pipe(sourcemaps.init({ loadMaps: true })) // create sourcemap before running edit commands so we know which file to reference
          .pipe(uglify()) //minify file
          .pipe(rename("main-min.js")) // rename file
        .pipe(sourcemaps.write('./', {sourceRoot: './js'})) // sourcemap gets written and references wherever sourceRoot is specified to be
        .pipe(gulp.dest('./build/js'));
    });
    

    其他有用的读物​​:

    Gulp browserify the gulp-y way

    【讨论】:

    • 我正在尝试遵循您的建议,但没有成功。我的 ES6 总是转换为我不想要的 ES5。我需要你建议的同样的东西,在 ES6 中调试。你能帮我解决这个问题吗?
    猜你喜欢
    • 2015-10-02
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多