[英]babel-loader breaks the `this` variable in an npm package
我從awesome-typescript-loader
切換到babel-loader
並且我在我們使用的 npm 包中遇到了這個問題 - lodash-inflection
。
Uncaught TypeError: this.pluralize is not a function at pluralize (lodash-inflection.js:68)
下面是損壞的(左)和工作的(右)之間的斷點。
lodash-inflection.js
的this
變量成為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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.