Repository navigation
Use named exports for ESM - #243
Closed
jordanbtucker wants to merge 5 commits into
Closed
jordanbtucker wants to merge 5 commits into
jordanbtucker wants to merge 5 commits into
Conversation
Member
Author
|
@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 = 1results in export const a = 1where module.exports = {a:1}results in const default_1 = {a:1}
export default default_1 |
|
Ah. OK. I see, I see :) |
jordanbtucker
force-pushed
the
named-exports
branch
7 times, most recently
from
August 12, 2022 05:54
db3e1a9 to
225c7e9
Compare
jordanbtucker
force-pushed
the
named-exports
branch
from
August 12, 2022 06:07
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
jordanbtucker
force-pushed
the
named-exports
branch
from
August 12, 2022 06:11
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. |
Member
Author
|
Closed by #370 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


This enhances support for JavaScript modules.
This adds
lib/index.mjs, which re-exportslib/index.jsas a default export andparseandstringifyas named exports. Themodulefield inpackage.jsonnow points to this file as the entry point forimportstatements in versions of Node.js that support modules.test/index.mjsensures thatlib/index.jsandlib/index.mjsexport the same instance. This follows the guidance in the first approach in Writing dual packages while avoiding or minimizing hazards.lib/index.jsnow defines a non-enumerabledefaultproperty on its export to support bundlers, like webpack, and transpilers, like TypeScript.rollup.config.jsnow useslib/index.mjsas its entry point for building browser bundles for JavaScript modules environments. It now writes browser bundles todist/json5.umd.jsanddist/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
buildandlibnow explicitly requirelib/index.jsto ensure thatlib/index.mjsis not imported. All files intestnow require the package root to simulate the default behavior of the version of Node.js being tested.Fixes #240