-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected token in round.js #1554
Comments
It looks like webpack uses the es6 source code in the folder |
Hmm, I don't think we've had this problem with any other libraries. My understanding is that libraries will generally compile down to ES5 for their released version. I guess we can use the es5 version. In mathjs 5.10.3 we have been manually importing all of the pieces we need like so: import add from 'mathjs/lib/function/arithmetic/add';
import bignumber from 'mathjs/lib/type/bignumber';
import core from 'mathjs/core';
import divide from 'mathjs/lib/function/arithmetic/divide';
import mathEval from 'mathjs/lib/expression/function/eval';
import multiply from 'mathjs/lib/function/arithmetic/multiply';
import subtract from 'mathjs/lib/function/arithmetic/subtract';
// http://mathjs.org/docs/custom_bundling.html
const math = core.create();
// types need to be imported before functions
math.import(bignumber);
math.import(multiply);
math.import(add);
math.import(subtract);
math.import(divide);
math.import(mathEval);
export default math; It doesn't appear that we can do it like this anymore, though, unless I'm missing something. It looks like there is a tree of dependencies that need to be handled. |
That's correct, though more and more libraries (and mathjs now too) start exposing ES6 modules, amongst others to better optimizing of bundles using tree shaking out of the box. This should be simply a matter of configuring your Webpack setup to also transpile the mathjs code. Maybe the following topic is helpful: babel/babel-loader#171 (comment) |
I've just done a test to double check whether the exported modules and treeshaking works correctly. I created an empty React application using
When building the application, it runs just fine on all browsers including IE11, and treeshaking works as expected (only thing was I had to add a polyfill for |
@josdejong Hmm, I also create a test demo by vue-cli3 (use mathjs 6.0.3). Appear the same error in Edge and other low version browsers (e.g. QQ browser 9.6.3 [ based on chromium53.0.2785 ]). I just do this import { random, evaluate, round } from 'mathjs/number';
export default () => {
let __result = random(0, 100);
__result = round(__result, 2);
return __result;
}; -- App.vue <template>
<div id="app">
{{ result }}
<button @click="calc">click me</button>
</div>
</template>
<script>
import func from '@/components/mathtest.js';
export default {
name: 'app',
data() {
return {
result: 0,
}
},
methods: {
calc() {
this.result = func();
}
}
}
</script> |
@mockdeep the error message is about webpack not recognizing the |
@taozi926494 can you create a separate issue for the vue3 thing? I did a bit of debugging with your code, It seems to have a different cause. Not entirely sure though. Can you create a codesandbox or stackblitz example demonstrating the issue? |
@josdejong I think the problem is, as you mentioned, that we're not transpiling mathjs with Webpack. We've got plenty of spread operators in our own code, but we don't transpile node modules, as that would take forever. We'll leave it locked at version 5.x for now until we can figure out a better option. |
@mockdeep did you try upgrading Webpack? That may be a good idea anyway? |
I may understand the issue now: the mathjs configuration in package.js has a |
@josdejong we're currently on version 3.4.0. We haven't had a chance to upgrade to version 4.x, but I also don't think that should matter for this use case.
I think |
I think you're right that it's not because of an old version of Webpack. I indeed think it's because the module code should be transpiled to ES5. Still have to figure out how to get that working with babel (tried the |
|
Yes indeed. I would like to offer both with mathjs: the |
Ah, okay, I see what you're saying now. I'm a little surprising that the ES modules one needs to be transpiled to ES5 still. |
Okay, I did some digging and did my best to answer your question. |
That's it indeed 🎉, thanks a lot. I have a working proof of concept now. I hope to work it out tomorrow night and publish the fix. |
This issue should be fixed now in |
When upgrading to MathJS 6.0.2, we get an error when trying to make use of mathjs in our application, compiling with Webpack 3.4.0:
Here's what it looks like where we set up MathJS:
The text was updated successfully, but these errors were encountered: