Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babelify sourceType error when used programmatically

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?

like image 396
Ben Avatar asked May 15 '15 01:05

Ben


1 Answers

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"))
})
like image 131
Ben Avatar answered Sep 22 '22 07:09

Ben