Skip to content

Commit 56041ce

Browse files
committed
feat(nextjs): Bump webpack plugin
1 parent de44fe0 commit 56041ce

File tree

4 files changed

+55
-224
lines changed

4 files changed

+55
-224
lines changed

packages/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"@sentry/types": "7.100.0",
3232
"@sentry/utils": "7.100.0",
3333
"@sentry/vercel-edge": "7.100.0",
34-
"@sentry/webpack-plugin": "1.21.0",
34+
"@sentry/webpack-plugin": "2.14.2",
3535
"chalk": "3.0.0",
3636
"resolve": "1.22.8",
3737
"rollup": "2.78.0",

packages/nextjs/src/config/types.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import type { GLOBAL_OBJ } from '@sentry/utils';
2-
import type { SentryCliPluginOptions } from '@sentry/webpack-plugin';
32
import type { DefinePlugin, WebpackPluginInstance } from 'webpack';
43

5-
export type SentryWebpackPluginOptions = SentryCliPluginOptions;
6-
export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpackPluginOptions };
7-
84
// Export this from here because importing something from Webpack (the library) in `webpack.ts` confuses the heck out of
95
// madge, which we use for circular dependency checking. We've manually excluded this file from the check (which is
106
// safe, since it only includes types), so we can import it here without causing madge to fail. See
@@ -49,6 +45,40 @@ export type NextConfigObject = {
4945
};
5046

5147
export type SentryBuildtimeOptions = {
48+
/**
49+
* TODO
50+
*/
51+
sourcemaps: {
52+
disable: {};
53+
};
54+
55+
/**
56+
* TODO
57+
*/
58+
release: {
59+
disable: {};
60+
};
61+
62+
/**
63+
* TODO
64+
*/
65+
unstable_sentryWebpackPluginOptions: {};
66+
67+
/**
68+
* TODO
69+
*/
70+
silent: {};
71+
72+
/**
73+
* TODO
74+
*/
75+
componentAnnotation: {};
76+
77+
/**
78+
* TODO
79+
*/
80+
telemetry: {};
81+
5282
/**
5383
* Override the SDK's default decision about whether or not to enable to the Sentry webpack plugin for server files.
5484
* Note that `false` forces the plugin to be enabled, even in situations where it's not recommended.
@@ -143,7 +173,7 @@ export type NextConfigFunction = (
143173
export type WebpackConfigFunction = (config: WebpackConfigObject, options: BuildContext) => WebpackConfigObject;
144174
export type WebpackConfigObject = {
145175
devtool?: string;
146-
plugins?: Array<WebpackPluginInstance | SentryWebpackPlugin>;
176+
plugins?: Array<WebpackPluginInstance>;
147177
entry: WebpackEntryProperty;
148178
output: { filename: string; path: string };
149179
target: string;

packages/nextjs/src/config/webpack.ts

Lines changed: 10 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as path from 'path';
33
/* eslint-disable complexity */
44
/* eslint-disable max-lines */
55
import { getSentryRelease } from '@sentry/node-experimental';
6-
import { arrayify, dropUndefinedKeys, escapeStringForRegex, loadModule, logger } from '@sentry/utils';
7-
import type SentryCliPlugin from '@sentry/webpack-plugin';
6+
import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger } from '@sentry/utils';
87
import * as chalk from 'chalk';
98
import { sync as resolveSync } from 'resolve';
10-
import type { Compiler } from 'webpack';
119

10+
import type { SentryWebpackPluginOptions } from '@sentry/webpack-plugin';
11+
import { sentryWebpackPlugin } from '@sentry/webpack-plugin';
1212
import { DEBUG_BUILD } from '../common/debug-build';
1313
import type { VercelCronsConfig } from '../common/types';
1414
// Note: If you need to import a type from Webpack, do it in `types.ts` and export it from there. Otherwise, our
@@ -18,13 +18,11 @@ import type {
1818
EntryPropertyObject,
1919
NextConfigObject,
2020
SentryBuildtimeOptions,
21-
SentryWebpackPluginOptions,
2221
WebpackConfigFunction,
2322
WebpackConfigObject,
2423
WebpackConfigObjectWithModuleRules,
2524
WebpackEntryProperty,
2625
WebpackModuleRule,
27-
WebpackPluginInstance,
2826
} from './types';
2927

