@sentry/nextjs
Version:
Official Sentry SDK for Next.js
106 lines (103 loc) • 3.97 kB
JavaScript
import { debug } from '@sentry/core';
import * as path from 'path';
import { serializeInstrumentations, getSentryInstrumentations, getOrchestrionLoaderPath } from '@sentry/server-utils/orchestrion/webpack';
import { supportsNativeDebugIds, supportsTurbopackRuleCondition } from '../util.js';
import { generateValueInjectionRules } from './generateValueInjectionRules.js';
function constructTurbopackConfig({
userNextConfig,
userSentryOptions,
routeManifest,
nextJsVersion,
vercelCronsConfig
}) {
const shouldEnableNativeDebugIds = (supportsNativeDebugIds(nextJsVersion ?? "") && userNextConfig?.turbopack?.debugIds) ?? userSentryOptions?.sourcemaps?.disable !== true;
const newConfig = {
...userNextConfig.turbopack,
...shouldEnableNativeDebugIds ? { debugIds: true } : {}
};
const tunnelPath = userSentryOptions?.tunnelRoute !== void 0 && userNextConfig.output !== "export" && typeof userSentryOptions.tunnelRoute === "string" ? `${userNextConfig.basePath ?? ""}${userSentryOptions.tunnelRoute}` : void 0;
const valueInjectionRules = generateValueInjectionRules({
routeManifest,
nextJsVersion,
tunnelPath,
vercelCronsConfig
});
for (const { matcher, rule } of valueInjectionRules) {
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, { matcher, rule });
}
const applicationKey = userSentryOptions?.applicationKey ?? userSentryOptions?._experimental?.turbopackApplicationKey;
if (applicationKey && nextJsVersion && supportsTurbopackRuleCondition(nextJsVersion)) {
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
matcher: "*.{ts,tsx,js,jsx,mjs,cjs}",
rule: {
condition: { not: { path: /next\/dist\/build\/polyfills/ } },
loaders: [
{
loader: path.resolve(__dirname, "..", "loaders", "moduleMetadataInjectionLoader.js"),
options: {
applicationKey
}
}
]
}
});
}
const turbopackReactComponentAnnotation = userSentryOptions?._experimental?.turbopackReactComponentAnnotation;
if (turbopackReactComponentAnnotation?.enabled && nextJsVersion && supportsTurbopackRuleCondition(nextJsVersion)) {
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
matcher: "*.{tsx,jsx}",
rule: {
condition: { not: "foreign" },
loaders: [
{
loader: path.resolve(__dirname, "..", "loaders", "componentAnnotationLoader.js"),
options: {
ignoredComponents: turbopackReactComponentAnnotation.ignoredComponents ?? []
}
}
]
}
});
}
newConfig.rules = maybeAddOrchestrionRule(newConfig.rules, userSentryOptions, nextJsVersion);
return newConfig;
}
function maybeAddOrchestrionRule(rules, userSentryOptions, nextJsVersion) {
if (!userSentryOptions?._experimental?.useDiagnosticsChannelInjection || !nextJsVersion || !supportsTurbopackRuleCondition(nextJsVersion)) {
return rules;
}
return safelyAddTurbopackRule(rules, {
matcher: "*.{js,mjs,cjs}",
rule: {
condition: "node",
loaders: [
{
loader: getOrchestrionLoaderPath(),
// Turbopack JSON-serializes loader options, so a RegExp `filePath` must be encoded first.
options: {
instrumentations: serializeInstrumentations(getSentryInstrumentations())
}
}
]
}
});
}
function safelyAddTurbopackRule(existingRules, { matcher, rule }) {
if (!existingRules) {
return {
[matcher]: rule
};
}
if (existingRules[matcher]) {
debug.log(
`[@sentry/nextjs] - Turbopack rule already exists for ${matcher}. Please remove it from your Next.js config in order for Sentry to work properly.`
);
return existingRules;
}
return {
...existingRules,
[matcher]: rule
};
}
export { constructTurbopackConfig, safelyAddTurbopackRule };
//# sourceMappingURL=constructTurbopackConfig.js.map