我正在开发一个 React 项目,但在让 webpack.config.js
工作时遇到问题,我不断收到 SyntaxError: Unexpected token import
在一个简单的导入语句上:从“path”导入路径
我查看了 other stackoverflow 回答并相应地修改了我的配置,但到目前为止没有任何帮助
webpack.config.js
import path from "path";
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve("dist"),
filename: "index_bundle.js"
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
query: {
babelrc: false,
presets: [
"stage-0",
"react",
["es2015", { "modules": false }],
["env", {
"targets":
{ "uglify": false }
}
]
]
}
}
]
}
};
.babelrc
{
"presets": ["es2015"]
}
获取错误:
(function (exports, require, module, __filename, __dirname) { import path from "path";
^^^^^^
SyntaxError: Unexpected token import
使用 webpack-babel 的经验告诉我,这确实是一件很愚蠢的事情,但我在过去的几个小时里一直在抓狂,无法弄清楚。
最佳答案
Babel 不会转译配置文件,只是在入口点中转译代码。你必须做这个老派的事情。
const path = require("path");
关于javascript - babel-loader 不转换 webpack.config.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44680751/