esm.sh icon indicating copy to clipboard operation
esm.sh copied to clipboard

Failed to import - @pyscript/core

Open WebReflection opened this issue 2 years ago • 5 comments

Failing module

  • GitHub: https://github.com/pyscript/pyscript/
  • npm: https://www.npmjs.com/package/@pyscript/core
// wait for bootstrap and see errors about re-definition of something
// that is defined once in core and nowhere else
import('https://esm.sh/@pyscript/core/dist/core.js').then(
  () => document.body.appendChild(document.createElement('py-script'))
);

Error message

After onload I got this:

Uncaught (in promise) DOMException: Failed to execute 'define' on 'CustomElementRegistry': the name "py-script" has already been used with this registry

Additional info

We've found this issue and the problem is that esm.sh re-bundle our /dist/error-XXX.js file which imports from /dist/core.js which was also the initiator (it's about lazy plugins with a shared source). Basically instead of looking exactly as we published it in our pre-optimized dist folder, it looks like this causing all sort of problems with our project, as the core.js can't be shared, reused, or trusted by any mean.

Moreover, it looks like explicit exports field in package.json are completely ignored so that we cannot use just //esm.sh/@pyscript/core neither, as that should point to /dist/core.js instead of trying to resolve all the things.

Is there any way to specify we want esm.sh to follow defined exports and keep the files untouched? Thank you.

WebReflection avatar Sep 11 '23 09:09 WebReflection

FYI we've changed the default main export as index.js, which simply import * from './dist/core.js' but the issue remains and there's no escape hatch to avoid re-bundling our project in a way that makes sense (or that work at all).

WebReflection avatar Sep 12 '23 08:09 WebReflection

the name "py-script" has already been used with this registry

seems the package registries the element automatically

ije avatar Sep 12 '23 10:09 ije

@ije that's how custom elements work, the issue is that the error plugin includes again the whole package breaking in all possible ways:

  • the reference to hooks is not the desired one
  • anything in there is not the same as anything exported by the initial core.js
  • every other CDN works as expected, esm.sh is the only one breaking our module

Please read again Additional Info in the filed bug and compare what we expect as a file to what this CDN returns instead, thanks.

WebReflection avatar Sep 12 '23 11:09 WebReflection

@ije Is there a plain esm mode without bundling. That would probably solve the issue. Bundling is not good for singletons, especially when there are multiple import() calls and esm.sh has no idea what packages to share, so it if bundles them each time, that causes problems and it is not how ES Modules actually work. I had same issues with Skypack, but that's inactive now.

trusktr avatar Nov 19 '23 05:11 trusktr

This issue was fixed with the introduction of raw.esm.sh. Here is the original example with the no bundling option enabled:

import('https://raw.esm.sh/@pyscript/core/dist/core.js').then(
  () => document.body.appendChild(document.createElement('py-script')) 
);// Add your code here 

We can close this issue

GianlucaGuarini avatar Apr 25 '24 07:04 GianlucaGuarini