@sentry/nextjs
Version:
Official Sentry SDK for Next.js
181 lines (178 loc) • 7.94 kB
JavaScript
import { filterInstrumentedExternals, ORCHESTRION_RUNTIME_EXTERNAL_PACKAGES, BUNDLE_SAFE_INSTRUMENTED_PACKAGES } from '../diagnosticsChannelInjection.js';
import { handleRunAfterProductionCompile } from '../handleRunAfterProductionCompile.js';
import { constructTurbopackConfig } from '../turbopack/constructTurbopackConfig.js';
import { detectActiveBundler, supportsProductionCompileHook } from '../util.js';
import { constructWebpackConfigFunction } from '../webpack.js';
import { DEFAULT_SERVER_EXTERNAL_PACKAGES } from './constants.js';
function getBundlerInfo(nextJsVersion) {
const activeBundler = detectActiveBundler();
const isTurbopack = activeBundler === "turbopack";
const isWebpack = activeBundler === "webpack";
const isTurbopackSupported = supportsProductionCompileHook(nextJsVersion ?? "");
return { isTurbopack, isWebpack, isTurbopackSupported };
}
function maybeWarnAboutUnsupportedTurbopack(nextJsVersion, bundlerInfo) {
if (!bundlerInfo.isTurbopackSupported && bundlerInfo.isTurbopack) {
console.warn(
`[@sentry/nextjs] WARNING: You are using the Sentry SDK with Turbopack. The Sentry SDK is compatible with Turbopack on Next.js version 15.4.1 or later. You are currently on ${nextJsVersion}. Please upgrade to a newer Next.js version to use the Sentry SDK with Turbopack.`
);
}
}
function maybeWarnAboutUnsupportedRunAfterProductionCompileHook(nextJsVersion, userSentryOptions, bundlerInfo) {
if (userSentryOptions.useRunAfterProductionCompileHook && !supportsProductionCompileHook(nextJsVersion ?? "") && bundlerInfo.isWebpack) {
console.warn(
"[@sentry/nextjs] The configured `useRunAfterProductionCompileHook` option is not compatible with your current Next.js version. This option is only supported on Next.js version 15.4.1 or later. Will not run source map and release management logic."
);
}
}
function maybeConstructTurbopackConfig(incomingUserNextConfigObject, userSentryOptions, routeManifest, nextJsVersion, bundlerInfo, vercelCronsConfigResult) {
if (!bundlerInfo.isTurbopack) {
return void 0;
}
const vercelCronsConfig = vercelCronsConfigResult.strategy === "spans" ? vercelCronsConfigResult.config : void 0;
return constructTurbopackConfig({
userNextConfig: incomingUserNextConfigObject,
userSentryOptions,
routeManifest,
nextJsVersion,
vercelCronsConfig
});
}
function resolveUseRunAfterProductionCompileHookOption(userSentryOptions, bundlerInfo) {
return userSentryOptions.useRunAfterProductionCompileHook ?? (bundlerInfo.isTurbopack ? true : false);
}
function maybeSetUpRunAfterProductionCompileHook({
incomingUserNextConfigObject,
userSentryOptions,
releaseName,
nextJsVersion,
bundlerInfo,
turboPackConfig,
shouldUseRunAfterProductionCompileHook
}) {
if (!shouldUseRunAfterProductionCompileHook) {
return;
}
if (!supportsProductionCompileHook(nextJsVersion ?? "")) {
return;
}
if (incomingUserNextConfigObject?.compiler?.runAfterProductionCompile === void 0) {
incomingUserNextConfigObject.compiler ?? (incomingUserNextConfigObject.compiler = {});
incomingUserNextConfigObject.compiler.runAfterProductionCompile = async ({ distDir }) => {
await handleRunAfterProductionCompile(
{
releaseName,
distDir,
buildTool: bundlerInfo.isTurbopack ? "turbopack" : "webpack",
usesNativeDebugIds: bundlerInfo.isTurbopack ? turboPackConfig?.debugIds : void 0,
sriEnabled: !!incomingUserNextConfigObject.experimental?.sri
},
userSentryOptions
);
};
return;
}
if (typeof incomingUserNextConfigObject.compiler.runAfterProductionCompile === "function") {
incomingUserNextConfigObject.compiler.runAfterProductionCompile = new Proxy(
incomingUserNextConfigObject.compiler.runAfterProductionCompile,
{
async apply(target, thisArg, argArray) {
const { distDir } = argArray[0] ?? { distDir: ".next" };
await target.apply(thisArg, argArray);
await handleRunAfterProductionCompile(
{
releaseName,
distDir,
buildTool: bundlerInfo.isTurbopack ? "turbopack" : "webpack",
usesNativeDebugIds: bundlerInfo.isTurbopack ? turboPackConfig?.debugIds : void 0,
sriEnabled: !!incomingUserNextConfigObject.experimental?.sri
},
userSentryOptions
);
}
}
);
return;
}
console.warn(
"[@sentry/nextjs] The configured `compiler.runAfterProductionCompile` option is not a function. Will not run source map and release management logic."
);
}
function maybeEnableTurbopackSourcemaps(incomingUserNextConfigObject, userSentryOptions, bundlerInfo) {
if (!bundlerInfo.isTurbopackSupported || !bundlerInfo.isTurbopack || userSentryOptions.sourcemaps?.disable) {
return;
}
if (incomingUserNextConfigObject.productionBrowserSourceMaps !== void 0) {
return;
}
if (userSentryOptions.debug) {
console.log("[@sentry/nextjs] Automatically enabling browser source map generation for turbopack build.");
}
incomingUserNextConfigObject.productionBrowserSourceMaps = true;
if (userSentryOptions.sourcemaps?.deleteSourcemapsAfterUpload !== void 0) {
return;
}
if (userSentryOptions.debug) {
console.warn(
"[@sentry/nextjs] Source maps will be automatically deleted after being uploaded to Sentry. If you want to keep the source maps, set the `sourcemaps.deleteSourcemapsAfterUpload` option to false in `withSentryConfig()`. If you do not want to generate and upload sourcemaps at all, set the `sourcemaps.disable` option to true."
);
}
userSentryOptions.sourcemaps = {
...userSentryOptions.sourcemaps,
deleteSourcemapsAfterUpload: true
};
}
function getServerExternalPackagesPatch(incomingUserNextConfigObject, nextMajor, useDiagnosticsChannelInjection = false) {
const mergeExternals = (userProvided) => {
const defaults = useDiagnosticsChannelInjection ? filterInstrumentedExternals(DEFAULT_SERVER_EXTERNAL_PACKAGES, BUNDLE_SAFE_INSTRUMENTED_PACKAGES) : DEFAULT_SERVER_EXTERNAL_PACKAGES;
return [
...userProvided || [],
...defaults,
...useDiagnosticsChannelInjection ? ORCHESTRION_RUNTIME_EXTERNAL_PACKAGES : []
];
};
if (nextMajor && nextMajor >= 15) {
return { serverExternalPackages: mergeExternals(incomingUserNextConfigObject.serverExternalPackages) };
}
return {
experimental: {
...incomingUserNextConfigObject.experimental,
serverComponentsExternalPackages: mergeExternals(
incomingUserNextConfigObject.experimental?.serverComponentsExternalPackages
)
}
};
}
function getWebpackPatch({
incomingUserNextConfigObject,
userSentryOptions,
releaseName,
routeManifest,
nextJsVersion,
shouldUseRunAfterProductionCompileHook,
bundlerInfo,
vercelCronsConfigResult
}) {
if (!bundlerInfo.isWebpack || userSentryOptions.webpack?.disableSentryConfig) {
return {};
}
return {
webpack: constructWebpackConfigFunction({
userNextConfig: incomingUserNextConfigObject,
userSentryOptions,
releaseName,
routeManifest,
nextJsVersion,
useRunAfterProductionCompileHook: shouldUseRunAfterProductionCompileHook,
vercelCronsConfigResult
})
};
}
function getTurbopackPatch(bundlerInfo, turboPackConfig) {
if (!bundlerInfo.isTurbopackSupported || !bundlerInfo.isTurbopack) {
return {};
}
return { turbopack: turboPackConfig };
}
export { getBundlerInfo, getServerExternalPackagesPatch, getTurbopackPatch, getWebpackPatch, maybeConstructTurbopackConfig, maybeEnableTurbopackSourcemaps, maybeSetUpRunAfterProductionCompileHook, maybeWarnAboutUnsupportedRunAfterProductionCompileHook, maybeWarnAboutUnsupportedTurbopack, resolveUseRunAfterProductionCompileHookOption };
//# sourceMappingURL=getFinalConfigObjectBundlerUtils.js.map