-
-
Notifications
You must be signed in to change notification settings - Fork 630
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
Add ES Module Support #1100
Add ES Module Support #1100
Conversation
Thanks, looks good, let me test locally first and happy to merge then. |
actually, can you think of unit test for this? Should run for node 13+ only |
merging for now, but would be good to add unit test somehow later |
Note that package This change needs more work; as it is currently it will stop most imports from working. When you specify an For example, with: {
"exports": {
"./promise": "./promise.js"
}
} Here are some examples that will error: require('mysql2')
require('mysql2/package.json')
require('mysql2/lib')
require('mysql2/lib/index')
require('mysql2/lib/index.js')
require('mysql2/promise.js') Shooting from the hip, if you are happy to allow everything in {
"exports": {
".": "./lib/index.js",
"./lib/": "./lib/",
"./promise": "./promise.js"
"./promise.js": "./promise.js"
"./package": "./package.json",
"./package.json": "./package.json"
}
} You could choose not to do the additional |
thanks for heads up @jaydenseric! Only 2 entry points are documented to work, |
Anything declared in https://unpkg.com/browse/[email protected]/ When moving to package
I don't think Note that ESM can only import from CJS files with a default import, so to support more ergonomic ESM named imports you need to use conditional package exports, and have seperate https://github.com/jaydenseric/extract-files/blob/v8.1.0/package.json#L33 Note that conditional package exports breaks Node.js v13-13.6: https://github.com/jaydenseric/extract-files/blob/v8.1.0/package.json#L43 There is much to discuss on this topic; it's enormously complex. Unfortunately almost everything you see or read in the wild is now outdated/wrong. |
Do you have any experience unit testing all this complexity @jaydenseric ? Don't want to test this manually more than once |
Adequately explaining the testing also involves explaining the best practices of how it all works, which I don't have the time to do here, sorry. I don't actually use this package; I just noticed this PR get merged and thought to give you a heads-up about the gotchas. IMO the gold standard is testing the real ESM functionality of Node.js without any transpilation or money-patching. This particularly helps detect invalid importing of dependencies that would be tolerated by Babel/Webpack. To do that without abandoning old Node.js or CJS, a few things need to happen. First, here is a package test scripts excerpt: https://github.com/jaydenseric/extract-files/blob/v8.1.0/package.json#L77 {
"test:esm": "if node --input-type=module -e '' > /dev/null 2>&1; then coverage-node -r hard-rejection/register test/index.mjs; fi",
"test:cjs": "coverage-node -r hard-rejection/register test",
} If Node.js supports ESM natively the If you publish any ESM https://github.com/jaydenseric/extract-files/tree/v8.1.0/src/test Note that for lib code, with the exception of entrypoint files you want named exports to work from, you don't want sibling https://nodejs.org/api/esm.html#esm_dual_package_hazard I buy into strategy 1: https://nodejs.org/api/esm.html#esm_approach_1_use_an_es_module_wrapper This has implications for tree-shaking, but IMO this is balanced out by removing the chance that separate Note that package |
that would be super helpful |
Hi, this PR appears to be causing a regression - import 'mysql2' is not working
Please see PR above for a fix for the issue |
Just a heads-up: v2.2.0 was a breaking change for me, as I was previously using
which worked fine up to v2.1.0. From v2.2.0 it is necessary to use
Perhaps it would be worth adding
to the |
sounds like an easy fix, can you create PR? |
Adding ES module support to resolve #1093