-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Updated as of Sep 2022:
We are excited to announce a long-awaited feature - improved support for packages distributed as ES modules 🎉
It is currently in beta, which means we encourage you to try it out, but not use it in production applications yet. This issue will cover all technical details and can be also used to provide feedback or report bugs if you find any.
TL;DR
- Demo and essential information: https://www.jsdelivr.com/esm
- You can load any ESM file like this:
https://cdn.jsdelivr.net/npm/uuid/+esm(default file)https://cdn.jsdelivr.net/npm/uuid/dist/esm-browser/index.js/+esm(explicit file).
- There's a new domain that you can use for shorter and prettier links. It redirects everything to our main domain and is not meant to be used in production:
https://esm.run/uuidhttps://esm.run/uuid/dist/esm-browser/index.js
How this works
Locating entry points
To find the default file, we use exports, module, and jsnext:main fields from package.json. These fields are currently used by all existing tooling and by most package authors.
Handling imports
Resolving
- Absolute URLs like
https://example.com/fooare always kept in their original form. - Relative imports are first resolved via the
browserfield if it exists (this applies to the entry point as well) and then resolved using node's experimental resolution algorithm, which supports extension resolution and importing from directories that include an index file. The supported extensions are:.mjs,.js, and.json.
Bundling
-
Internal imports (pointing to files in the same package):
- are bundled with the requested file if they are static, e.g.,
import foo from './foo.js', - are rewritten to their jsDelivr URL if they are dynamic, e.g.,
import('./foo.js').
- are bundled with the requested file if they are static, e.g.,
-
External imports (pointing to files in other packages):
- are always rewritten to their jsDelivr URL, whether they are static or dynamic.
This means that one npm package roughly equals one bundle and one HTTP request, which allows good caching and doesn't require too many HTTP requests.
External URLs are always generated with fully resolved dependency versions based on package.json dependencies (or latest if the version is not specified there). This further improves caching and guarantees stable versions even for transitive dependencies.
Performance
- The generated files are automatically minified.
- Source maps are available for easy debugging.
- Once generated, the files are stored in our permanent storage and served from there.
- In supported browsers, HTTP preloading is used to start fetching external static dependencies as soon as possible.
What doesn't work (but may in the future)
Packages that are written in CJS, or a mix of ESM and CJS.CJS packages are now transformed to ESM.Packages that import core node.js modules.Most core node.js modules are now polyfilled.Packages that import files in formats other than JS/JSON.CSS is now supported too.- Packages larger than 50 MiB.
search on esm.run lists all packages. This will soon be changed to list only the supported ones.
The esm.run domain
This domain is introduced as an easier to remember and type alternative. It doesn't run on our multi-cdn architecture and simply redirects all requests to the main cdn domain, so it's not meant to be used in production.