Skip to content
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

feat: ESM support #25

Merged
merged 39 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
18c8db1
fix: mark the module as CJS for modern tooling
stipsan Aug 25, 2022
f0eae77
fix: add pkg.exports in a bc compatible way
stipsan Aug 25, 2022
44d2930
chore: setup CI and semantic-release
stipsan Aug 25, 2022
14bc2f0
chore: add deno test
stipsan Aug 25, 2022
7179bf9
chore: fix ci
stipsan Aug 25, 2022
1a525d8
feat: add ESM output
stipsan Aug 25, 2022
60e5dd2
chore: testing a much easier setup
stipsan Aug 25, 2022
c1b15e8
feat: add ESM browser exports & tests
stipsan Aug 25, 2022
05eb0d2
chore: fix CI
stipsan Aug 25, 2022
7171cbb
chore: ready for code review
stipsan Aug 25, 2022
f756231
fix: add document check for deno
stipsan Aug 25, 2022
1bbb1f1
chore: bump node-fetch
stipsan Aug 25, 2022
f1385d5
chore: happy CI happy life
stipsan Aug 25, 2022
c4352d1
chore: I repeat... happy ci happy life
stipsan Aug 26, 2022
162a58f
fix: node v12 bug
stipsan Aug 26, 2022
52f7af1
chore: enable sourcemaps
stipsan Aug 26, 2022
d0cdd48
chore: remove empty references
stipsan Aug 26, 2022
b9b4e52
feat: add smart imports for middleware
stipsan Aug 26, 2022
aa27d71
fix: remove `process` globals from mjs
stipsan Aug 26, 2022
1c1329f
fix: better cdn support
stipsan Aug 26, 2022
5965edd
fix: get rid of `object-assign`
stipsan Aug 26, 2022
681f71b
chore: better externals perf
stipsan Aug 26, 2022
cbc57e7
chore: fix coverage
stipsan Aug 26, 2022
4947e9a
chore: cleanup
stipsan Aug 26, 2022
fa88d59
fix: ESM all the things
stipsan Aug 26, 2022
4c03ad1
chore: more ESM
stipsan Aug 26, 2022
27d517f
chore: a little less ESM for the sake of the tests
stipsan Aug 26, 2022
5d05384
chore: better fix for the mixed exports
stipsan Aug 26, 2022
5d93fd5
chore: simplify
stipsan Aug 26, 2022
9ddd737
chore: fix paths
stipsan Aug 26, 2022
348f2b9
chore: fix cjs build
stipsan Aug 26, 2022
bfb3476
chore: fix the last ESM tests
stipsan Aug 26, 2022
461fdf0
fix: don't ship keepAlive to browsers
stipsan Aug 26, 2022
56ab3ef
fix: handle deno oddity
stipsan Aug 26, 2022
6e7cdd4
fix: add support for vercel and bun
stipsan Aug 26, 2022
25cb0ff
fix: update magic imports
stipsan Aug 26, 2022
22eb36c
fix: support vercel edge runtime
stipsan Aug 26, 2022
4335768
chore: browserify says no
stipsan Aug 26, 2022
9263d4a
fix: finally, a verified working edge runtime setup
stipsan Aug 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"plugins": ["add-module-exports"],
"env": {
"browser": {
"presets": [
Expand All @@ -7,10 +8,11 @@
{
"targets": {
"browsers": ["ie >= 9"]
}
},
}
]
]
],
"plugins": ["@babel/plugin-transform-object-assign"]
},
"node": {
"presets": [
Expand All @@ -22,7 +24,21 @@
}
}
]
]
],
"plugins": ["@babel/plugin-transform-modules-commonjs"]
},
"test": {
"presets": [
[
"@babel/env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": ["@babel/plugin-transform-modules-commonjs", "istanbul"]
}
}
}
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/coverage
/dist
/lib
/lib-node
/umd
# ESLint isn't configured to deal with ESM yet
/test-esm
136 changes: 136 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
name: CI & Release

on:
# Build on pushes to release branches
push:
branches: [main]
# Build on pull requests targeting release branches
pull_request:
branches: [main]
# https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow
# https://github.com/sanity-io/semantic-release-preset/actions/workflows/ci.yml
workflow_dispatch:
inputs:
release:
description: 'Release new version'
required: true
default: false
type: boolean

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}-${{ github.event.inputs.release || 'no-release' }}
cancel-in-progress: true

jobs:
build:
name: Build, lint and test coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: npm
- run: npm ci
- run: npm run build
- run: npm run lint
- run: npm run coverage

test:
name: Node.js ${{ matrix.node }} on ${{ matrix.os }}
needs: build
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
# Test the oldest Node.js version we support, and the latest in development
node: [12, current]
include:
# ubuntu and lts already runs tests in the `build` job, so we only need to add windows and mac here
- os: macos-latest
node: lts/*
- os: windows-latest
node: lts/*
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm run build
- run: npm run coverage
env:
SKIP_MTLS_TEST: "${{ matrix.os == 'ubuntu-latest' && 'true' || 'false' }}"

test-esm:
name: Test ESM exports
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
# The testing suite uses the native test runner introduced in Node.js v18
# https://nodejs.org/api/test.html
node-version: 18
cache: npm
- run: npm ci
- run: npm run build
- run: npm run build:esm
- run: npm run test:esm
# This test will run both in a CJS and an ESM mode in Node.js to ensure backwards compatibility
name: Ensure pkg.exports don't break anything in modern Node.js envs
- run: npm run test:esm:browser
# This test is just ensuring the pkg.exports defined by 'browser' conditionals don't point to files that don't exist and have valid syntax (no CJS)
# Please note that this test DOES support Node.js APIs, we need to test in a Cloudflare Worker v8 runtime, or Vercel Edge Runtime, to fully test e2e
name: Test the 'browser' conditional using Node.js

test-deno:
name: Test Deno in Node compatibility mode
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: npm
- run: npm ci
- run: npm run build
- run: npm run build:esm
- uses: denoland/setup-deno@v1
- run: npm run test:esm:deno
# When not in Node Compat mode it's likely using something like https://esm.sh/get-it, which supports the same `deno` conditional
# and thus it's not necessary to run a test outside compat mode here
name: Test that pkg.exports supports Deno running in Node Compatibility mode

release:
name: Semantic release
runs-on: ubuntu-latest
needs: [test, test-esm, test-deno]
if: inputs.release == true
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: npm
- run: npm ci
# @TODO Runs with --dry-run initially, remove once it's confirmed it'll publish the right thing(tm)
- run: npx semantic-release --dry-run
# Don't allow interrupting the release step if the job is cancelled, as it can lead to an inconsistent state
# e.g. git tags were pushed but it exited before `npm publish`
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
# Should release fail, dry rerun with debug on for richer logs
- run: npx semantic-release --dry-run --debug
if: ${{ failure() }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pids
*.seed

# Built sources
/dist
/lib
/lib-node
/umd
Expand Down
12 changes: 0 additions & 12 deletions .npmignore

This file was deleted.

3 changes: 3 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@istanbuljs/nyc-config-babel"
}
4 changes: 4 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@sanity/semantic-release-preset",
"branches": ["main"]
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,10 @@ This module was inspired by the great work of others:
## License

MIT-licensed. See LICENSE.

## Release new version

Run the ["CI & Release" workflow](https://github.com/sanity-io/get-it/actions).
Make sure to select the main branch and check "Release new version".

Semantic release will only release on configured branches, so it is safe to run release on any branch.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
module.exports = require('./lib-node')
// 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/esm/index.mjs')
} else {
module.exports = require('./lib-node')
}
8 changes: 7 additions & 1 deletion middleware.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
module.exports = require('./lib/middleware')
// 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/esm/middleware/index.mjs')
} else {
module.exports = require('./lib/middleware')
}
Loading