搞了一天,我不明白为什么 babel for gulp 对我不起作用,或者我错误地连接了模块......
浏览器中的错误是这样的:
Uncaught ReferenceError: require is not defined
at main.min.js:1
我像这样连接模块:
import focusVisible from "focus-visible";
main.min.js 文件中的代码:
"use strict";var e;(e=require("focus-visible"))&&e.__esModule;
吞咽任务:
const { src, dest, series, watch, parallel } = require('gulp'),
fileinclude = require('gulp-file-include'),
rename = require("gulp-rename"),
uglify = require('gulp-uglify-es').default,
babel = require("gulp-babel"),
notify = require("gulp-notify"),
browserSync = require("browser-sync").create()
const changingScripts = () => {
return src(['src/js/main.js', 'src/js/pages/**/*.js'])
.pipe(babel())
.pipe(fileinclude())
.pipe(dest('dist/js'))
.pipe(uglify({
toplevel: true
}).on('error', notify.onError()))
.pipe(rename({
extname: '.min.js'
}))
.pipe(dest('dist/js'))
.pipe(browserSync.stream())
}
package.json 文件是这样的:
{
"name": "project_name",
"version": "1.0.0",
"description": "some description of the project",
"scripts": {},
"keywords": ["keyword_1", "keyword_2", "keyword_3"],
"author": "project_author",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/eslint-parser": "^7.16.3",
"@babel/preset-env": "^7.16.4",
"@babel/register": "^7.16.0",
"browser-sync": "^2.27.7",
"eslint": "^8.2.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.25.3",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-file-include": "^2.3.0",
"gulp-notify": "^4.0.0",
"gulp-rename": "^2.0.0",
"gulp-uglify-es": "^3.0.0"
}
}
.babelrc 文件如下所示:
{
"presets": ["@babel/preset-env"]
}
似乎一切都应用了所需的东西。
如果您能提供帮助,我将不胜感激。
整个项目都因为这个错误而卡住了……
建议不使用就解决问题Webpack
:)