@sentry/nextjs
Version:
Official Sentry SDK for Next.js
131 lines (127 loc) • 5.04 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const core = require('@sentry/core');
const path = require('path');
const config = require('@sentry/server-utils/orchestrion/config');
const webpack = require('@sentry/server-utils/orchestrion/webpack');
const buildLogger = require('../buildLogger.js');
const util = require('../util.js');
const tunnel = require('../withSentryConfig/tunnel.js');
const generateValueInjectionRules = require('./generateValueInjectionRules.js');
function constructTurbopackConfig({
userNextConfig,
userSentryOptions,
routeManifest,
nextJsVersion,
vercelCronsConfig
}) {
const shouldEnableNativeDebugIds = (util.supportsNativeDebugIds(nextJsVersion ?? "") && userNextConfig?.turbopack?.debugIds) ?? userSentryOptions?.sourcemaps?.disable !== true;
const newConfig = {
...userNextConfig.turbopack,
...shouldEnableNativeDebugIds ? { debugIds: true } : {}
};
const valueInjectionRules = generateValueInjectionRules.generateValueInjectionRules({
routeManifest,
nextJsVersion,
tunnelRouteValues: tunnel.getTunnelRouteInjectedValues(userNextConfig, userSentryOptions),
vercelCronsConfig
});
for (const { matcher, rule } of valueInjectionRules) {
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, { matcher, rule });
}
const applicationKey = userSentryOptions?.applicationKey;
if (applicationKey && nextJsVersion && util.supportsTurbopackRuleCondition(nextJsVersion)) {
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
matcher: "*.{ts,tsx,js,jsx,mjs,cjs}",
rule: {
condition: {
all: [
"browser",
{ not: { path: /next\/dist\/build\/polyfills/ } },
...userSentryOptions?.applicationKeyExcludeDependencies ? [{ not: "foreign" }] : []
]
},
loaders: [
{
loader: path.resolve(__dirname, "..", "loaders", "moduleMetadataInjectionLoader.js"),
options: {
applicationKey
}
}
]
}
});
}
const reactComponentAnnotation = {
...userSentryOptions?.reactComponentAnnotation,
// eslint-disable-next-line typescript/no-deprecated
...userSentryOptions?._experimental?.turbopackReactComponentAnnotation
};
if (reactComponentAnnotation.enabled) {
if (nextJsVersion && util.supportsTurbopackRuleCondition(nextJsVersion)) {
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
matcher: "*.{tsx,jsx}",
rule: {
condition: { not: "foreign" },
loaders: [
{
loader: path.resolve(__dirname, "..", "loaders", "componentAnnotationLoader.js"),
options: {
ignoredComponents: reactComponentAnnotation.ignoredComponents ?? []
}
}
]
}
});
} else {
buildLogger.getBuildLogger(userSentryOptions?.silent).warn(
`[@sentry/nextjs] \`reactComponentAnnotation\` is enabled but React component annotation requires Next.js 16+ on Turbopack builds${nextJsVersion ? ` (detected ${nextJsVersion})` : ""}. Your components will not be annotated.`
);
}
}
newConfig.rules = maybeAddOrchestrionRule(newConfig.rules, userSentryOptions, nextJsVersion);
return newConfig;
}
function maybeAddOrchestrionRule(rules, userSentryOptions, nextJsVersion) {
if (userSentryOptions?.buildTimeInstrumentation === false || !nextJsVersion || !util.supportsTurbopackRuleCondition(nextJsVersion)) {
return rules;
}
const importHelperPath = webpack.resolveOrchestrionRuntimeRequest("@sentry/server-utils");
const instrumentedPackages = config.getInstrumentedModuleNames().map(core.escapeStringForRegex);
const instrumentedPackagePath = new RegExp(`node_modules/(?:${instrumentedPackages.join("|")})/`);
return safelyAddTurbopackRule(rules, {
matcher: "*.{js,mjs,cjs}",
rule: {
condition: { all: ["node", { path: instrumentedPackagePath }] },
loaders: [
{
loader: webpack.getOrchestrionLoaderPath(),
// Turbopack JSON-serializes loader options, so a RegExp `filePath` must be encoded first.
options: {
instrumentations: webpack.serializeInstrumentations(webpack.getSentryInstrumentations()),
...importHelperPath ? { importHelperPath } : {}
}
}
]
}
});
}
function safelyAddTurbopackRule(existingRules, { matcher, rule }) {
if (!existingRules) {
return {
[matcher]: rule
};
}
if (existingRules[matcher]) {
core.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
};
}
exports.constructTurbopackConfig = constructTurbopackConfig;
exports.safelyAddTurbopackRule = safelyAddTurbopackRule;
//# sourceMappingURL=constructTurbopackConfig.js.map