jspm / project

Roadmap and management repo for the jspm project
161 stars 8 forks source link

undefined is not a function, when using JSPM CDN #294

Closed trusktr closed 11 months ago

trusktr commented 1 year ago

This has an error:

playground link

Uncaught TypeError: r.escape is not a function

Here's the same example but using the local node_modules from docs.lume.io (it currently has the same 0.3.0-alpha.19 version of lume installed, see the version logged to console):

playground link

Just to double check, JSPM's importmap is getting 1.6.0:

https://ga.jspm.io/npm:regexr@1.6.0/index.js

And the one from docs.lume.io is currently the same version:

https://docs.lume.io/node_modules/regexr/package.json

{
    "name": "regexr",
    "version": "1.6.0",

Search for r.escape in this file:

https://docs.lume.io/node_modules/regexr/src/index.js

trusktr commented 12 months ago

Ah, looks like JSPM picks regex@1.6.0/index.js (the non-ESM file, which has the CommonJS default export discrepancy), while my importmap is mapping to regexr@1.6.0/src/index.js (the actual ESM file).

Looks like this could be solved with an override, but regexr 2.0 is already out, published as ESM and type:module, so a better fix is to get the depedency updated in Lume.

trusktr commented 12 months ago

I thought a workaround would be to manually change the generated importmap to point to the src/index.js ESM file, but apparently JSPM does not expose that file. This link is 404:

https://ga.jspm.io/npm:regexr@1.6.0/src/index.js

playground link

Here's what we see on unpkg:

https://unpkg.com/regexr@1.6.0/src/index.js

This makes it a bit difficult to perform an immediate workaround.


Should JSPM CDN allow fetching different files, like unpkg does, to make workarounds for these sort of situations easy?

Regexr 1.6 doesn't have type:module or an exports field, so the file not being exposed seems like an arbitrary limitation.

guybedford commented 11 months ago

JSPM does not expose all files in a package, only those that are reachable. It is not a direct npm mirror but instead a modular build of npm packages.

Because the package.json "exports" field is encapsulating, other files are now non-reachable via import in Node.js and other environments.

This same property of non-reachability is also what enables the JSPM package optimizations to apply.

trusktr commented 11 months ago

regexr 1.6 does not have an exports field though:

https://unpkg.com/browse/regexr@1.6.0/package.json

The expecation of publishing a package *without* an exports field (f.e. to NPM) is that we can simply import any file from that package.

guybedford commented 11 months ago

This should be working now if you clear your browser cache.