Skip to content

Commit 86c8d2a

Browse files
authored
build(polyfills): Remove output format specific logic (#9467)
1 parent 9a92e6b commit 86c8d2a

File tree

9 files changed

+12
-29
lines changed

9 files changed

+12
-29
lines changed

nx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"build:transpile": {
2828
"inputs": ["production", "^production"],
29-
"dependsOn": ["^build:transpile:uncached", "^build:transpile", "build:transpile:uncached"],
29+
"dependsOn": ["^build:transpile"],
3030
"outputs": ["{projectRoot}/build/npm", "{projectRoot}/build/esm", "{projectRoot}/build/cjs"]
3131
},
3232
"build:types": {

packages/angular-ivy/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"build:transpile": {
6868
"dependsOn": [
6969
"^build:transpile",
70-
"^build:transpile:uncached",
7170
"^build:types"
7271
]
7372
}

packages/angular/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"build:transpile": {
7272
"dependsOn": [
7373
"^build:transpile",
74-
"^build:transpile:uncached",
7574
"^build:types"
7675
]
7776
}

packages/utils/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"build": "run-p build:transpile build:types",
3535
"build:dev": "yarn build",
3636
"build:transpile": "yarn ts-node scripts/buildRollup.ts",
37-
"build:transpile:uncached": "yarn ts-node scripts/buildRollup.ts",
3837
"build:types": "run-s build:types:core build:types:downlevel",
3938
"build:types:core": "tsc -p tsconfig.types.json",
4039
"build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8",

packages/utils/rollup.npm.config.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

3-
export default makeNPMConfigVariants(
4-
makeBaseNPMConfig({
5-
// We build the polyfills separately because they're not included in the top-level exports of the package, in order
6-
// to keep them out of the public API.
7-
entrypoints: ['src/index.ts', 'src/buildPolyfills/index.ts'],
8-
}),
9-
);
3+
export default makeNPMConfigVariants(makeBaseNPMConfig());

packages/utils/scripts/buildRollup.ts

-14
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,3 @@ run('yarn rollup -c rollup.npm.config.js');
1414
// We want to distribute the README because it contains the MIT license blurb from Sucrase and Rollup
1515
fs.copyFileSync('src/buildPolyfills/README.md', 'build/cjs/buildPolyfills/README.md');
1616
fs.copyFileSync('src/buildPolyfills/README.md', 'build/esm/buildPolyfills/README.md');
17-
18-
// Because we import our polyfills from `@sentry/utils/cjs/buildPolyfills` and `@sentry/utils/esm/buildPolyfills` rather
19-
// than straight from `@sentry/utils` (so as to avoid having them in the package's public API), when tests run, they'll
20-
// expect to find `cjs` and `esm` at the root level of the repo.
21-
try {
22-
fs.symlinkSync('build/cjs', 'cjs');
23-
} catch (oO) {
24-
// if we get here, it's because the symlink already exists, so we're good
25-
}
26-
try {
27-
fs.symlinkSync('build/esm', 'esm');
28-
} catch (oO) {
29-
// same as above
30-
}

packages/utils/src/buildPolyfills/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Build Polyfills
22

3-
This is a collection of syntax and import/export polyfills either copied directly from or heavily inspired by those used by [Rollup](https://github.com/rollup/rollup) and [Sucrase](https://github.com/alangpierce/sucrase). When either tool uses one of these polyfills during a build, it injects the function source code into each file needing the function, which can lead to a great deal of duplication. For our builds, we have therefore implemented something similar to [`tsc`'s `importHelpers` behavior](https://www.typescriptlang.org/tsconfig#importHelpers): Instead of leaving the polyfills injected in multiple places, we instead replace each injected function with an `import` or `require` statement, pulling from the CJS or ESM builds as appropriate. (In other words, the injected `import` statements import from `@sentry/utils/esm/buildPolyfills` and the injected `require` statements pull from `@sentry/utils/cjs/buildPolyfills/`. Because these functions should never be part of the public API, they're not exported from the package directly.)
3+
This is a collection of syntax and import/export polyfills either copied directly from or heavily inspired by those used by [Rollup](https://github.com/rollup/rollup) and [Sucrase](https://github.com/alangpierce/sucrase). When either tool uses one of these polyfills during a build, it injects the function source code into each file needing the function, which can lead to a great deal of duplication. For our builds, we have therefore implemented something similar to [`tsc`'s `importHelpers` behavior](https://www.typescriptlang.org/tsconfig#importHelpers): Instead of leaving the polyfills injected in multiple places, we instead replace each injected function with an `import` or `require` statement.
44

55
Note that not all polyfills are currently used by the SDK, but all are included here for future compatitibility, should they ever be needed. Also, since we're never going to be calling these directly from within another TS file, their types are fairly generic. In some cases testing required more specific types, which can be found in the test files.
66

packages/utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export * from './cache';
3434
export * from './eventbuilder';
3535
export * from './anr';
3636
export * from './lru';
37+
export * from './buildPolyfills';

rollup/plugins/extractPolyfillsPlugin.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export function makeExtractPolyfillsPlugin() {
4040
return null;
4141
}
4242

43+
// The index.js file of the tuils package will include identifiers named after polyfills so we would inject the
44+
// polyfills, however that would override the exports so we should just skip that file.
45+
const isUtilsPackage = process.cwd().endsWith('packages/utils');
46+
if (isUtilsPackage && sourceFile === 'index.js') {
47+
return null;
48+
}
49+
4350
const parserOptions = {
4451
sourceFileName: sourceFile,
4552
// We supply a custom parser which wraps the provided `acorn` parser in order to override the `ecmaVersion` value.
@@ -187,9 +194,7 @@ function createImportOrRequireNode(polyfillNodes, currentSourceFile, moduleForma
187194
// relative
188195
const isUtilsPackage = process.cwd().endsWith('packages/utils');
189196
const importSource = literal(
190-
isUtilsPackage
191-
? `./${path.relative(path.dirname(currentSourceFile), 'buildPolyfills')}`
192-
: `@sentry/utils/${moduleFormat}/buildPolyfills`,
197+
isUtilsPackage ? `./${path.relative(path.dirname(currentSourceFile), 'buildPolyfills')}` : '@sentry/utils',
193198
);
194199

195200
// This is the `x, y, z` of inside of `import { x, y, z }` or `var { x, y, z }`

0 commit comments

Comments
 (0)