Skip to content

Commit 2e32c30

Browse files
authored
feat(nextjs): Add option to use hidden-source-map as webpack devtool value (#4436)
We currently force the value of the webpack option `devtool` to be `source-map`, in order to guarantee that the correct sourcemaps are generated during build. There is another `devtool` value[1], `hidden-source-map`, which produces the same maps (so just as good for our purposes), but without adding `sourceMappingURL` comments at the bottom of the resulting JS files. (Some users prefer this as a way not to have the browser complain when it tries to follow the `sourceMappingURL` link and comes up empty.) This PR adds an option that users can pass in their nextjs config, under the key `sentry.hideSourceMaps`. When this is set to true, the SDK will use `hidden-source-map` as the `devtool` value instead of `source-map`. Note that since this is a front-end-only problem, the option only applies to client-side builds. The new option is documented in getsentry/sentry-docs#4627. Fixes #3549. [1] https://webpack.js.org/configuration/devtool/
1 parent 472cb0c commit 2e32c30

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type NextConfigObject = {
2020
sentry?: {
2121
disableServerWebpackPlugin?: boolean;
2222
disableClientWebpackPlugin?: boolean;
23+
hideSourceMaps?: boolean;
2324
};
2425
} & {
2526
// other `next.config.js` options

packages/nextjs/src/config/webpack.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,17 @@ export function constructWebpackConfigFunction(
7777
if (enableWebpackPlugin) {
7878
// TODO Handle possibility that user is using `SourceMapDevToolPlugin` (see
7979
// https://webpack.js.org/plugins/source-map-dev-tool-plugin/)
80-
// TODO Give user option to use `hidden-source-map` ?
8180

82-
// Next doesn't let you change this is dev even if you want to - see
81+
// Next doesn't let you change `devtool` in dev even if you want to, so don't bother trying - see
8382
// https://github.com/vercel/next.js/blob/master/errors/improper-devtool.md
8483
if (!buildContext.dev) {
85-
newConfig.devtool = 'source-map';
84+
// `hidden-source-map` produces the same sourcemaps as `source-map`, but doesn't include the `sourceMappingURL`
85+
// comment at the bottom. For folks who aren't publicly hosting their sourcemaps, this is helpful because then
86+
// the browser won't look for them and throw errors into the console when it can't find them. Because this is a
87+
// front-end-only problem, and because `sentry-cli` handles sourcemaps more reliably with the comment than
88+
// without, the option to use `hidden-source-map` only applies to the client-side build.
89+
newConfig.devtool =
90+
userNextConfig.sentry?.hideSourceMaps && !buildContext.isServer ? 'hidden-source-map' : 'source-map';
8691
}
8792

8893
newConfig.plugins = newConfig.plugins || [];

packages/nextjs/test/config.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,26 @@ describe('webpack config', () => {
300300
expect(finalWebpackConfig).toEqual(expect.objectContaining(materializedUserWebpackConfig));
301301
});
302302

303+
it('allows for the use of `hidden-source-map` as `devtool` value for client-side builds', async () => {
304+
const userNextConfigHiddenSourceMaps = { ...userNextConfig, sentry: { ...userNextConfig.sentry } };
305+
userNextConfigHiddenSourceMaps.sentry.hideSourceMaps = true;
306+
307+
const finalClientWebpackConfig = await materializeFinalWebpackConfig({
308+
userNextConfig: userNextConfigHiddenSourceMaps,
309+
incomingWebpackConfig: clientWebpackConfig,
310+
incomingWebpackBuildContext: clientBuildContext,
311+
});
312+
313+
const finalServerWebpackConfig = await materializeFinalWebpackConfig({
314+
userNextConfig: userNextConfigHiddenSourceMaps,
315+
incomingWebpackConfig: serverWebpackConfig,
316+
incomingWebpackBuildContext: serverBuildContext,
317+
});
318+
319+
expect(finalClientWebpackConfig.devtool).toEqual('hidden-source-map');
320+
expect(finalServerWebpackConfig.devtool).toEqual('source-map');
321+
});
322+
303323
describe('webpack `entry` property config', () => {
304324
const serverConfigFilePath = `./${SERVER_SDK_CONFIG_FILE}`;
305325
const clientConfigFilePath = `./${CLIENT_SDK_CONFIG_FILE}`;

0 commit comments

Comments
 (0)