Skip to content

Commit 139c65e

Browse files
committed
refactor value injection for turbopack
1 parent 07075dd commit 139c65e

File tree

5 files changed

+736
-209
lines changed

5 files changed

+736
-209
lines changed

packages/nextjs/src/config/turbopack/constructTurbopackConfig.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { debug } from '@sentry/core';
22
import * as chalk from 'chalk';
3-
import * as path from 'path';
43
import type { RouteManifest } from '../manifest/types';
5-
import type { NextConfigObject, TurbopackOptions, TurbopackRuleConfigItemOrShortcut } from '../types';
4+
import type { NextConfigObject, TurbopackMatcherWithRule, TurbopackOptions } from '../types';
5+
import { generateValueInjectionRules } from './generateValueInjectionRules';
66

77
/**
88
* Construct a Turbopack config object from a Next.js config object and a Turbopack options object.
@@ -24,48 +24,14 @@ export function constructTurbopackConfig({
2424
...userNextConfig.turbopack,
2525
};
2626

27-
const isomorphicValues = {
28-
_sentryNextJsVersion: nextJsVersion || '',
29-
};
30-
31-
const clientValues = {
32-
...isomorphicValues,
33-
_sentryRouteManifest: JSON.stringify(routeManifest),
34-
};
35-
36-
const serverValues = {
37-
...isomorphicValues,
38-
};
39-
40-
// Client value injection
41-
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
42-
matcher: '**/instrumentation-client.*',
43-
rule: {
44-
loaders: [
45-
{
46-
loader: path.resolve(__dirname, '..', 'loaders', 'valueInjectionLoader.js'),
47-
options: {
48-
...clientValues,
49-
},
50-
},
51-
],
52-
},
27+
const valueInjectionRules = generateValueInjectionRules({
28+
routeManifest,
29+
nextJsVersion,
5330
});
5431

55-
// Server value injection
56-
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
57-
matcher: '**/instrumentation.*',
58-
rule: {
59-
loaders: [
60-
{
61-
loader: path.resolve(__dirname, '..', 'loaders', 'valueInjectionLoader.js'),
62-
options: {
63-
...serverValues,
64-
},
65-
},
66-
],
67-
},
68-
});
32+
for (const { matcher, rule } of valueInjectionRules) {
33+
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, { matcher, rule });
34+
}
6935

7036
return newConfig;
7137
}
@@ -80,7 +46,7 @@ export function constructTurbopackConfig({
8046
*/
8147
export function safelyAddTurbopackRule(
8248
existingRules: TurbopackOptions['rules'],
83-
{ matcher, rule }: { matcher: string; rule: TurbopackRuleConfigItemOrShortcut },
49+
{ matcher, rule }: TurbopackMatcherWithRule,
8450
): TurbopackOptions['rules'] {
8551
if (!existingRules) {
8652
return {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as path from 'path';
2+
import type { RouteManifest } from '../manifest/types';
3+
import type { JSONValue, TurbopackMatcherWithRule } from '../types';
4+
5+
/**
6+
* Generate the value injection rules for client and server in turbopack config.
7+
*/
8+
export function generateValueInjectionRules({
9+
routeManifest,
10+
nextJsVersion,
11+
}: {
12+
routeManifest?: RouteManifest;
13+
nextJsVersion?: string;
14+
}): TurbopackMatcherWithRule[] {
15+
const rules: TurbopackMatcherWithRule[] = [];
16+
const isomorphicValues: Record<string, JSONValue> = {};
17+
let clientValues: Record<string, JSONValue> = {};
18+
let serverValues: Record<string, JSONValue> = {};
19+
20+
if (nextJsVersion) {
21+
isomorphicValues._sentryNextJsVersion = nextJsVersion;
22+
}
23+
24+
if (routeManifest) {
25+
serverValues._sentryRouteManifest = routeManifest;
26+
}
27+
28+
if (Object.keys(isomorphicValues).length > 0) {
29+
clientValues = { ...clientValues, ...isomorphicValues };
30+
serverValues = { ...serverValues, ...isomorphicValues };
31+
}
32+
33+
// Client value injection
34+
if (Object.keys(clientValues).length > 0) {
35+
rules.push({
36+
matcher: '**/instrumentation-client.*',
37+
rule: {
38+
loaders: [
39+
{
40+
loader: path.resolve(__dirname, '..', 'loaders', 'valueInjectionLoader.js'),
41+
options: {
42+
values: clientValues,
43+
},
44+
},
45+
],
46+
},
47+
});
48+
}
49+
50+
// Server value injection
51+
if (Object.keys(serverValues).length > 0) {
52+
rules.push({
53+
matcher: '**/instrumentation.*',
54+
rule: {
55+
loaders: [
56+
{
57+
loader: path.resolve(__dirname, '..', 'loaders', 'valueInjectionLoader.js'),
58+
options: {
59+
values: serverValues,
60+
},
61+
},
62+
],
63+
},
64+
});
65+
}
66+
67+
return rules;
68+
}

packages/nextjs/src/config/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ export type EnhancedGlobal = typeof GLOBAL_OBJ & {
621621
SENTRY_RELEASES?: { [key: string]: { id: string } };
622622
};
623623

624-
type JSONValue = string | number | boolean | JSONValue[] | { [k: string]: JSONValue };
624+
export type JSONValue = string | number | boolean | JSONValue[] | { [k: string]: JSONValue };
625625

626626
type TurbopackLoaderItem =
627627
| string
@@ -637,6 +637,11 @@ type TurbopackRuleCondition = {
637637

638638
export type TurbopackRuleConfigItemOrShortcut = TurbopackLoaderItem[] | TurbopackRuleConfigItem;
639639

640+
export type TurbopackMatcherWithRule = {
641+
matcher: string;
642+
rule: TurbopackRuleConfigItemOrShortcut;
643+
};
644+
640645
type TurbopackRuleConfigItemOptions = {
641646
loaders: TurbopackLoaderItem[];
642647
as?: string;

0 commit comments

Comments
 (0)