I'm using webpack in my project, where I'm importing mathjs. When I try to run the generated bundle (or rather, include it in HTML), I get an unexpected token error.
Here's the lines generated by webpack where the unexpected token is:
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();
}
Notice the _λ
variable.
Here's the mathjs code that generates it:
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)
which means that the variable was originally named λ
. Now, while it was new to me that JS supported non-ASCII characters, it clearly must have given how widely used this library is.
Even more peculiarly, mathjs has worked for me previously already. I'm honestly not sure what changed, but when I run webpack now, I get this apparently unreadable output.
Perhaps it's noteworthy to say that I'm also using TypeScript. I do have another issue with mathjs which is that webpack just generates an absolutely massive bundle file, but that should be reserved for a different thread.
My configuration files look as follows:
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"]
}
package.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"
}
}
Edit: Simplest reproducible example I could make, with the 3 configuration files the same as above. Let the entry point specified above, src/view/index.ts, be as follows:
import * as m from 'mathjs';
m.parse("A=B*C").compile();
The error is still the same, although if I actually open bundle.js it does display the lambda-character, so I guess it's just in the chrome console that it isn't displayed properly.
So, for some reason, ANY importing/including of mathjs causes this error. Mind you, I deleted node_modules and reinstalled again. The weirdest part about this is, again, that it used to work just a couple hours ago?!