另一个 ParseError: 'import' 和 'export' 只能与 'sourceType: module' 一起使用 :(

3

我花了好几天的时间在互联网和SO上搜寻解决这个问题的方法(请不要将此标记为重复!)。我正在尝试使用ES6的导入功能:

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

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

我的 gulp 任务在 'import from bulmajs' 行无法编译; 我得到了可怕的 "ParseError: 'import' and 'export' may appear only with 'sourceType: module'" :(

这是 gulp 任务:

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'));

我很想继续使用babel v7,但是看不出我的任务/ package.json有什么问题(特别是下划线导入有效的情况下)。

这是我的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
     }
   ]
 ]
}

任何帮助都将不胜感激,我感觉自己已经阅读了所有相关的线程/SO帖子,但仍然没有找到解决方案。

问题还存在吗? - Nezih
我还没有解决它。 - Liz
1个回答

6

Babelify默认不处理任何node_modules,因为那里通常有成千上万的文件需要处理。由于bulmajs的源代码是用es6依赖的javascript编写的(underscore并非如此!),因此它也需要进行babel转换。你的gulp任务应该包括以下内容:

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);
})

以下是我在调试这个问题时创建的存储库,如果有兴趣的话可以查看(不要期望有什么激动人心的东西)https://github.com/Hypaethral/bulmajs-browserify-mvp-example


1
谢谢Hypaethral!这绝对是正确的!你真是我的救命恩人。 - Liz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接