Mathjs: Upgrade source code and builds scripts to ES6/exnext

Created on 6 Aug 2017  路  13Comments  路  Source: josdejong/mathjs

math.js still uses ES5 as source code. It would be nice to be able to use ES6, since it has a lot of useful improvements like const/let, arrow functions, etc.

To be able to start using ES6 in the source code, we need to:

  • move the source code from lib to src (need to plan this carefully, no open PR's)
  • create a build script which transpiles ES6 code in src into ES5 code in lib (for node.js)
  • extend the script that bundles the source code into dist such that it transpiles to ES5.
  • do some updates in package.json to point to the esnext and es5 code entry points.

Help would be welcome here.

feature help wanted

Most helpful comment

I want to setup an ES6 build script today, at first without any refactoring, basically move the code from lib to src. This means that any open pull requests will get merge conflicts but there is nothing big open at this moment except the v5 branch and browser_testing which I will merge first

All 13 comments

Something like https://lebab.io/ could help with converting the current code base into es6.

I hadn't thought about trying to automatically convert code to es6, that could be interesting too, thanks for the tip Harry.

Thanks @josdejong for creating this great library.
This is a good idea, do you have any plans for this?

Plans enough but time is the limiting factor here :smile:

If we want to use es6 import/export statements we would need to first move all the require() calls into the top level of the module.

Yes indeed, good point. I would also like to get rid of all the semicolons :)

In total, it's quite some work, but I think we can do the migration in small steps, something like:

  1. set up a build-script and move the code (as is) to src, have the transpiled code in lib
  2. refactor require() calls with ES6 modules (import / export)
  3. refactoring removing all semicolons
  4. rewrite the examples and documentation to ES6 (where applicable).
  5. where applicable, we can simplify/improve ES5 code by refactoring to ES6. Can be done piece-by-piece I think. Like rewrite use of prototype with class (something like https://lebab.io/ could come in handy there), start using arrow functions where it helps for better readability, replace {foo: foo} with more concise {foo}, etc...

I want to setup an ES6 build script today, at first without any refactoring, basically move the code from lib to src. This means that any open pull requests will get merge conflicts but there is nothing big open at this moment except the v5 branch and browser_testing which I will merge first

See #1129

Implemented via #1129

Something like https://lebab.io/ could help with converting the current code base into es6.

I've played around a bit with lebab, it's impressive. Converting var to const/let works nicely. Converting require to import/export gives issues, we need some refactoring here and there to make this work.

Converting require to import/export gives issues, we need some refactoring here and there to make this work.

Am I correct in saying this is because lebab is expecting require to be at the top level and we call require from functions?

Yes exactly, that is one of the reasons (see related discussion #71). And there are other places where we simply have to refactor stuff like using import * as utils from '../../utils' instead of importing specific functions. And for some reason some stuff breaks like type.Complex is not defined, not yet sure what's going wrong there.

lebab was of great help so far! Saved a lot of work refactoring var into let/const

Was this page helpful?
0 / 5 - 0 ratings