Browserify - ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module [英] Browserify - ParseError: 'import' and 'export' may appear only with 'sourceType: module
问题描述
在我的 gulpfile 中有
In my gulpfile I have
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var babel = require("gulp-babel");
var rename = require('gulp-rename');
var source = require('vinyl-source-stream');
var browserify = require('gulp-browserify');
var notify = require("gulp-notify");
gulp.task('js', function () {
gulp.src('js/main.js')
.pipe(babel())
.pipe(browserify())
.on('error', errorAlert)
.pipe(rename('./dist/js/bundle.js'))
//.pipe(uglify())
.pipe(gulp.dest('./'))
.pipe(notify({title: "Success", message: "Well Done!", sound: "Glass"}));
})
在我的 app.js 中,我尝试导入但出现错误
and in my app.js I am trying to import but get the errror
import SimpleBreakpoints from 'simple-breakpoints'
知道如何摆脱错误并使用导入语法吗?
Any idea how to get rid of the error and use the import syntax?
.babelrc
{
"presets": ["es2015"],
}
推荐答案
在你的配置中,你通过管道将 js/main.js
传送到 Babel,所以这是唯一将被转译的文件.当 Browserify 需要 app.js
时,它会看到 ES6 内容并影响您看到的错误.
In your configuration, you pipe js/main.js
to Babel, so that's the only file that will be transpiled. When Browserify requires app.js
, it will seen ES6 content and will effect the error you are seeing.
你可以使用 Babelify 来解决这个问题.这是一个 Browserify 转换,它将转换 Browserify 接收到的源.
You could use Babelify to solve the problem. It's a Browserify transform that will transpile the source that Browserify receives.
要安装它,请运行以下命令:
To install it, run this command:
npm install babelify --save-dev
要对其进行配置,请将您的任务更改为:
And to configure it, change your task to:
gulp.task('js', function () {
gulp.src('js/main.js')
.pipe(browserify({ transform: ['babelify'] }))
.on('error', errorAlert)
.pipe(rename('./dist/js/bundle.js'))
//.pipe(uglify())
.pipe(gulp.dest('./'))
.pipe(notify({ title: "Success", message: "Well Done!", sound: "Glass" }));
})
这篇关于Browserify - ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!