Gulp + browserify + 6to5 +源地图

时间:2015-01-22 11:29:49

标签: gulp browserify source-maps babeljs

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

所以:  1.我使用ES6语法编写模块。  2. 6to5将这些模块转换为CommonJS(或其他)语法。  3. Browserify捆绑模块。  4.源地图返回原始的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
};

模块/ main.js

import {
    bar
}
from './B';

bar();

代码似乎正在运行,但它并没有缩小,而源地图是内联的(实际上并不适用于生产)。

2 个答案:

答案 0 :(得分:46)

以此为出发点:

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'));
});

答案 1 :(得分:3)

我不明白为什么我们必须使用某些东西才能让它工作,所以我在这里添加我自己的答案。对于那些寻找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这就是我们缓冲源流的原因。

对于那些不熟悉源图的人来说,它们本质上是一种将您的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