我正在尝试在Node.js中动态导入Webpack
if (condition) {
import('webpack').then(webpack => webpack);
}
但是在终端中,我看到以下错误:
C:\Users\myUser\react\node_modules\@babel\core\lib\transformation\normalize-file.js:209
throw err;
^
SyntaxError: C:\Users\myUser\react\server\index.js: Support for the experimental syntax 'dynamicImport' isn't currently enabled (23:3):
19 |
20 | if (condition) {
> 21 | import('webpack').then(webpack => webpack);
| ^
22 |
Add @babel/plugin-syntax-dynamic-import (https://git.io/vb4Sv) to the 'plugins' section of your Babel config to enable parsing.
我的.babelrc中安装了@ babel / plugin-syntax-dynamic-import
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-class-properties"
]
}
我甚至尝试使用加载器“ babel-loader”将其添加到.js规则下的webpack conf文件中。
我正试图避免使用CmJS
const webpack = require('webpack');
无论如何,我都会收到相同的错误,并且找不到解决方案。有人经历过这个吗?谢谢
答案 0 :(得分:1)
将plugins: ["dynamic-import-webpack"]
添加到.babelrc
并安装插件$npm i babel-plugin-dynamic-import-webpack --D