Skip to content

Failed to import - cbor-x works with pinned v135, but not with latest #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
josephrocca opened this issue Feb 12, 2025 · 4 comments
Open
Assignees
Labels
browser Not working in Browser

Comments

@josephrocca
Copy link

This works:

await import('https://esm.sh/[email protected]?pin=v135');

But this doesn't:

await import('https://esm.sh/[email protected]');

Here's the error (tested in Chrome latest):

encode.js:1229 Uncaught ReferenceError: __Buffer$ is not defined

I think the latest version might be trying to smartly remove the (IIUC) un-needed Buffer dependency (which would be awesome because it does bloat the lib a lot), but it looks like there might be a bug with that aspect of the esm.sh bundler?

@josephrocca josephrocca added the browser Not working in Browser label Feb 12, 2025
@ije
Copy link
Member

ije commented Feb 12, 2025

sorry for the break change, i will look into it!

@ije ije self-assigned this Feb 12, 2025
@ije
Copy link
Member

ije commented Feb 13, 2025

i found cbor-x delcares a browser field in it's package.json:

{
  "browser": {
    "node:buffer": false
  }
}

https://github.com/kriszyp/cbor-x/blob/f5313831fd1ea2c9407a5d25b56f379244919fc5/package.json#L74-L76

with that setting, esm.sh would not add Buffer polyfill for browser imports (this future is added in v136)

@ije
Copy link
Member

ije commented Feb 13, 2025

#1079 replaces __Buffer$ to globalThis.Buffer, however you still need to add Buffer poyfill for browser usage, for example:

import { Buffer } from "https://esm.sh/node/buffer.mjs"
globalThis.Buffer = Buffer

await import('https://esm.sh/[email protected]');

@josephrocca
Copy link
Author

@ije cbor-x doesn't require Buffer when used in the browser - e.g. for the cbor-x official index.min.js: https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js it's 33k instead of ~60k since it doesn't need Buffer in the browser.

I think this

const Buffer = typeof globalThis === 'object' && globalThis.Buffer;

is getting incorrectly(?) minified into this:

var Ie=typeof globalThis=="object"&&__Buffer$

when it should be getting minified into this:

var Ie=typeof globalThis=="object"&&globalThis.__Buffer$

Because then the check works properly instead of throwing an error, IIUC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
browser Not working in Browser
Projects
None yet
Development

No branches or pull requests

2 participants