gpt4 book ai didi

gulp - 另一个解析错误: 'import' and 'export' may appear only with 'sourceType: module' :(

转载 作者:行者123 更新时间:2023-12-01 21:47:49 27 4
gpt4 key购买 nike

我花了几天时间在互联网上搜索这个问题的解决方案(请不要将其标记为重复!)。我正在尝试使用 ES6 导入:

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

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

我的 gulp 任务无法在“从 bulmajs 导入”行编译;我收到可怕的“ParseError:'导入'和'导出'可能仅与'sourceType:模块'一起出现”:(

这是 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,但看不出我的 task/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 帖子,但仍然没有找到解决方案。

最佳答案

Babelify 默认情况下不处理任何node_modules,因为其中通常有数以万计的文件需要处理。因为 bulmajs 的源码是用依赖 es6 的 javascript 编写的(不是下划线的情况!),所以它也需要进行 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

关于gulp - 另一个解析错误: 'import' and 'export' may appear only with 'sourceType: module' :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53037883/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com