Skip to content

build(polyfills): Remove output format specific logic #9467

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

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"build:transpile": {
"inputs": ["production", "^production"],
"dependsOn": ["^build:transpile:uncached", "^build:transpile", "build:transpile:uncached"],
"dependsOn": ["^build:transpile"],
"outputs": ["{projectRoot}/build/npm", "{projectRoot}/build/esm", "{projectRoot}/build/cjs"]
},
"build:types": {
Expand Down
1 change: 0 additions & 1 deletion packages/angular-ivy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"build:transpile": {
"dependsOn": [
"^build:transpile",
"^build:transpile:uncached",
"^build:types"
]
}
Expand Down
1 change: 0 additions & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"build:transpile": {
"dependsOn": [
"^build:transpile",
"^build:transpile:uncached",
"^build:types"
]
}
Expand Down
1 change: 0 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"build": "run-p build:transpile build:types",
"build:dev": "yarn build",
"build:transpile": "yarn ts-node scripts/buildRollup.ts",
"build:transpile:uncached": "yarn ts-node scripts/buildRollup.ts",
"build:types": "run-s build:types:core build:types:downlevel",
"build:types:core": "tsc -p tsconfig.types.json",
"build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8",
Expand Down
8 changes: 1 addition & 7 deletions packages/utils/rollup.npm.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';

export default makeNPMConfigVariants(
makeBaseNPMConfig({
// We build the polyfills separately because they're not included in the top-level exports of the package, in order
// to keep them out of the public API.
entrypoints: ['src/index.ts', 'src/buildPolyfills/index.ts'],
}),
);
export default makeNPMConfigVariants(makeBaseNPMConfig());
14 changes: 0 additions & 14 deletions packages/utils/scripts/buildRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,3 @@ run('yarn rollup -c rollup.npm.config.js');
// We want to distribute the README because it contains the MIT license blurb from Sucrase and Rollup
fs.copyFileSync('src/buildPolyfills/README.md', 'build/cjs/buildPolyfills/README.md');
fs.copyFileSync('src/buildPolyfills/README.md', 'build/esm/buildPolyfills/README.md');

// Because we import our polyfills from `@sentry/utils/cjs/buildPolyfills` and `@sentry/utils/esm/buildPolyfills` rather
// than straight from `@sentry/utils` (so as to avoid having them in the package's public API), when tests run, they'll
// expect to find `cjs` and `esm` at the root level of the repo.
try {
fs.symlinkSync('build/cjs', 'cjs');
} catch (oO) {
// if we get here, it's because the symlink already exists, so we're good
}
try {
fs.symlinkSync('build/esm', 'esm');
} catch (oO) {
// same as above
}
2 changes: 1 addition & 1 deletion packages/utils/src/buildPolyfills/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Build Polyfills

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.)
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.

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.

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './cache';
export * from './eventbuilder';
export * from './anr';
export * from './lru';
export * from './buildPolyfills';
11 changes: 8 additions & 3 deletions rollup/plugins/extractPolyfillsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export function makeExtractPolyfillsPlugin() {
return null;
}

// The index.js file of the tuils package will include identifiers named after polyfills so we would inject the
// polyfills, however that would override the exports so we should just skip that file.
const isUtilsPackage = process.cwd().endsWith('packages/utils');
if (isUtilsPackage && sourceFile === 'index.js') {
return null;
}

const parserOptions = {
sourceFileName: sourceFile,
// We supply a custom parser which wraps the provided `acorn` parser in order to override the `ecmaVersion` value.
Expand Down Expand Up @@ -187,9 +194,7 @@ function createImportOrRequireNode(polyfillNodes, currentSourceFile, moduleForma
// relative
const isUtilsPackage = process.cwd().endsWith('packages/utils');
const importSource = literal(
isUtilsPackage
? `./${path.relative(path.dirname(currentSourceFile), 'buildPolyfills')}`
: `@sentry/utils/${moduleFormat}/buildPolyfills`,
isUtilsPackage ? `./${path.relative(path.dirname(currentSourceFile), 'buildPolyfills')}` : '@sentry/utils',
);

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