Skip to content

Commit da5013a

Browse files
committed
inject loader
1 parent de9afec commit da5013a

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as valueInjectionLoader } from './valueInjectionLoader';
22
export { default as prefixLoader } from './prefixLoader';
33
export { default as wrappingLoader } from './wrappingLoader';
4+
export { default as moduleMetadataInjectionLoader } from './moduleMetadataInjectionLoader';

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { debug } from '@sentry/core';
2+
import * as path from 'path';
23
import type { VercelCronsConfig } from '../../common/types';
34
import type { RouteManifest } from '../manifest/types';
45
import type { NextConfigObject, SentryBuildOptions, TurbopackMatcherWithRule, TurbopackOptions } from '../types';
5-
import { supportsNativeDebugIds } from '../util';
6+
import { supportsNativeDebugIds, supportsTurbopackRuleCondition } from '../util';
67
import { generateValueInjectionRules } from './generateValueInjectionRules';
78

89
/**
@@ -56,6 +57,32 @@ export function constructTurbopackConfig({
5657
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, { matcher, rule });
5758
}
5859

60+
// Add module metadata injection loader for thirdPartyErrorFilterIntegration support.
61+
// This is only added when turbopackApplicationKey is set AND the Next.js version supports the
62+
// `condition` field in Turbopack rules (Next.js 16+). Without `condition: { not: 'foreign' }`,
63+
// the loader would tag node_modules as first-party, defeating the purpose.
64+
const applicationKey = userSentryOptions?._experimental?.turbopackApplicationKey;
65+
if (
66+
applicationKey &&
67+
nextJsVersion &&
68+
supportsTurbopackRuleCondition(nextJsVersion)
69+
) {
70+
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
71+
matcher: '*.{ts,tsx,js,jsx,mjs,cjs}',
72+
rule: {
73+
condition: { not: 'foreign' },
74+
loaders: [
75+
{
76+
loader: path.resolve(__dirname, '..', 'loaders', 'moduleMetadataInjectionLoader.js'),
77+
options: {
78+
applicationKey,
79+
},
80+
},
81+
],
82+
},
83+
});
84+
}
85+
5986
return newConfig;
6087
}
6188

packages/nextjs/src/config/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,17 @@ export type SentryBuildOptions = {
713713
* Requires cron jobs to be configured in `vercel.json`.
714714
*/
715715
vercelCronsMonitoring?: boolean;
716+
/**
717+
* Application key used by `thirdPartyErrorFilterIntegration` to distinguish
718+
* first-party code from third-party code in Turbopack builds.
719+
*
720+
* When set, a Turbopack loader injects `_sentryModuleMetadata` into every
721+
* first-party module, mirroring what `@sentry/webpack-plugin` does for
722+
* webpack builds via its `moduleMetadata` / `applicationKey` option.
723+
*
724+
* Requires Next.js 16+
725+
*/
726+
turbopackApplicationKey?: string;
716727
}>;
717728

718729
/**

0 commit comments

Comments
 (0)