Created
January 5, 2014 00:08
-
-
Save ryanseddon/8262519 to your computer and use it in GitHub Desktop.
Gulp + browserify conundrum. How can I get browserify to properly transform the es6 module syntax to CommonJS syntax and pass along the stream so browserify can figure out how to bundle up the file into a single dist release? Is this possible?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var gulp = require('gulp'); | |
| var es6ModuleTranspiler = require('gulp-es6-module-transpiler'); | |
| var browserify = require('gulp-browserify'); | |
| var through = require('through'); | |
| function transpileModuleSyntax(file) { | |
| var data = ''; | |
| return through(write, end); | |
| function write (buf) { data += buf } | |
| function end () { | |
| this.queue(es6ModuleTranspiler({type: 'cjs'})); | |
| this.queue(null); | |
| } | |
| } | |
| gulp.task('build', function() { | |
| // Transpile ES6 modules | |
| // browser friendly | |
| gulp.src('src/*.js') | |
| .pipe(browserify({ | |
| transform: [transpileModuleSyntax], | |
| debug: true | |
| })) | |
| .pipe(gulp.dest('./dist')); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is the entry point that browserify should figure out | |
| // ES6 module syntax that the transform method should take care of | |
| import { support, dispatchClick } from 'utils'; | |
| if(support) { | |
| dispatchClick(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var support = (function support() { | |
| return true; | |
| }()); | |
| function dispatchClick(coords){ | |
| // Create synthetic click event | |
| } | |
| export { support, dispatchClick }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As far as I understand, there are a couple of problems in your solution:
es6ModuleTranspiler()returnsthrough streamwhich reads/writes with vinyl File object, while browserify's transform should return a stream that takes the raw file contents.with
gulp.src('src/*.js').pipe(browserify(...))you are trying to transform all js-files insrcdirectory. But browserify will automatically process thererequires, inserting it's content. So your final bundle will consist a lot of duplicate code.You should
pipeonly single entry point of your app.This works for me: