我正在做一个项目,想使用 gulp 来自动化一些任务,编译 js 和其他代码。我正在使用Browserify
并Babelify
结合babel-eslint
将我的源类型设置为模块但是当我尝试在我的 main.js 文件中导入 node_modules 时我仍然得到这个 ParseError
ParseError:“import”和“export”可能只出现在“sourceType:module”中
有没有办法来解决这个问题?
我的 gulp 文件
const gulp = require('gulp');
const sass = require('gulp-sass');
const minify = require('gulp-minify');
const rename = require('gulp-rename');
const browserify = require('browserify');
const babelify = require('babelify');
const tap = require('gulp-tap');
const flatmap = require('gulp-flatmap');
const logger = require('gulplog');
const source = require('vinyl-source-stream');
const sourcemaps = require('gulp-sourcemaps');
const buffer = require('vinyl-buffer');
const babel = require('gulp-babel');
const plumber = require('gulp-plumber');
gulp.task('default', function () {
let b = browserify({
entries: './resources/js/main.js',
debug: true,
cache: {},
packageCache: {},
fullPaths: true,
transform: [babelify.configure({
presets: ['@babel/preset-env']
})]
})
return b.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
// Add other gulp transformations (eg. uglify) to the pipeline here.
.on('error', (error) => {
logger.error(error);
})
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/js/'));
});
包.json
{
"name": "portfolio",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"bulma": "^0.9.0"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@vizuaalog/bulmajs": "^0.11.0",
"babel-eslint": "^8.2.6",
"babelify": "^10.0.0",
"browserify": "^16.5.1",
"eslint": "^4.19.1",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-flatmap": "^1.0.2",
"gulp-minify": "^3.1.0",
"gulp-plumber": "^1.2.1",
"gulp-rename": "^2.0.0",
"gulp-sass": "^4.1.0",
"gulp-sourcemaps": "^2.6.5",
"gulp-tap": "^2.0.0",
"gulplog": "^1.0.0",
"node-sass": "^4.14.1",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"browserify": {
"transform": [
[
"babelify",
{
"presets": [
"@babel/preset-env"
]
}
]
]
},
"author": "",
"license": "ISC"
}
eslint.rc
{
"ecmaFeatures": {
"modules": true,
"spread": true,
"restParams": true
},
"env": {
"browser": true,
"node": true,
"es6": true
},
"rules": {
"no-unused-vars": 2,
"no-undef": 2
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
}