Skip to content

Commit

Permalink
fix: improve ESM output to support Deno, Bun and Edge runtimes (#29)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Expanding ESM support is a significant change. Although a tremendous effort is made to preserve backward compatibility it can't be guaranteed as there are too many conditions, environments, and runtime versions to cover them all.
  • Loading branch information
stipsan committed Dec 25, 2022
1 parent d048261 commit 5ef19d4
Show file tree
Hide file tree
Showing 42 changed files with 4,701 additions and 11,800 deletions.
1 change: 1 addition & 0 deletions .babelrc
Expand Up @@ -9,6 +9,7 @@
}
]
],
"plugins": ["add-module-exports", "@babel/plugin-transform-object-assign","@babel/plugin-transform-modules-commonjs"],
"env": {
"test": {
"plugins": ["istanbul"]
Expand Down
67 changes: 35 additions & 32 deletions .github/workflows/ci.yml
Expand Up @@ -32,6 +32,7 @@ jobs:
with:
name: build-output
path: |
dist/
lib/
umd/
Expand All @@ -51,46 +52,48 @@ jobs:
- if: steps.prod-deps.outputs.cache-hit != 'true'
run: npm install --omit=dev --ignore-scripts

edge-runtime:
# Disabled until the jest runtime works with ESM
# edge-runtime:
# runs-on: ubuntu-latest
# needs: [build, prod-deps]
# steps:
# - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3
# - uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # tag=v3
# with:
# node-version: lts/*
# cache: 'npm'
# cache-dependency-path: runtimes/edge/package-lock.json
# - uses: actions/cache@1c73980b09e7aea7201f325a7aa3ad00beddcdda # tag=v3
# with:
# path: ./node_modules
# key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
# - uses: actions/download-artifact@9782bd6a9848b53b110e712e20e42d89988822b7 # tag=v3
# with:
# name: build-output
# - run: npm ci
# working-directory: runtimes/edge
# - run: npm test
# working-directory: runtimes/edge

deno-runtime:
runs-on: ubuntu-latest
needs: [build, prod-deps]
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3
- uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # tag=v3
with:
node-version: lts/*
cache: 'npm'
cache-dependency-path: runtimes/edge/package-lock.json
- uses: actions/cache@1c73980b09e7aea7201f325a7aa3ad00beddcdda # tag=v3
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ./node_modules
key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- uses: actions/download-artifact@9782bd6a9848b53b110e712e20e42d89988822b7 # tag=v3
- uses: actions/download-artifact@v3
with:
name: build-output
- run: npm ci
working-directory: runtimes/edge
- run: npm test
working-directory: runtimes/edge

## Temporarily disabled until we can get all downstream dependencies working with deno
# deno-runtime:
# runs-on: ubuntu-latest
# needs: [build, prod-deps]
# steps:
# - uses: actions/checkout@v3
# - uses: actions/cache@v3
# with:
# path: ./node_modules
# key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
# - uses: denoland/setup-deno@v1
# - name: deno test
# run: |
# deno fmt --check
# deno lint
# deno check *.ts
# deno test --allow-net
# working-directory: runtimes/deno
- uses: denoland/setup-deno@v1
- name: deno test
run: |
deno fmt --check
deno lint
deno task test
working-directory: runtimes/deno

bun-runtime:
runs-on: ubuntu-latest
Expand Down
Binary file modified bun.lockb
Binary file not shown.
7 changes: 7 additions & 0 deletions index.js
@@ -0,0 +1,7 @@
// The EdgeRuntime check is to support dead-code elimination on Vercel Edge Functions:
// https://edge-runtime.vercel.sh/features/available-apis#addressing-the-runtime
if (typeof EdgeRuntime === 'string') {
module.exports = require('./dist/sanityClient.browser.mjs')
} else {
module.exports = require('./lib/sanityClient.js')
}

0 comments on commit 5ef19d4

Please sign in to comment.