我正在尝试编写一个gulp任务,允许我在JS中使用模块(CommonJS很好),使用browserify + 6to5.我也希望源映射能够工作.
所以:1.我使用ES6语法编写模块.2. 6to5将这些模块转换为CommonJS(或其他)语法.3. Browserify捆绑模块.4.源映射返回原始ES6文件.
怎么写这样的任务?
编辑:这是我到目前为止所拥有的:
吞咽任务
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));
});
Run Code Online (Sandbox Code Playgroud)
模块/ A.js
function foo() {
console.log('Hello World');
let x = 10;
console.log('x is', x);
}
export {
foo
};
Run Code Online (Sandbox Code Playgroud)
模块/ B.js
import {
foo
}
from './A';
function bar() {
foo();
}
export { …Run Code Online (Sandbox Code Playgroud)