ai / nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
https://zelark.github.io/nano-id-cc/
MIT License
24.61k stars 790 forks source link

Fix CJS compatibility #490

Closed FurryR closed 1 month ago

FurryR commented 2 months ago

Proposed Changes

Ensure compatibility with some CDNs & bundlers on some CJS libraries.

jsDelivr translates const { nanoid } = require('nanoid') directly into import e from 'nanoid/+esm'; const { nanoid } = e, which is problematic and causes some libraries fail to load.

Here is an example using postcss:

await import('https://cdn.jsdelivr.net/npm/postcss@8.4.41/+esm')

Additional Information

It is suggested to squash merge this PR.

ai commented 2 months ago
  1. We also have url-alphabet
  2. Please run eslint --fix .
  3. I really worry that it could break something else. Can you test that this method doesn’t break Vite and webpack (you can add version from your branch by npm install --save-dev nanoid@FurryR/nanoid
FurryR commented 2 months ago
  1. We also have url-alphabet
  2. Please run eslint --fix .
  3. I really worry that it could break something else. Can you test that this method doesn’t break Vite and webpack (you can add version from your branch by npm install --save-dev nanoid@FurryR/nanoid
  1. Sorry that I considered url-alphabet a private file. I will add default export for it later.
  2. okay I will do that later
  3. It seemed that adding a default export is not a breaking change at all, but I am unsure if the type description files (.d.ts) need to be updated. Anyway, I will take a look.
FurryR commented 2 months ago

Tested with pnpm start. This PR does not break Vite.

ai commented 2 months ago

const { nanoid } = require('nanoid')

But Nano ID 4.x and 5.x don’t support require(). If you are using CJS you need 3.x version. We have a special branch for it.

ferreiro commented 1 month ago

@ai any way you can guys ship this? I was trying using nanoId with typescript the problem is that when I transpile and push the code to google app engine, it says that the require() is not supported. Maybe you have a workaround instead of needing this PR?

ai commented 1 month ago

@ferreiro as I told I need some investigation that it will not break in many possible cases where Nano ID is used.

If you need it, please help to the community.

FurryR commented 1 month ago

const { nanoid } = require('nanoid')

But Nano ID 4.x and 5.x don’t support require(). If you are using CJS you need 3.x version. We have a special branch for it.

Hmm. weird. If you access https://cdn.jsdelivr.net/npm/nanoid@3.3.7/non-secure/+esm you will see.

FurryR commented 1 month ago

Anyway, I will close this PR and wait for a better workaround. Thank you all guys.