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
Use named exports for ESM #243
base: main
Are you sure you want to change the base?
Conversation
@ArthurKa By default, // mod.js
exports = {a:1}
module.exports = {b:2}
console.log('exports =', exports)
console.log('module.exports =', module.exports) // index.js
const mod = require('./mod')
console.log('mod =', mod)
What will this output?
The issue this solved is that webpack sees exports.a = 1 results in export const a = 1 where module.exports = {a:1} results in const default_1 = {a:1}
export default default_1 |
Ah. OK. I see, I see :) |
db3e1a9
to
225c7e9
Compare
225c7e9
to
69fd672
Compare
This large commit enhances support for JavaScript modules. This adds `lib/index.mjs`, which re-exports `lib/index.js` as a default export and `parse` and `stringify` as named exports. The `module` field in `package.json` now points to this file as the entry point for `import` statements in versions of Node.js that support modules. `test/index.mjs` ensures that `lib/index.js` and `lib/index.mjs` export the same instance. This follows the guidance in the first approach in [Writing dual packages while avoiding or minimizing hazards][guidance]. `lib/index.js` now defines a non-enumerable `default` property on its export to support bundlers, like webpack, and transpilers, like TypeScript. `rollup.config.js` now uses `lib/index.mjs` as its entry point for building browser bundles for JavaScript modules environments. It now writes browser bundles to `dist/json5.umd.js` and `dist/json5.esm.js`, as well as minified versions. These are the new canonical paths for browser bundles. The legacy `dist/index.*` paths still exist for backward compatibility, but they will be removed in the next major version. All files in `build` and `lib` now explicitly require `lib/index.js` to ensure that `lib/index.mjs` is not imported. All files in `test` now require the package root to simulate the default behavior of the version of Node.js being tested. [guidance]: https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#writing-dual-packages-while-avoiding-or-minimizing-hazards
69fd672
to
ac00390
Compare
So what is the reason for not merging? |
@jordanbtucker could you do a alpha release of these changes? Would like to test them out, as JSON5 is currently not usable in dual CJS and ESM modules and I think this would fix it. |
This enhances support for JavaScript modules.
This adds
lib/index.mjs
, which re-exportslib/index.js
as a default export andparse
andstringify
as named exports. Themodule
field inpackage.json
now points to this file as the entry point forimport
statements in versions of Node.js that support modules.test/index.mjs
ensures thatlib/index.js
andlib/index.mjs
export the same instance. This follows the guidance in the first approach in Writing dual packages while avoiding or minimizing hazards.lib/index.js
now defines a non-enumerabledefault
property on its export to support bundlers, like webpack, and transpilers, like TypeScript.rollup.config.js
now useslib/index.mjs
as its entry point for building browser bundles for JavaScript modules environments. It now writes browser bundles todist/json5.umd.js
anddist/json5.esm.js
, as well as minified versions. These are the new canonical paths for browser bundles. The legacydist/index.*
paths still exist for backward compatibility, but they will be removed in the next major version.All files in
build
andlib
now explicitly requirelib/index.js
to ensure thatlib/index.mjs
is not imported. All files intest
now require the package root to simulate the default behavior of the version of Node.js being tested.Fixes #240