[英]Browserify - ParseError: 'import' and 'export' may appear only with 'sourceType: module
In my gulpfile I have在我的 gulpfile 中,我有
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"}));
})
and in my app.js I am trying to import but get the errror在我的 app.js 我试图导入但得到错误
import SimpleBreakpoints from 'simple-breakpoints'
Any idea how to get rid of the error and use the import syntax?知道如何摆脱错误并使用导入语法吗?
Edit: the .babelrc编辑:.babelrc
{
"presets": ["es2015"],
}
In your configuration, you pipe js/main.js
to Babel, so that's the only file that will be transpiled.在您的配置中,您通过管道将
js/main.js
到 Babel,因此这是将被转译的唯一文件。 When Browserify requires app.js
, it will seen ES6 content and will effect the error you are seeing.当 Browserify 需要
app.js
,它会看到 ES6 内容并且会影响你看到的错误。
You could use Babelify to solve the problem.你可以使用 Babelify 来解决这个问题。 It's a Browserify transform that will transpile the source that Browserify receives.
这是一个 Browserify 转换,它将转换 Browserify 接收的源。
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" }));
})
For those who work with gulp
and want to transpile ES6 to ES5 with browserify
, you might stumble upon gulp-browserify plug-in.对于那些谁一起工作
gulp
,并希望transpile ES6到ES5与browserify
,你可能会绊倒后一饮而尽,browserify插件。 Warning as it is from version 0.5.1 gulp-browserify
is no longer suported!!!警告,因为它是从0.5.1
gulp-browserify
不再支载! . . Consequences, of this action and transpiling with
gulp-browserify
will result with source code that might produce errors such as the one in question or similar to these: Uncaught ReferenceError: require is not defined or Uncaught SyntaxError: Unexpected identifier next to your import statements eg import * from './modules/bar.es6.js';
此操作的后果以及使用 gulp
gulp-browserify
browserify 进行转译将导致源代码可能产生错误,例如有问题的或类似的错误:未捕获的 ReferenceError:未定义或未捕获的 SyntaxError:导入语句旁边的意外标识符,例如import * from './modules/bar.es6.js';
Althoutg gulp-browserify
recomends to "checkout the recipes by gulp team for reference on using browserify with gulp" . Althoutg gulp
gulp-browserify
browserify 建议“查看 gulp团队的食谱,以供参考使用 browserify 和 gulp” 。 I found this advice to no avail.我发现这个建议没有用。 As it is now (2st July 2019) solution that worked for me was to replace
gulp-browserify
with gulp-bro@1.0.3 plug-in.因为它现在是为我工作(2ST 2019年7月)的解决办法是更换
gulp-browserify
与gulp-bro@1.0.3插件。 This successfully, transpired ES6 to ES5 (as it is now) - It might change in future since support for JavaSript libraries decays with time of it appearance.这成功地将 ES6 转化为 ES5(就像现在一样)——它可能会在未来发生变化,因为对 JavaSript 库的支持会随着它的出现而衰减。
Assumption: To reproduce this solution you should have installed docker .假设:要重现此解决方案,您应该已安装docker 。 Beside that you should be familiar with babel and babelify .
除此之外,您应该熟悉babel和babelify 。
This solution was successfully reproduced in docker environment, run node:11.7.0-alpine image.此方案在docker环境下成功复现,运行node:11.7.0-alpine image。
/src <- directory
/src/app/foo.es6.js
/src/app/modules/bar.es6.js
/src/app/dist <- directory
/src/app/dist/app.es5.js
/src/gulpfile.js
/src/.babelrc
/src/package.json
/src/node_modules <- directory
$ docker run --rm -it --name bro_demo node:11.7.0-alpine ash
$ mkdir -p /src/dist
$ mkdir -p /src/app/modules/
$ touch /src/app/foo.es6.js
$ touch /src/app/modules/bar.es6.js
$ touch /src/gulpfile.js
$ touch /src/.babelrc
$ touch /src/package.json
$ cd /src/
$ apk add vim
.babelrc .babelrc
{
"presets": ["@babel/preset-env"]
}
package.json包.json
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "",
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babelify": "^10.0.0",
"gulp": "^4.0.2",
"gulp-bro": "^1.0.3",
"gulp-rename": "^1.2.2"
}
}
bar.es6.js bar.es6.js
"use strict"
class Bar {
constructor(grammar) {
console.log('Bar time!');
}
}
export default Bar;
foo.es6.js foo.es6.js
"use strict"
import Bar from './modules/bar.es6.js';
class Foo {
constructor(grammar) {
console.log('Foo time!');
}
}
var foo = new Foo()
var bar = new Bar()
gulpfile.js gulpfile.js
const bro = require('gulp-bro');
const gulp = require('gulp');
const rename = require('gulp-rename');
const babelify = require('babelify');
function transpileResources(callback) {
gulp.src(['./app/foo.es6.js'])
.pipe(bro({transform: [babelify.configure({ presets: ['@babel/preset-env'] })] }))
.pipe(rename('app.es5.js'))
.pipe(gulp.dest('./dist/'));
callback();
}
exports.transpile = transpileResources;
$ npm install
$ npm install -g gulp@4.0.2
$ gulp transpile
[09:30:30] Using gulpfile /src/gulpfile.js
[09:30:30] Starting 'transpile'...
[09:30:30] Finished 'transpile' after 9.33 ms
$ node dist/app.es5.js
Foo time!
Bar time!
Source code after transpilation app.es5.js
app.es5.js
后的源代码app.es5.js
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
var _barEs = _interopRequireDefault(require("./modules/bar.es6.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Foo = function Foo(grammar) {
_classCallCheck(this, Foo);
console.log('Foo time!');
};
var foo = new Foo();
var bar = new _barEs["default"]();
},{"./modules/bar.es6.js":2}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Bar = function Bar(grammar) {
_classCallCheck(this, Bar);
console.log('Bar time!');
};
var _default = Bar;
exports["default"] = _default;
},{}]},{},[1]);
Simply switching to webpack instead of browserify fixed the issue for me.简单地切换到 webpack 而不是 browserify 为我解决了这个问题。
var webpack = require('webpack-stream')
gulp.task('default', function () {
return gulp.src('src/source.js')
.pipe(webpack({
output: {
filename: 'app.js'
}
}))
.pipe(gulp.dest('dist/app.js'))
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.