我在我的项目中使用 webpack,我正在导入 mathjs。当我尝试运行生成的包(或者更确切地说,将其包含在 HTML 中)时,我收到了一个意外的令牌错误。
这是 webpack 生成的行,其中意外的令牌是:
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _λ = _step.value;
var _i4 = indexOf(uniqueValues, _λ, equal);
if (_i4 === -1) {
uniqueValues.push(_λ);
multiplicities.push(1);
} else {
multiplicities[_i4] += 1;
}
} // find eigenvectors by solving U − λE = 0
// TODO replace with an iterative eigenvector algorithm
// (this one might fail for imprecise eigenvalues)
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
注意_λ
变量。这是生成它的 mathjs 代码:
for (var λ of values) {
var i = indexOf(uniqueValues, λ, equal);
if (i === -1) {
uniqueValues.push(λ);
multiplicities.push(1);
} else {
multiplicities[i] += 1;
}
} // find eigenvectors by solving U − λE = 0
// TODO replace with an iterative eigenvector algorithm
// (this one might fail for imprecise eigenvalues)
这意味着该变量最初被命名为λ
. 现在,虽然 JS 支持非 ASCII 字符对我来说是新事物,但它显然已经给出了这个库的广泛使用范围。更特别的是,mathjs 以前已经为我工作过。老实说,我不确定发生了什么变化,但是当我现在运行 webpack 时,我得到了这个明显不可读的输出。
也许值得注意的是,我也在使用 TypeScript。我确实对 mathjs 有另一个问题,那就是 webpack 只是生成了一个绝对庞大的捆绑文件,但这应该保留给不同的线程。
我的配置文件如下所示:
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/view/index.ts',
mode: 'development',
devtool: 'inline-source-map', // For some reason no other option works well. Might be related.
module: {
rules: [
{
test: /\.(t|j)sx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
}
};
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "./dist/",
"rootDirs": [
"./src/"
],
"sourceMap": true,
"strict": true,
"target": "ES2020" //Used to be ES2016, I changed to 20 to test if it resolved the issue. It didn't.
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
包.json
{
...
"scripts": {
...,
"build": "webpack"
},
"devDependencies": {
...
"@types/node": "^15.12.4",
"ts-loader": "^9.2.3",
"ts-node": "^10.1.0",
"typescript": "^4.3.5",
"webpack": "^5.44.0",
"webpack-cli": "^4.7.2"
},
"dependencies": {
...
"mathjs": "^9.4.3"
}
}
编辑:我可以制作的最简单的可重现示例,其中 3 个配置文件与上述相同。让上面指定的入口点 src/view/index.ts 如下:
import * as m from 'mathjs';
m.parse("A=B*C").compile();
错误仍然是一样的,尽管如果我真的打开 bundle.js 它确实显示了 lambda 字符,所以我猜它只是在 chrome 控制台中没有正确显示。
因此,出于某种原因,任何导入/包含 mathjs 都会导致此错误。请注意,我删除了 node_modules 并重新安装。最奇怪的部分是,它曾经在几个小时前工作过?!