-
Notifications
You must be signed in to change notification settings - Fork 780
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
Build: Add native ESM distribution #1798
Conversation
f3f6c02
to
17ce8ca
Compare
Extracted from #1798 to minimize the diff.
57adbb8
to
e18fedb
Compare
This is a bit more convoluted. Bundlers, when the However, the interop of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments but nothing blocking
9a020ee
to
5ad054c
Compare
This is amazing 🎇 |
First released with QUnit 3.0.0-alpha.4. Ref qunitjs/qunit#1798. Ref qunitjs/qunit#1551.
First released with QUnit 3.0.0-alpha.4. Ref qunitjs/qunit#1798. Ref qunitjs/qunit#1551.
Source
Convert /src/core/qunit.js to ESM export.
This file can be used directly by ESM in theory when debugging, but in practice we'll let rollup bundle it into
qunit.module.js
, which is otherwise functionally the same as the source file.Add /src/core/qunit-commonjs.js as second entrypoint for rollup.
Rollup would complain if attempting to export the ESM qunit.js source as "iife", as Rollup will detect the input file as having exports and insist on taking control over exporting it to a global variable. We don't want that as we control that ourselves instead.
qunit-commonjs.js is a thin entrypoint that generates the
qunit.js
distribution file, which is the same asqunit.module.js
except in "iife" format, with our (conditional)module.exports
assignment at the end.Build
/qunit/qunit.js
is the same ES5/CJS compatible distribution it has always been, now sourced from /src/core/qunit-commonjs.js./qunit/qunit.module.js
is a complete standalone distribution in ESM format. Given that IE11 doesn't support type=module, I've removed IE11 from the Babel config for this file, which makes the file a bit smaller as it uses most ES6 features natively (275K vs 250K; uncompressed, unminified).Package
If we simply advertised the new
/qunit/qunit.module.js
file inpackage.json#exports.module
, then we would break numerous Node.js and bundler use cases.To test for these, previous commits already added
/demos/bunders/
, which is tested vianode bin/qunit.js demos/bundlers.js
, as part ofnpm run test-demos
(and the "CI: Demos" GitHub job).Specifically:
A Node.js project that uses a mixture of CJS
require('qunit')
and ESMimport 'qunit'
previously shared a single logical QUnit instance since the module was just one file, and thus both used the same internal "require"-cache. This means the user's test files, or any test runners, plugins, etc, all import/require the same qunit module to define assertion plugins, add a hook listeners, or register tests. And thus everything executes correctly on the main QUnit instance.As-is this would break by having some of these attach stuff to a secondary instance that the main test runner (e.g. QUnit CLI) isn't running.
A frontend project that uses a bundler or adapter (e.g. karma-qunit), and similarly involves mixed use among its test files and/or between it and any plugins, that sends the result to a browser. As-is this would end up embedding two separate copies of QUnit, unaware of each other.
To mitigate this, package.json contains two overrides:
node
, tell ESM mode to import a Node.js-specific wrapper that re-uses and re-exports the CJS file.bundler
, tell CJS mode to import a bundler-specific wrapper that re-uses and re-exports the ESM file.This is based on https://github.com/jquery/jquery/wiki/jQuery-4-exports-explainer as implemented at jquery/jquery#5429, which does the same for jQuery.
Fixes #1551.
Fixes #1724.