javascript - babel-loader 破坏了 npm 包中的 `this` 变量
问题描述
我从awesome-typescript-loader
to切换到,babel-loader
并且在我们使用的 npm 包中遇到了这个问题 - lodash-inflection
。
Uncaught TypeError: this.pluralize is not a function at pluralize (lodash-inflection.js:68)
下面是损坏的(左)和工作的(右)之间的断点。
中的 this
变量lodash-inflection.js
变成了window
而不是包所期望的实际值,因此包不再工作。
下面是修改的内容webpack.js
老的
{
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'awesome-typescript-loader',
options: {
getCustomTransformers: isDev && path.join(__dirname, './webpack.ts-transformers.js')
}
}
新的
{
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'babel-loader',
options: {
presets: [
["@babel/preset-env", { modules: "false" }],
["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }],
"@babel/preset-react",
],
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator"
],
env: {
development: {
plugins: [
[
"styled-components",
{
displayName: true
}
]
]
}, production: {
plugins: [
[
"styled-components",
{
displayName: false
}
]
]
}
}
}
}],
}
我见过的大多数 babel 选项都是这样的。我还尝试更改presets
.
解决方案
查看包,尝试导入和使用单个方法lodash-inflection
似乎存在一个未解决的问题。尝试导入整个包:
import inflection from 'lodash-inflection';
//...
inflection.pluralize(foo, bar);
推荐阅读
- javascript - VueJs过滤表中的日期
- c++ - 模板类实例化如何与类继承一起工作?
- ios - 使用 Flutter 登录 Google:错误代码 -4
- spring - Spring Boot - Apache Derby 复制 ListArray 对象的 ID
- python - 如何将两个图添加到子图?
- mongodb - mongodb的ttl进程通过0
- amazon-web-services - AWS 我可以在 AWS 控制台中查看非默认 VPC 资源吗
- javascript - 等待 Promise 时事件引用丢失
- ansible - 防止 to_nice_yaml 生成别名
- random - RNG种子选择高度影响模拟耳蜗