我正在尝试将 babel-loader 与 babel-plugin-transform-runtime 一起使用。
我已按照以下说明进行操作: https://github.com/babel/babel-loader#babel-is-injecting-helpers-into-each-file-and-bloating-my-code
相关代码:
rules: [
// the 'transform-runtime' plugin tells babel to require the runtime
// instead of inlining it.
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/transform-runtime']
}
}
}
]
构建时出现以下错误:
模块构建失败:错误:找不到模块“@babel/plugin-transform-runtime”
如果我将插件名称更改为:plugins: ['transform-runtime'],我会收到以下错误:
模块构建失败:TypeError:this.setDynamic 不是函数
问题是什么?
最佳答案
经过一番努力,我找到了正确的方法。
Tl;dr
如果你安装了新的 babel 加载器,你应该加载新的 babel 插件。
全文
官方文档中的安装:
npm install babel-loader@8.0.0-beta.0 @babel/core @babel/preset-env webpack
在 github 页面中,这些是 runtime 插件的说明:
NOTE: You must run npm install babel-plugin-transform-runtime --save-dev to include this in your project and babel-runtime itself as a dependency with npm install babel-runtime --save.
相反,您应该像这样使用新版本:
npm install --save-dev @babel/plugin-transform-runtime
npm install --save @babel/runtime
然后它将与文档中的配置一起工作。
关于javascript - Webpack babel-loader runtime : Module build failed: TypeError: this. setDynamic 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48366243/