dmonad / lib0

Monorepo of isomorphic utility functions
MIT License
345 stars 62 forks source link

`import "yjs"` has been broken for browser contexts since `lib@0.2.73` due to remapping exports #74

Closed spence closed 11 months ago

spence commented 11 months ago

Describe the bug

lib0/webcrypto has been broken for browser contexts since lib@0.2.73 due to remapping exports (2617ed3).

To Reproduce

# cargo install deno

git clone https://github.com/spence/lib0-webcrypto.git
cd lib0-webcrypto
deno task start

# open http://localhost:8080 and show console

Expected Behavior

Via pinning lib0/webcrypto@0.2.73:

image

Actual Behavior

Without pinning:

image

Environment Information

Additional context

You can see the published source of each version via source maps.

0.2.72 (works)

curl -sS https://esm.sh/v132/lib0@0.2.72/es2022/webcrypto.js.map | jq
{
  "mappings": ";AAEO,IAAMA,EAAS,OAAO,OAChBC,EAAkB,OAAO,gBAAgB,KAAK,MAAM",
  "names": [
    "subtle",
    "getRandomValues"
  ],
  "sourceRoot": "/",
  "sources": [
    "../esmd/npm/lib0@0.2.72/node_modules/.pnpm/lib0@0.2.72/node_modules/lib0/webcrypto.browser.js"
  ],
  "sourcesContent": [
    "/* eslint-env browser */\n\nexport const subtle = crypto.subtle\nexport const getRandomValues = crypto.getRandomValues.bind(crypto)\n"
  ],
  "version": 3
}

0.2.73 (broken)

curl -sS https://esm.sh/v132/lib0@0.2.73/es2022/webcrypto.js.map | jq
{
  "mappings": ";AACA,OAAS,aAAAA,MAAiB,8DAEnB,IAAMC,EAA6BD,EAAW,OACxCE,EAAsCF,EAAW,gBAAgB,KAAKA,CAAS",
  "names": [
    "webcrypto",
    "subtle",
    "getRandomValues"
  ],
  "sourceRoot": "/",
  "sources": [
    "../esmd/npm/lib0@0.2.73/node_modules/.pnpm/lib0@0.2.73/node_modules/lib0/webcrypto.node.js"
  ],
  "sourcesContent": [
    "\nimport { webcrypto } from 'node:crypto'\n\nexport const subtle = /** @type {any} */ (webcrypto).subtle\nexport const getRandomValues = /** @type {any} */ (webcrypto).getRandomValues.bind(webcrypto)\n"
  ],
  "version": 3
}

Related:

dmonad commented 11 months ago

It is strange to me that esm.sh prefers the "node" export over the "default" export. I think I fixed this by explicitly providing a "browser" bundle.

Maybe you should look into jsdelivr or esm.run, as they don't have this weird behavior (they also use rollup, the bundler that I would recommend).

Tip: next time please just describe the steps for reproduction (I usually don't clone foreign repositories).

spence commented 11 months ago

Thanks!

leolorenzoluis commented 11 months ago

@spence How did you solve it? How does this work in Typescript/NextJS without esm.run? This only happens in Next ^13.5.0, so maybe something broke on their end on how they import modules


./node_modules/lib0/random.js
Attempted import error: 'getRandomValues' is not exported from 'lib0/webcrypto' (imported as 'getRandomValues').
dmonad commented 10 months ago

@leolorenzoluis This should work now in the latest release of Yjs (>=13.6.8)

leolorenzoluis commented 10 months ago

@dmonad Same issue with latest next version (13.5.4) and yjs (13.6.8)

Server Error
ModuleDependencyError: export 'getRandomValues' (imported as 'getRandomValues') was not found in 'lib0/webcrypto' (possible exports: default)
dmonad commented 10 months ago

I think you are talking about a different issue. Please open a new ticket with steps for reproduction