Skip to content
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 internally + update browser endpoints #145

Merged
merged 5 commits into from
Mar 21, 2020
Merged

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Mar 21, 2020

It's tricky to get multi-platform targeting libraries right these days. Currently, yaml supports:

  • CJS Node >=6
  • ESM Node >=13
  • Bundlers like Webpack & Rollup

To achieve that, the ESM sources are transpiled first to CJS under dist/, with a Node 6.5 target (actually giving us 6.0 support), and then to ESM under browser/dist/, with a Browserslist target that includes IE 11.

CJS Node finds its sources by just automatically adding a .js file extension, and then finding a manually crafted re-exporter from dist/. ESM Node goes via the "exports" object in the package.json, but does much the same. Both of these use the same CJS implementation internally.

Bundlers find their target via the "browser" package.json object, which leads to similar re-exporters from browser/dist/ as for Node.

Eventually, I'd really like to be able to consolidate all of those into one ESM build, but that'll take some time yet for earlier Node versions to go out of favor, and possibly IE 11 as well -- and require a semver-major update to this library. Meanwhile, this PR is about taking a couple of small steps towards a bit more sanity.

Internally, this switches all exports and imports to be named, rather than defaults. This allows us to drop Webpack's _interopRequire* wrappers for require() calls, and not need an __esModule prop in each module.

The browser re-exporters are switched from CJS to ESM, as the implementation they're re-exporting is already ESM. This should be completely transparent to any normal users, but in the particular case of having a custom UMD build of just yaml, you may need to specify that you want its default export exposed. The Webpack option for this is output.libraryExport: 'default'.

@eemeli eemeli merged commit d39ab5e into master Mar 21, 2020
@eemeli eemeli deleted the named-exports branch March 21, 2020 15:19
@eemeli
Copy link
Owner Author

eemeli commented Mar 28, 2020

Comment on this by @BurtHarris here:

@eemeli, I like this style, but could you explain what motivated the change?

I hope the PR's description answers your question?

yhatt added a commit to marp-team/marp-vscode that referenced this pull request Apr 18, 2020
CommonJS build of yaml v1.9.0 throws error `Duplicate export 'YAML'`
while building via rollup.

yaml v1.9.0 has supported ES modules through "exports" field.
(eemeli/yaml#145) However, rollup bundler cannot
recognize it in any plugins.
(rollup/plugins#208)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant