Skip to content

Commit 7f2e804

Browse files
authored
feat: Make ESM output valid Node ESM (#10928)
Some bundlers, especially older ones, don't support resolving `.mjs` files out of the box. To retain compatibility with these bundlers without requiring users to adjust their configurations, we need to keep all our ESM output as `.js` files. So node.js loads these as modules, we output a `package.json` into each `./build/esm` output directory containing simply `{ "type": "module" }`. This works because whenever node.js is asked to load a `.js` file, it tries to work out whether this is CJS or ESM by searching up through parent directories until it finds a `package.json` with the `type` set. This PR: - Adds a Rollup plugin that injects the `package.json` into the ESM output - Adds the package `exports` that @AbhiPrasad worked out for #10833 - Fixes an import issue with `next/router` which is CJS (at least in v10) - Fixes an import issue with `@prisma/instrumentation` which is CJS - Ensures that CJS only integrations are not included in the `@sentry/node` default integrations when running as ESM This PR also makes some unrelated changes: - Changes to the old Node SDKs to allow the tests to pass - Removes the bundle size analysis CI job as it doesn't appears to be compatible with the node ESM output
1 parent 8d0b779 commit 7f2e804

File tree

44 files changed

+373
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+373
-64
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -279,39 +279,6 @@ jobs:
279279
# `job_build` can't see `job_install_deps` and what it returned)
280280
dependency_cache_key: ${{ needs.job_install_deps.outputs.dependency_cache_key }}
281281

282-
job_size_check:
283-
name: Size Check
284-
needs: [job_get_metadata, job_build]
285-
timeout-minutes: 15
286-
runs-on: ubuntu-20.04
287-
if:
288-
github.event_name == 'pull_request' || needs.job_get_metadata.outputs.is_develop == 'true' ||
289-
needs.job_get_metadata.outputs.is_release == 'true'
290-
steps:
291-
- name: Check out current commit (${{ needs.job_get_metadata.outputs.commit_label }})
292-
uses: actions/checkout@v4
293-
with:
294-
ref: ${{ env.HEAD_COMMIT }}
295-
- name: Set up Node
296-
uses: actions/setup-node@v4
297-
with:
298-
# The size limit action runs `yarn` and `yarn build` when this job is executed on
299-
# use Node 14 for now.
300-
node-version: '14'
301-
- name: Restore caches
302-
uses: ./.github/actions/restore-cache
303-
env:
304-
DEPENDENCY_CACHE_KEY: ${{ needs.job_build.outputs.dependency_cache_key }}
305-
- name: Check bundle sizes
306-
uses: getsentry/size-limit-action@runForBranch
307-
with:
308-
github_token: ${{ secrets.GITHUB_TOKEN }}
309-
skip_step: build
310-
main_branch: develop
311-
# When on release branch, we want to always run
312-
# Else, we fall back to the default handling of the action
313-
run_for_branch: ${{ (needs.job_get_metadata.outputs.is_release == 'true' && 'true') || '' }}
314-
315282
job_lint:
316283
name: Lint
317284
# Even though the linter only checks source code, not built code, it needs the built code in order check that all

dev-packages/rollup-utils/npmHelpers.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
makeSetSDKSourcePlugin,
1818
makeSucrasePlugin,
1919
} from './plugins/index.mjs';
20+
import { makePackageNodeEsm } from './plugins/make-esm-plugin.mjs';
2021
import { mergePlugins } from './utils.mjs';
2122

2223
const packageDotJSON = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), './package.json'), { encoding: 'utf8' }));
@@ -104,6 +105,7 @@ export function makeBaseNPMConfig(options = {}) {
104105
...builtinModules,
105106
...Object.keys(packageDotJSON.dependencies || {}),
106107
...Object.keys(packageDotJSON.peerDependencies || {}),
108+
...Object.keys(packageDotJSON.optionalDependencies || {}),
107109
],
108110
};
109111

@@ -120,7 +122,7 @@ export function makeBaseNPMConfig(options = {}) {
120122
export function makeNPMConfigVariants(baseConfig) {
121123
const variantSpecificConfigs = [
122124
{ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } },
123-
{ output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm') } },
125+
{ output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm'), plugins: [makePackageNodeEsm()] } },
124126
];
125127

126128
return variantSpecificConfigs.map(variant => deepMerge(baseConfig, variant));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Outputs a package.json file with {type: module} in the root of the output directory so that Node
3+
* treats .js files as ESM.
4+
*/
5+
export function makePackageNodeEsm() {
6+
return {
7+
name: 'make-package-node-esm',
8+
generateBundle() {
9+
this.emitFile({
10+
type: 'asset',
11+
fileName: 'package.json',
12+
source: '{ "type": "module" }',
13+
});
14+
},
15+
};
16+
}

packages/browser/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
"main": "build/npm/cjs/index.js",
1919
"module": "build/npm/esm/index.js",
2020
"types": "build/npm/types/index.d.ts",
21+
"exports": {
22+
"./package.json": "./package.json",
23+
".": {
24+
"import": {
25+
"types": "./build/npm/types/index.d.ts",
26+
"default": "./build/npm/esm/index.js"
27+
},
28+
"require": {
29+
"types": "./build/npm.types/index.d.ts",
30+
"default": "./build/npm/cjs/index.js"
31+
}
32+
}
33+
},
2134
"typesVersions": {
2235
"<4.9": {
2336
"build/npm/types/index.d.ts": [

packages/bun/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
"main": "build/esm/index.js",
1919
"module": "build/esm/index.js",
2020
"types": "build/types/index.d.ts",
21+
"exports": {
22+
"./package.json": "./package.json",
23+
".": {
24+
"import": {
25+
"types": "./build/types/index.d.ts",
26+
"default": "./build/esm/index.js"
27+
},
28+
"require": {
29+
"types": "./build/types/index.d.ts",
30+
"default": "./build/cjs/index.js"
31+
}
32+
}
33+
},
2134
"typesVersions": {
2235
"<4.9": {
2336
"build/npm/types/index.d.ts": [

packages/core/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
"main": "build/cjs/index.js",
1919
"module": "build/esm/index.js",
2020
"types": "build/types/index.d.ts",
21+
"exports": {
22+
"./package.json": "./package.json",
23+
".": {
24+
"import": {
25+
"types": "./build/types/index.d.ts",
26+
"default": "./build/esm/index.js"
27+
},
28+
"require": {
29+
"types": "./build/types/index.d.ts",
30+
"default": "./build/cjs/index.js"
31+
}
32+
}
33+
},
2134
"typesVersions": {
2235
"<4.9": {
2336
"build/types/index.d.ts": [

packages/deno/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
"license": "MIT",
99
"module": "build/index.mjs",
1010
"types": "build/index.d.ts",
11+
"exports": {
12+
"./package.json": "./package.json",
13+
".": {
14+
"import": {
15+
"types": "./build/index.d.ts",
16+
"default": "./build/index.mjs"
17+
}
18+
}
19+
},
1120
"publishConfig": {
1221
"access": "public"
1322
},

packages/feedback/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
"main": "build/npm/cjs/index.js",
1919
"module": "build/npm/esm/index.js",
2020
"types": "build/npm/types/index.d.ts",
21+
"exports": {
22+
"./package.json": "./package.json",
23+
".": {
24+
"import": {
25+
"types": "./build/npm/types/index.d.ts",
26+
"default": "./build/npm/esm/index.js"
27+
},
28+
"require": {
29+
"types": "./build/npm/types/index.d.ts",
30+
"default": "./build/npm/cjs/index.js"
31+
}
32+
}
33+
},
2134
"typesVersions": {
2235
"<4.9": {
2336
"build/npm/types/index.d.ts": [

packages/gatsby/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
"main": "build/cjs/index.js",
2727
"module": "build/esm/index.js",
2828
"types": "build/types/index.d.ts",
29+
"exports": {
30+
"./package.json": "./package.json",
31+
".": {
32+
"import": {
33+
"types": "./build/types/index.d.ts",
34+
"default": "./build/esm/index.js"
35+
},
36+
"require": {
37+
"types": "./build/types/index.d.ts",
38+
"default": "./build/cjs/index.js"
39+
}
40+
}
41+
},
2942
"typesVersions": {
3043
"<4.9": {
3144
"build/types/index.d.ts": [

packages/integration-shims/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
"main": "build/cjs/index.js",
66
"module": "build/esm/index.js",
77
"types": "build/types/index.d.ts",
8+
"exports": {
9+
"./package.json": "./package.json",
10+
".": {
11+
"import": {
12+
"types": "./build/types/index.d.ts",
13+
"default": "./build/esm/index.js"
14+
},
15+
"require": {
16+
"types": "./build/types/index.d.ts",
17+
"default": "./build/cjs/index.js"
18+
}
19+
}
20+
},
821
"typesVersions": {
922
"<4.9": {
1023
"build/types/index.d.ts": [

0 commit comments

Comments
 (0)