I am using Gulp, Browserify and Babelify to compile my source code from ES6 + JSX to plain Javascript.
My gulpfile.js
looks like:
var gulp = require('gulp')
var fs = require("fs")
var browserify = require("browserify")
var babelify = require("babelify")
gulp.task('es6',function(){
return browserify({ debug: true })
.transform(babelify.configure({
extensions: ['.es6']
}))
.require("./src/es6/app.es6", { entry: true })
.bundle()
.on("error", function (err) { console.log("Error : " + err.message); })
.pipe(fs.createWriteStream("./build/js/bundle.js"))
})
The first lines of my app.es6
is:
"use strict"
import React from 'react'
import MenuBar from './menu'
When I run $ gulp es6
, I get the following error:
Error : 'import' and 'export' may appear only with 'sourceType: module'
However this does not happen when I use Browserify's cli:
$ browserify src/es6/app.es6 -t babelify -o build/js/bundle.js --extension=.es6
Any thoughts on how this problem can be resolved?
The extensions:['.es6']
option needs to be added to browserify's configuration:
browserify({ debug: true, extensions: ['.js', '.json', '.es6'] })
The entire gulpfile
should look like this:
var gulp = require('gulp')
var fs = require("fs")
var browserify = require("browserify")
var babelify = require("babelify")
gulp.task('es6',function(){
var extensions = ['.js','.json','.es6'];
return browserify({ debug: true, extensions:extensions })
.transform(babelify.configure({
extensions: extensions
}))
.require("./src/es6/app.es6", { entry: true })
.bundle()
.on("error", function (err) { console.log("Error : " + err.message); })
.pipe(fs.createWriteStream("./build/js/bundle.js"))
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With