esm.run: importing from the same library breaks singletons [esm-raw]
Describe the bug
Using /+esm to import more than one ES module from the same library results in shared modules being bundled, effectively breaking singleton patterns.
The behavior is demonstrated in this pen:
https://codepen.io/claviska/pen/vYJyVYb?editors=1000
Affected jsDelivr links
This import loads an icon component that imports a base path utility from dist/utilities/base-path.js:
https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/components/icon/icon.js/+esm
This import loads the base path utility directly:
https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/utilities/base-path.js/+esm
In the first URL, you'll see the base path module has been bundled into the icon module. (It's hard to see because the code is mangled, but search for document.getElementsByTagName("script") which is unique to the base path module.)
In the example, the user imports a method called setBasePath() that updates a singleton value. Because the module is bundled with the icon, the singleton is broken resulting in icons having the wrong base path.
Additional context
Contrast the broken example above to unpkg's ?module which works as expected:
https://codepen.io/claviska/pen/eYEBPYR?editors=1000
In this case, the base path module is not bundled with the icon module.
Using singletons in JS modules is a common pattern that lets us scope important values to modules rather than relying on globals. If you need additional information or have any questions, I'm happy to help however I can!
Source: jsdelivr/jsdelivr