8000 feat(build): Core packages into single output files (#11030) · jonator/sentry-javascript@140f017 · GitHub
[go: up one dir, main page]

Skip to content

Commit 140f017

Browse files
authored
feat(build): Core packages into single output files (getsentry#11030)
Before making the changes in this PR, the following code: ```ts console.time('import sdk'); const pkg = await import('@sentry/nextjs'); console.assert(pkg != undefined); console.timeEnd('import sdk'); ``` Outputs around 75ms with node v18.17.0. After the changes in this PR, the time came down to around 55ms, so around a 30% improvement for Node.js loading. I suspect there will be greater improvements for bundlers but I've not tested that yet.
1 parent 2a1a036 commit 140f017

File tree

9 files changed

+114
-41
lines changed

9 files changed

+114
-41
lines changed

packages/browser/rollup.npm.config.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@ export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
// packages with bundles have a different build directory structure
66
hasBundles: true,
7+
packageSpecificConfig: {
8+
output: {
9+
// set exports to 'named' or 'auto' so that rollup doesn't warn
10+
exports: 'named',
11+
// set preserveModules to false because we want to bundle everything into one file.
12+
preserveModules: false,
13+
},
14+
},
715
}),
816
);

packages/core/rollup.npm.config.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
packageSpecificConfig: {
6+
output: {
7+
// set exports to 'named' or 'auto' so that rollup doesn't warn
8+
exports: 'named',
9+
// set preserveModules to false because we want to bundle everything into one file.
10+
preserveModules: false,
11+
},
12+
},
13+
}),
14+
);
Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
import { makeBaseBundleConfig } from '@sentry-internal/rollup-utils';
22

3-
function createAnrWorkerConfig(destDir, esm) {
4-
return makeBaseBundleConfig({
5-
bundleType: 'node-worker',
6-
entrypoints: ['src/integrations/anr/worker.ts'],
7-
licenseTitle: '@sentry/node',
8-
outputFileBase: () => 'worker-script.js',
9-
packageSpecificConfig: {
10-
output: {
11-
dir: destDir,
12-
sourcemap: false,
13-
},
14-
plugins: [
15-
{
16-
name: 'output-base64-worker-script',
17-
renderChunk(code) {
18-
const base64Code = Buffer.from(code).toString('base64');
19-
if (esm) {
20-
return `export const base64WorkerScript = '${base64Code}';`;
21-
} else {
22-
return `exports.base64WorkerScript = '${base64Code}';`;
23-
}
24-
},
3+
export function createAnrWorkerCode() {
4+
let base64Code;
5+
6+
return {
7+
workerRollupConfig: makeBaseBundleConfig({
8+
bundleType: 'node-worker',
9+
entrypoints: ['src/integrations/anr/worker.ts'],
10+
licenseTitle: '@sentry/node',
11+
outputFileBase: () => 'worker-script.js',
12+
packageSpecificConfig: {
13+
output: {
14+
dir: 'build/esm/integrations/anr',
15+
sourcemap: false,
2516
},
26-
],
17+
plugins: [
18+
{
19+
name: 'output-base64-worker-script',
20+
renderChunk(code) {
21+
base64Code = Buffer.from(code).toString('base64');
22+
},
23+
},
24+
],
25+
},
26+
}),
27+
getBase64Code() {
28+
return base64Code;
2729
},
28-
});
30+
};
2931
}
30-
31-
export const anrWorkerConfigs = [
32-
createAnrWorkerConfig('build/esm/integrations/anr', true),
33-
createAnrWorkerConfig('build/cjs/integrations/anr', false),
34-
];
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1+
import replace from '@rollup/plugin-replace';
12
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
2-
import { anrWorkerConfigs } from './rollup.anr-worker.config.mjs';
3+
import { createAnrWorkerCode } from './rollup.anr-worker.config.mjs';
4+
5+
const { workerRollupConfig, getBase64Code } = createAnrWorkerCode();
36

47
export default [
5-
...makeNPMConfigVariants(makeBaseNPMConfig()),
6-
// The ANR worker builds must come after the main build because they overwrite the worker-script.js file
7-
...anrWorkerConfigs,
8+
// The worker needs to be built first since it's output is used in the main bundle.
9+
workerRollupConfig,
10+
...makeNPMConfigVariants(
11+
makeBaseNPMConfig({
12+
packageSpecificConfig: {
13+
output: {
14+
// set exports to 'named' or 'auto' so that rollup doesn't warn
15+
exports: 'named',
16+
// set preserveModules to false because we want to bundle everything into one file.
17+
preserveModules: false,
18+
},
19+
plugins: [
20+
replace({
21+
delimiters: ['###', '###'],
22+
// removes some webpack warnings
23+
preventAssignment: true,
24+
values: {
25+
base64WorkerScript: () => getBase64Code(),
26+
},
27+
}),
28+
],
29+
},
30+
}),
31+
),
832
];
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// This file is a placeholder that gets overwritten in the build directory.
2-
export const base64WorkerScript = '';
1+
// This string is a placeholder that gets overwritten with the worker code.
2+
export const base64WorkerScript = '###base64WorkerScript###';
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
packageSpecificConfig: {
6+
output: {
7+
// set exports to 'named' or 'auto' so that rollup doesn't warn
8+
exports: 'named',
9+
// set preserveModules to false because we want to bundle everything into one file.
10+
preserveModules: false,
11+
},
12+
},
13+
}),
14+
);
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
packageSpecificConfig: {
6+
output: {
7+
// set exports to 'named' or 'auto' so that rollup doesn't warn
8+
exports: 'named',
9+
// set preserveModules to false because we want to bundle everything into one file.
10+
preserveModules: false,
11+
},
12+
},
13+
}),
14+
);

packages/utils/rollup.npm.config.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
packageSpecificConfig: {
6+
output: {
7+
// set exports to 'named' or 'auto' so that rollup doesn't warn
8+
exports: 'named',
9+
// set preserveModules to false because we want to bundle everything into one file.
10+
preserveModules: false,
11+
},
12+
},
13+
}),
14+
);

packages/utils/scripts/buildRollup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ function run(cmd: string, options?: childProcess.ExecSyncOptions): string | Buff
1212
run('yarn rollup -c rollup.npm.config.mjs');
1313

1414
// We want to distribute the README because it contains the MIT license blurb from Sucrase and Rollup
15-
fs.copyFileSync('src/buildPolyfills/README.md', 'build/cjs/buildPolyfills/README.md');
16-
fs.copyFileSync('src/buildPolyfills/README.md', 'build/esm/buildPolyfills/README.md');
15+
fs.copyFileSync('src/buildPolyfills/README.md', 'build/cjs/build-polyfills-license.md');
16+
fs.copyFileSync('src/buildPolyfills/README.md', 'build/esm/build-polyfills-license.md');

0 commit comments

Comments
 (0)
0