Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds
module
andexports
topackage.json
.The files referenced are generated using
esbuild
.What this solves
A lot of new, modern, environments require packages to start shipping ESM.
For example when attempting to use
@sanity/client
in a Remix site will yield an error like the one below:This is from
same-origin
, which is included byget-it
, which is used by@sanity/client
.To solve this there's a new build command
npm run esbuild:browser
that transforms CJS to ESM and includes them in the bundle. This solves the problem, for now, buying us time to migrate these packages to ESM:Once they are shipping ESM we can stop prebundling them:
How to test
A test version is published under
npm i @sanity/client@esm
.Using unpkg.com
Run this in your console (not on github as it blocks external scripts, but for example on https://sanity.io or a localhost):
Using skypack.dev
./dist/sanityClient.browser.mjs
This file is generated to run in ESM strict envs, like deno, native browser ESM using CDNs like unpkg or skypack. To do this it bundles everything (no
import
orrequire
calls left) and it applies ourpkg.browser
field making sure to loadbrowserMiddleware.js
instead ofnodeMiddleware.js
../dist/sanityClient.node.cjs
Generated to run in allows calling
require
, using.cjs
to make it clear to modern 'node' that it's a CommonJS file.Once we have a bundler that implements module.createRequire we could consider adding a
./dist/sanityClient.node.mjs
version.pkg.module
This only affects bundlers that are modern enough to understand what it does, and does not trigger modern node to run in ESM module (unlike
pkg.type: "module"
). It points todist/sanityClient.browser.mjs
instead ofdist/sanityClient.node.js
as it has the highest chance of bundling success sincepkg.browser
is already applied to it.adding
package-check
toposttest
Runs @skypack/package-check to validate we're following ESM best practices.