feat(publisher): implement ESM for publisher.js - #4166
Conversation
b3ae6e0 to
3f16115
Compare
everyplace
left a comment
There was a problem hiding this comment.
Commented on the bigger changes to help explain the overall narrative.
| # Copy files. | ||
| cp dist/$basename.template.js dist/$basename$target.js | ||
| cp dist/$basename.template.js.map dist/$basename$target.js.map | ||
| for ext in "js" "mjs"; do |
There was a problem hiding this comment.
Content below isn't changed, just indented because of the extra loop above.
| publisher: { | ||
| output: args.minifiedPublisherName || 'publisher.js', | ||
| input: './src/publisher-main.ts', | ||
| esm: true, |
There was a problem hiding this comment.
Only when esm is present and true do mjs versions of the code also get output.
| const target = args.target || 'classic'; | ||
| const {input, output, esm} = builds[target]; | ||
| const outputs = [ | ||
| { |
There was a problem hiding this comment.
This used to be inlined below, but is now higher up to allow for contextually adding the esm version only when relevant.
| } | ||
|
|
||
| export function installPublisherRuntime(win: Window) { | ||
| interface PublisherWindow extends Window { |
There was a problem hiding this comment.
Adds an internal PublisherWindow interface for safer type checking against existing runtime installations, and introduces an optional autoStart configuration so ESM imports can avoid running immediate DOM side-effects upon evaluation.
| if (existingProp && !Array.isArray(existingProp)) { | ||
| return; | ||
| return ( | ||
| existingProp.api ?? { |
There was a problem hiding this comment.
Returns the existing .api singleton reference on duplicate script invocations so module exports do not evaluate to undefined, with a defensive no-op fallback to prevent TypeError runtime exceptions if the namespace was improperly initialized.
| if (script.getAttribute('preferred-sources-control') === 'manual') { | ||
| autoInit = false; | ||
| break; | ||
| if (options?.autoStart !== false) { |
There was a problem hiding this comment.
The DOM scanning logic below is unchanged from the original implementation—it is simply indented inside the new options?.autoStart check to bypass automatic DOM queries during passive ESM imports.
|
|
||
| installPublisherRuntime(self); | ||
| export const preferredSource = installPublisherRuntime(self, { | ||
| autoStart: !IS_ESM_BUILD, |
There was a problem hiding this comment.
Only defaults autoStart to true if the build is not ESM. If it is ESM, auto-start is disabled in favor of manual initialization.
|
|
||
| # Remove template binaries. | ||
| rm dist/*template.js* | ||
| rm -f dist/*template.*js* |
There was a problem hiding this comment.
Allows for .js and .mjs
ef68ddb to
8c8dbbf
Compare
…sher bundle - Refactor installPublisherRuntime to return PreferredSourceApi and attach .ready() promise to win.PREFERRED_SOURCE. - Support passive-by-default initialization via autoStart option to prevent side effects when importing publisher as an ES module. - Export preferredSource instance directly from publisher-main.ts entry point. - Configure Vite multi-format bundling (IIFE and ES) exclusively for the publisher target without impacting legacy bundles. - Add unit tests for API return types, .ready() resolution, and autoStart suppression.
…s in publisher module
…% branch coverage
…lean placeholder for consistency
8c8dbbf to
1e0bf8a
Compare
This PR adds a new export to the build script for esm bundles, enabling modern import syntax as a module.