3028
const RUNTIME_TO_SDK_ENTRYPOINT_MAP = {
@@ -35,17 +33,9 @@ const RUNTIME_TO_SDK_ENTRYPOINT_MAP = {
3533

3634
// Next.js runs webpack 3 times, once for the client, the server, and for edge. Because we don't want to print certain
3735
// warnings 3 times, we keep track of them here.
38-
let showedMissingAuthTokenErrorMsg = false;
39-
let showedMissingOrgSlugErrorMsg = false;
40-
let showedMissingProjectSlugErrorMsg = false;
4136
let showedHiddenSourceMapsWarningMsg = false;
42-
let showedMissingCliBinaryWarningMsg = false;
4337
let showedMissingGlobalErrorWarningMsg = false;
4438

45-
// TODO: merge default SentryWebpackPlugin ignore with their SentryWebpackPlugin ignore or ignoreFile
46-
// TODO: merge default SentryWebpackPlugin include with their SentryWebpackPlugin include
47-
// TODO: drop merged keys from override check? `includeDefaults` option?
48-
4939
/**
5040
* Construct the function which will be used as the nextjs config's `webpack` value.
5141
*
@@ -347,6 +337,7 @@ export function constructWebpackConfigFunction(
347337
}
348338
}
349339

340+
// TODO(v8): Remove this logic since we are deprecating es5.
350341
// The SDK uses syntax (ES6 and ES6+ features like object spread) which isn't supported by older browsers. For users
351342
// who want to support such browsers, `transpileClientSDK` allows them to force the SDK code to go through the same
352343
// transpilation that their code goes through. We don't turn this on by default because it increases bundle size
@@ -406,17 +397,10 @@ export function constructWebpackConfigFunction(
406397
// without, the option to use `hidden-source-map` only applies to the client-side build.
407398
newConfig.devtool = userSentryOptions.hideSourceMaps && !isServer ? 'hidden-source-map' : 'source-map';
408399

409-
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
410-
if (SentryWebpackPlugin) {
411-
newConfig.plugins = newConfig.plugins || [];
412-
newConfig.plugins.push(new SentryCliDownloadPlugin());
413-
newConfig.plugins.push(
414-
// @ts-expect-error - this exists, the dynamic import just doesn't know about it
415-
new SentryWebpackPlugin(
416-
getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions),
417-
),
418-
);
419-
}
400+
newConfig.plugins = newConfig.plugins || [];
401+
newConfig.plugins.push(
402+
sentryWebpackPlugin(getWebpackPluginOptions(buildContext, userSentryWebpackPluginOptions, userSentryOptions)),
403+
);
420404
}
421405
}
422406

@@ -486,13 +470,11 @@ function findTranspilationRules(rules: WebpackModuleRule[] | undefined, projectD
486470
// Each entry in `module.rules` is either a rule in and of itself or an object with a `oneOf` property, whose value is
487471
// an array of rules
488472
rules.forEach(rule => {
489-
// if (rule.oneOf) {
490473
if (isMatchingRule(rule, projectDir)) {
491474
matchingRules.push(rule);
492475
} else if (rule.oneOf) {
493476
const matchingOneOfRules = rule.oneOf.filter(oneOfRule => isMatchingRule(oneOfRule, projectDir));
494477
matchingRules.push(...matchingOneOfRules);
495-
// } else if (isMatchingRule(rule, projectDir)) {
496478
}
497479
});
498480

@@ -673,30 +655,6 @@ function addFilesToExistingEntryPoint(
673655
entryProperty[entryPointName] = newEntryPoint;
674656
}
675657

676-
/**
677-
* Check the SentryWebpackPlugin options provided by the user against the options we set by default, and warn if any of
678-
* our default options are getting overridden. (Note: If any of our default values is undefined, it won't be included in
679-
* the warning.)
680-
*
681-
* @param defaultOptions Default SentryWebpackPlugin options
682-
* @param userOptions The user's SentryWebpackPlugin options
683-
*/
684-
function checkWebpackPluginOverrides(
685-
defaultOptions: SentryWebpackPluginOptions,
686-
userOptions: Partial<SentryWebpackPluginOptions>,
687-
): void {
688-
// warn if any of the default options for the webpack plugin are getting overridden
689-
const sentryWebpackPluginOptionOverrides = Object.keys(defaultOptions).filter(key => key in userOptions);
690-
if (sentryWebpackPluginOptionOverrides.length > 0) {
691-
DEBUG_BUILD &&
692-
logger.warn(
693-
'[Sentry] You are overriding the following automatically-set SentryWebpackPlugin config options:\n' +
694-
`\t${sentryWebpackPluginOptionOverrides.toString()},\n` +
695-
"which has the possibility of breaking source map upload and application. This is only a good idea if you know what you're doing.",
696-
);
697-
}
698-
}
699-
700658
/**
701659
* Determine if this is an entry point into which both `Sentry.init()` code and the release value should be injected
702660
*
@@ -770,116 +728,9 @@ export function getWebpackPluginOptions(
770728
release: getSentryRelease(buildId),
771729
});
772730

773-
checkWebpackPluginOverrides(defaultPluginOptions, userPluginOptions);
774-
775731
return {
776-
...defaultPluginOptions,
777-
...userPluginOptions,
778-
errorHandler(err, invokeErr, compilation) {
779-
if (err) {
780-
const errorMessagePrefix = `${chalk.red('error')} -`;
781-
782-
if (err.message.includes('ENOENT')) {
783-
if (!showedMissingCliBinaryWarningMsg) {
784-
// eslint-disable-next-line no-console
785-
console.error(
786-
`\n${errorMessagePrefix} ${chalk.bold(
787-
'The Sentry binary to upload sourcemaps could not be found.',
788-
)} Source maps will not be uploaded. Please check that post-install scripts are enabled in your package manager when installing your dependencies and please run your build once without any caching to avoid caching issues of dependencies.\n`,
789-
);
790-
showedMissingCliBinaryWarningMsg = true;
791-
}
792-
return;
793-
}
794-
795-
// Hardcoded way to check for missing auth token until we have a better way of doing this.
796-
if (err.message.includes('Authentication credentials were not provided.')) {
797-
let msg;
798-
799-
if (process.env.VERCEL) {
800-
msg = `To fix this, use Sentry's Vercel integration to automatically set the ${chalk.bold.cyan(
801-
'SENTRY_AUTH_TOKEN',
802-
)} environment variable: https://vercel.com/integrations/sentry`;
803-
} else {
804-
msg =
805-
'You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/\n' +
806-
`After generating a Sentry auth token, set it via the ${chalk.bold.cyan(
807-
'SENTRY_AUTH_TOKEN',
808-
)} environment variable during the build.`;
809-
}
810-
811-
if (!showedMissingAuthTokenErrorMsg) {
812-
// eslint-disable-next-line no-console
813-
console.error(
814-
`${errorMessagePrefix} ${chalk.bold(
815-
'No Sentry auth token configured.',
816-
)} Source maps will not be uploaded.\n${msg}\n`,
817-
);
818-
showedMissingAuthTokenErrorMsg = true;
819-
}
820-
821-
return;
822-
}
823-
824-
// Hardcoded way to check for missing org slug until we have a better way of doing this.
825-
if (err.message.includes('An organization slug is required')) {
826-
let msg;
827-
if (process.env.VERCEL) {
828-
msg = `To fix this, use Sentry's Vercel integration to automatically set the ${chalk.bold.cyan(
829-
'SENTRY_ORG',
830-
)} environment variable: https://vercel.com/integrations/sentry`;
831-
} else {
832-
msg = `To fix this, set the ${chalk.bold.cyan(
833-
'SENTRY_ORG',
834-
)} environment variable to the to your organization slug during the build.`;
835-
}
836-
837-
if (!showedMissingOrgSlugErrorMsg) {
838-
// eslint-disable-next-line no-console
839-
console.error(
840-
`${errorMessagePrefix} ${chalk.bold(
841-
'No Sentry organization slug configured.',
842-
)} Source maps will not be uploaded.\n${msg}\n`,
843-
);
844-
showedMissingOrgSlugErrorMsg = true;
845-
}
846-
847-
return;
848-
}
849-
850-
// Hardcoded way to check for missing project slug until we have a better way of doing this.
851-
if (err.message.includes('A project slug is required')) {
852-
let msg;
853-
if (process.env.VERCEL) {
854-
msg = `To fix this, use Sentry's Vercel integration to automatically set the ${chalk.bold.cyan(
855-
'SENTRY_PROJECT',
856-
)} environment variable: https://vercel.com/integrations/sentry`;
857-
} else {
858-
msg = `To fix this, set the ${chalk.bold.cyan(
859-
'SENTRY_PROJECT',
860-
)} environment variable to the name of your Sentry project during the build.`;
861-
}
862-
863-
if (!showedMissingProjectSlugErrorMsg) {
864-
// eslint-disable-next-line no-console
865-
console.error(
866-
`${errorMessagePrefix} ${chalk.bold(
867-
'No Sentry project slug configured.',
868-
)} Source maps will not be uploaded.\n${msg}\n`,
869-
);
870-
showedMissingProjectSlugErrorMsg = true;
871-
}
872-
873-
return;
874-
}
875-
}
876-
877-
if (userPluginOptions.errorHandler) {
878-
return userPluginOptions.errorHandler(err, invokeErr, compilation);
879-
}
880-
881-
return invokeErr();
882-
},
732+
authToken: 'todo',
733+
url: 'todo',
883734
};
884735
}
885736

@@ -1076,54 +927,3 @@ function getRequestAsyncStorageModuleLocation(
1076927

1077928
return undefined;
1078929
}
1079-
1080-
let downloadingCliAttempted = false;
1081-
1082-
class SentryCliDownloadPlugin implements WebpackPluginInstance {
1083-
public apply(compiler: Compiler): void {
1084-
compiler.hooks.beforeRun.tapAsync('SentryCliDownloadPlugin', (compiler, callback) => {
1085-
const SentryWebpackPlugin = loadModule<SentryCliPlugin>('@sentry/webpack-plugin');
1086-
if (!SentryWebpackPlugin) {
1087-
// Pretty much an invariant.
1088-
return callback();
1089-
}
1090-
1091-
// @ts-expect-error - this exists, the dynamic import just doesn't know it
1092-
if (SentryWebpackPlugin.cliBinaryExists()) {
1093-
return callback();
1094-
}
1095-
1096-
if (!downloadingCliAttempted) {
1097-
downloadingCliAttempted = true;
1098-
// eslint-disable-next-line no-console
1099-
logger.info(
1100-
`\n${chalk.cyan('info')} - ${chalk.bold(
1101-
'Sentry binary to upload source maps not found.',
1102-
)} Package manager post-install scripts are likely disabled or there is a caching issue. Manually downloading instead...`,
1103-
);
1104-
1105-
// @ts-expect-error - this exists, the dynamic import just doesn't know it
1106-
const cliDownloadPromise: Promise<void> = SentryWebpackPlugin.downloadCliBinary({
1107-
log: () => {
1108-
// No logs from directly from CLI
1109-
},
1110-
});
1111-
1112-
cliDownloadPromise.then(
1113-
() => {
1114-
// eslint-disable-next-line no-console
1115-
logger.info(`${chalk.cyan('info')} - Sentry binary was successfully downloaded.\n`);
1116-
return callback();
1117-
},
1118-
e => {
1119-
// eslint-disable-next-line no-console
1120-
logger.error(`${chalk.red('error')} - Sentry binary download failed:`, e);
1121-
return callback();
1122-
},
1123-
);
1124-
} else {
1125-
return callback();
1126-
}
1127-
});
1128-
}
1129-
}

0 commit comments

Comments
 (0)