-
-
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
Feature/specify import require paths #1941
Feature/specify import require paths #1941
Conversation
Using `prepare` step to build ensures that dependency can be installed directly from git repository https://docs.npmjs.com/misc/scripts#prepublish-and-prepare `prepublishOnly` is run **after** `prepare` script so this should have no effect. Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
* chore(package): update @babel/core to version 7.8.7 * chore(package): update @babel/preset-env to version 7.8.7 * chore(package): update lockfile package-lock.json Co-authored-by: greenkeeper[bot] <23040076+greenkeeper[bot]@users.noreply.github.com>
…#1774) * remove contributors from package.json in favour of AUTHORS The contributors section of our package.json is _very_ long and manually updating can (?) be a pain. I took a leaf out of nodejs's book and copied their update-authors script [1]. [1]: https://github.com/nodejs/node/blob/master/tools/update-authors.js * remove some of my many emails * add a couple more mail mappings * get lint passing * add instruction to update-authors Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
…ecision when input contains BigNumbers
* make dot product faster and correct for ℂ * minor fixes * added tests, fixed bugs * add dot for sparse matrices * make multiply(vec, vec) use dot * add test for complex vectors * added test for mul(vec, vec), removed one TODO comment Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
Shouldnt provide any issues for other systems I hope
This reverts commit 4cd2704.
This reverts commit 268b594.
Bumps [@babel/preset-env](https://github.com/babel/babel) from 7.9.0 to 7.9.5. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/master/CHANGELOG.md) - [Commits](babel/babel@v7.9.0...v7.9.5) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
* Prefix the cli test with 'node' so it works on windows Shouldnt provide any issues for other systems I hope * Revert "Prefix the cli test with 'node' so it works on windows" This reverts commit 4cd2704. * Revert "Revert "Prefix the cli test with 'node' so it works on windows"" This reverts commit 268b594. Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
I've had a play, trying to reproduce that node issue you mentioned in point 10:
I'm unable to reproduce the issue. I've tested in Node v11.15, v12.18, and v14.3 |
Thanks for your updates! So if I'm not mistaken these points are still open to be adressed:
Feedbacks on the other points:
|
|
|
Hey, sorry, I've not had the chance to get back to this for a while.
|
Okay, I've re-checked point 8, and you're right it does have issues, using the nested package.json in the Firstly it doesn't like the lack of extension in the imports. So I added them. e.g. in module.exports = /* #__PURE__ */ defaultInstance If you set the root package.json to {
"type": "commonjs"
} Currently it's the other way around, and we have one in the I do agree that it would be good to get it so that ESM is the "default" and set in the root package.json file. I think the only thing stopping that is the fact that the gulp process breaks. |
No problem at all! Take it easy. I guess we're in the same boat 😉
|
FYI: I've changed the base of this PR to be merged into |
|
I've just found this: https://gulpjs.com/docs/en/getting-started/javascript-and-gulpfiles/ Which suggests that Gulp does support ESM, if the gulpfile is named as |
|
|
I've got this mostly working when making the root package.json I'm having issues with the
So I tried changing it to use
However, on my test project that includes the local repo, I can run both ESM and CommonJS without issues. So it looks like it's only a problem with the tests.
|
Ai! I see the problem. If I remember well I didn't find a solution to export dynamic output using ES6, or maybe it was that I couldn't get treeshaking working. Let me give it a try finding a solution for this. I'm amazed that the library itself indeed does work despite this single commonjs file, only the build/test seem to have issues. |
I expect to be able to look into it coming Wednesday. |
I have an idea to turn this into a proper ES file: generate all the exports explicitly. Will require a small new build script similar to those other scripts generating index files etc, shouldn't be hard. Let me try that out coming Wednesday! |
I now understand why the library works but the tests don't: the |
It was quite a puzzle but I think I've got everything working. I've merged this PR into a new branch in the mathjs repo and created a PR: #1962 Still a few open ends, but getting there 😅 |
That's fantastic! Looks good. |
Your PR is merged only this page doesn't yet know it because your work is still in a separate feature branch. I expect it to be marked as "merged" by Github automatically once we've merged this feature branch. |
* Add `.js` extension to source file imports * Specify package `exports` in `package.json` Specify package type as `commonjs` (It's good to be specific) * Move all compiled scripts into `lib` directory Remove ./number.js (You can use the compiled ones in `./lib/*`) Tell node that the `esm` directory is type `module` and enable tree shaking. Remove unused files from packages `files` property * Allow importing of package.json * Make library ESM first * - Fix merge conflicts - Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs` - Refactor unit tests to be able to run with plain nodejs (no transpiling) - Fix browser examples * Fix browser and browserstack tests * Fix running unit tests on Node 10 (which has no support for modules) * Fix node.js examples (those are still commonjs) * Remove the need for `browserBundle.cjs` * Generate minified bundle only * [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963) Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.** - [Release notes](https://github.com/bitinn/node-fetch/releases) - [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md) - [Commits](node-fetch/node-fetch@v2.6.0...v2.6.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Cleanup console.log * Add integration tests to test the entry points (commonjs/esm, full/number only) * Create backward compatibility error messages in the files moved/removed since v8 * Describe breaking changes in HISTORY.md * Bump karma from 5.2.1 to 5.2.2 (#1965) Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2. - [Release notes](https://github.com/karma-runner/karma/releases) - [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md) - [Commits](karma-runner/karma@v5.2.1...v5.2.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
I've just published This PR should be marked as merged already, I'm not sure why it doesn't. Will close the PR now, all your code is in |
What it changes
There are three main parts of this PR:
.js
extension to all theimport
statements in thesrc
directory.This makes it ES module compliant, as file extensions are required for ESM, and work for CommonJS as well.
When scripts are compiled to CommonJS or ESM it retains the file extension in the
import
/require
statements.I did try to get Gulp to be able to change the extension to
.mjs
for ESM and.cjs
for CommonJS, but couldn't find a way of doing it.Instead, I had to settle with adding a
package.json
file in the compiled ESM directory to specify thetype: module
, the inverse of the example here: https://2ality.com/2019/10/hybrid-npm-packages.html#variant%3A-avoiding-.mjs It can't be a bad idea if he's suggesting it 😉exports
property to thepackages.json
file, which tells node 12+ which files to use, depending on whether you're usingimport
orrequire
.On node < 12, it will fallback to the
main
property of thepackage.json
as it currently does, so there should be no difference there.lib
, rather than several different ones.We now have:
/dist
->/lib/browser/
: The scripts suitable for browser use/lib
->lib/umd/
: The CommonJS / UMD scripts/esm
->/lib/esm
: The ES module scriptsIt also removes the root
/number.js
file, as theexports
points directly to the compiled ones.What it fixes
This aims to fix #1928 when trying to use mathjs ESM directly in node, without a package bundler.
Currently, node either tries to load the CommonJS versions and doesn't like it or, if you specify an ESM script it complains about the lack of file extension.
It also attempts to fix the issue #1766 although there's currently an outstanding issue with Jest, where it doesn't understand the
exports
property.However, once Jest does support
exports
, these changes should work for Jest as well, fixing that issue.In the meantime, you should be able to work around it by being specific with the
import
path:import mathjs/lib/esm/number.js
should work as thepackage.json
tells us it's an ESM file.import mathjs/lib/umd/number.js
should also work as it uses CommonJSrequire
anyway.How it's tested
npm run build
still completes successfully, and that the tests all still run and pass (locally, and they also pass in Travis)..mjs
and.cjs
file, importing mathjs and running scripts.These changes are very much breaking changes.
I'm more than happy to discuss this PR, and happy to make any changes.