【发布时间】:2018-12-05 22:31:51
【问题描述】:
我正在尝试在 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');
在任何情况下,我都会收到相同的错误,但我找不到解决方案。有人经历过吗?谢谢
【问题讨论】:
-
查看 webpack 指南中代码拆分下的动态导入:webpack.js.org/guides/code-splitting/#dynamic-imports
标签: node.js webpack babeljs dynamic-import