简体   繁体   English

ES6模块+ Gulp

[英]ES6 modules + Gulp

I need to create bundle, app written with es6 modules. 我需要创建用es6模块编写的应用程序包。 Example: 例:

import Api from 'api';

function Loader(){
   // some code that user api
};

export default Loader;

Develop version converting to AMD via gulp-es6-module-transpiler and works fine with RequireJS. 通过gulp-es6-module-transpiler开发将版本转换为AMD的版本,并与RequireJS配合良好。 But i can't find way to build bundle from this. 但是我找不到从中构建捆绑包的方法。 I've try to use gulp-es6-module-transpiler and gulp-browserify: 我尝试使用gulp-es6-module-transpiler和gulp-browserify:

gulp.task('build_js', function() {
   gulp.src('app/**/*.jsx')
    .pipe(gulp_react({errLogToConsole: true}))
    .pipe(transpiler({
        type: 'cjs'
    }))
    .pipe(gulp.dest('./' + build_fld + '/'));
});

gulp.task('brow', ['build_js'] function(){
   gulp.src('dist/auto-name.js')
    .pipe(browserify())
    .pipe(gulp.dest('./' + build_fld + '/'));
});

But i have error from Browserify: 但是我有来自Browserify的错误:

Error: module 'name' not found from 'd:\\Work\\lib.react-suggest\\dist\\fake_32525252.js';

But generally it's bad way, because as I understand browserify not suport vinyl streams, and it need first build js files with CommonJS modules, and then user Browserify. 但是通常这是一种不好的方法,因为据我了解,browserify不能支持乙烯基流,它首先需要使用CommonJS模块构建js文件,然后再使用Browserify。

Maybe I understand something wrong, and it can be build from one stream? 也许我理解错了,并且可以从一个流中构建它? What other solutions are possible? 还有哪些其他解决方案? As result i want AMD for develop and CommonJS or Global for production. 结果,我希望使用AMD进行开发,使用CommonJS或Global进行生产。

App file stucture what i have: 应用程序文件结构我所拥有的:

/app
   -app.jsx
   -component1.jsx
   -component2.jsx
   -index.html
/.tmp
/dist
gulpfile.js

At result i want: 结果我想要:

/app
   -app.jsx
   -component1.jsx
   -component2.jsx
   -index.html
/.tmp
  -app.js
  -component1.js
  -component2.js
  -index.html
/dist
  -app.min.js
gulpfile.js

From what I know, Browserify does accept stream in the add/require method. 据我所知,Browserify确实在add / require方法中接受流。 I am not sure if gulp call produces true stream. 我不确定gulp调用是否会产生真正的流。 However frankly why would use gulp in there when you can call browserify api directly to produce result? 但是坦率地说,当您可以直接调用browserify api产生结果时,为什么要在其中使用gulp?

var b = browserify();
b.add('dist/auto-name.js');
b.bundle().pipe(gulp.dest('./' + build_fld + '/'));

Unless of course you are doing some other magic with Gulp you haven't shown here. 除非您当然正在用Gulp做其他魔术,否则这里没有显示。 In that case good luck :) 在那种情况下,祝你好运:)

You can use 0.2.0 version of gulp-es6-module-transpiler . 您可以使用gulp-es6-module-transpiler的 0.2.0版本。 It supports bundle and commonjs formatters, so building a bundle should not be a problem. 它支持bundlecommonjs格式化程序,因此构建bundle应该不是问题。 As for AMD, you can use es6-module-transpiler-amd-formatter module. 至于AMD,您可以使用es6-module-transpiler-amd-formatter模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM