gulpbrowserifybabeljsbabelify

Another ParseError: 'import' and 'export' may appear only with 'sourceType: module' :(


I have spent days scouring the internet and SO for a solution to this issue (please don't mark this as duplicate!). I am trying to use ES6 imports:

import * as _ from 'underscore';        <--- works
const test = _.clone({'2':1});          <--- works

import Bulma from '@vizuaalog/bulmajs'; <--- fails

My gulp task fails to complile at the 'import from bulmajs' line; I get the dreaded "ParseError: 'import' and 'export' may appear only with 'sourceType: module'" :(

Here's the gulp task:

browserify('./src/js/index.js', {debug: true})
    .transform(babelify.configure())
    .bundle().on('error', function (err) {
    console.log(err);                   <--- error thrown here 
})
.pipe(source('index.js')) // Readable Stream -> Stream Of Vinyl Files
.pipe(buffer()) // Vinyl Files -> Buffered Vinyl Files
.pipe(sourcemaps.init().on('error', function (e) {
    console.log(e);
}))
.pipe(uglify().on('error', function (e) {
    console.log('here')
    console.log(e);
}))
.pipe(rename({extname: '.min.js'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(./prod/js'));

I'm keen to keep using babel v7, but can't see what's wrong with my task/package.json (especially as the underscore import works).

Here's my package.json:

"dependencies": {
   "@vizuaalog/bulmajs": "^0.7.0",
   "bulma": "^0.7.2",
   "underscore": "^1.9.1"
 }
 "devDependencies": {
   "@babel/core": "^7.1.2",
   "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
   "@babel/preset-env": "^7.1.0",
   "autoprefixer": "^9.2.1",
   "babel-eslint": "^10.0.1",
   "babelify": "^10.0.0",
   "browserify": "^16.2.3",
   "browserify-css": "^0.14.0",
   "browserify-shim": "^3.8.14",
   "eslint": "^5.7.0",
   "fancy-log": "^1.3.2",
   "gulp": "^3.9.1",
   "gulp-autoprefixer": "^6.0.0",
   "gulp-clean-css": "^3.10.0",
   "gulp-csso": "^3.0.1",
   "gulp-imagemin": "^4.1.0",
   "gulp-newer": "^1.4.0",
   "gulp-rename": "^1.4.0",
   "gulp-sass": "^4.0.1",
   "gulp-sourcemaps": "^2.6.4",
   "gulp-uglify": "^3.0.1",
   "gulplog": "^1.0.0",
   "node-sass": "^4.9.3",
   "path": "^0.12.7",
   "run-sequence": "^2.2.1",
   "vinyl-buffer": "^1.0.1",
   "vinyl-source-stream": "^2.0.0"
 },
 "browserify": {
 "debug": true,
 "transform": [
   [
     "browserify-css",
     {
       "autoInject": true,
       "minify": true
     }
   ],
   [
     "babelify",
     {
       "presets": [
         [
           "@babel/preset-env",
           {
             "modules": "commonjs",
             "targets": {
               "browsers": [
                 "last 5 versions",
                 "safari >= 7"
               ]
             }
           }
         ]
       ],
       "plugins": [
         "@babel/plugin-proposal-object-rest-spread"
       ],
       "sourceMaps": true
     }
   ]
 ]
}

Any help would be greatly appreciated, I feel like I've read every relevant thread/SO post around this and still haven't found a solution.


Solution

  • Babelify does not process any node_modules by default because there's usually tens of thousands of files in there to process. Because the source of bulmajs is written in es6-dependent javascript (not the case with underscore!), it also needs to be babel-ified. Your gulp task should include something like this:

    browserify('./src/js/index.js', {debug: true})
        .transform(babelify, {
            global: true,                                     // babelify EVERYTHING!!
            ignore: [/\/node_modules\/(?!@vizuaalog\/)/],     // just kidding, only babelify the one module we need
            presets: ["@babel/preset-env"]
        })
        .bundle().on('error', function (err) {
        console.log(err);
    })
    

    Here's the repository I created while debugging this issue in case it's of interest to anyone (don't expect anything exciting) https://github.com/Hypaethral/bulmajs-browserify-mvp-example