Skip to content

Commit e92ad68

Browse files
authored
chore(nextjs): Clean up and add to config tests (getsentry#4414)
Some small fixes to comments, some cleaner unpacking of variables, and two added tests pulled out of an upcoming PR to keep it focused on its original goal. No behavior changes.
1 parent cf6d23a commit e92ad68

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ async function addSentryToEntryProperty(
116116
// we know is that it won't have gotten *simpler* in form, so we only need to worry about the object and function
117117
// options. See https://webpack.js.org/configuration/entry-context/#entry.
118118

119+
const { isServer, dir: projectDir, dev: isDev, config: userNextConfig } = buildContext;
120+
119121
const newEntryProperty =
120122
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
121123

122124
// `sentry.server.config.js` or `sentry.client.config.js` (or their TS equivalents)
123-
const userConfigFile = buildContext.isServer
124-
? getUserConfigFile(buildContext.dir, 'server')
125-
: getUserConfigFile(buildContext.dir, 'client');
125+
const userConfigFile = isServer ? getUserConfigFile(projectDir, 'server') : getUserConfigFile(projectDir, 'client');
126126

127127
// we need to turn the filename into a path so webpack can find it
128128
const filesToInject = [`./${userConfigFile}`];
@@ -131,12 +131,12 @@ async function addSentryToEntryProperty(
131131
// server SDK's default `RewriteFrames` instance (which needs it at runtime). Doesn't work when using the dev server
132132
// because it somehow tricks the file watcher into thinking that compilation itself is a file change, triggering an
133133
// infinite recompiling loop. (This should be fine because we don't upload sourcemaps in dev in any case.)
134-
if (buildContext.isServer && !buildContext.dev) {
134+
if (isServer && !isDev) {
135135
const rewriteFramesHelper = path.resolve(
136136
fs.mkdtempSync(path.resolve(os.tmpdir(), 'sentry-')),
137137
'rewriteFramesHelper.js',
138138
);
139-
fs.writeFileSync(rewriteFramesHelper, `global.__rewriteFramesDistDir__ = '${buildContext.config.distDir}';\n`);
139+
fs.writeFileSync(rewriteFramesHelper, `global.__rewriteFramesDistDir__ = '${userNextConfig.distDir}';\n`);
140140
// stick our helper file ahead of the user's config file so the value is in the global namespace *before*
141141
// `Sentry.init()` is called
142142
filesToInject.unshift(rewriteFramesHelper);
@@ -269,13 +269,13 @@ export function getWebpackPluginOptions(
269269
buildContext: BuildContext,
270270
userPluginOptions: Partial<SentryWebpackPluginOptions>,
271271
): SentryWebpackPluginOptions {
272-
const { isServer, dir: projectDir, buildId, dev: isDev, config: nextConfig, webpack } = buildContext;
273-
const distDir = nextConfig.distDir ?? '.next'; // `.next` is the default directory
272+
const { buildId, isServer, webpack, config: userNextConfig, dev: isDev, dir: projectDir } = buildContext;
273+
const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory
274274

275275
const isWebpack5 = webpack.version.startsWith('5');
276-
const isServerless = nextConfig.target === 'experimental-serverless-trace';
276+
const isServerless = userNextConfig.target === 'experimental-serverless-trace';
277277
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
278-
const urlPrefix = nextConfig.basePath ? `~${nextConfig.basePath}/_next` : '~/_next';
278+
const urlPrefix = userNextConfig.basePath ? `~${userNextConfig.basePath}/_next` : '~/_next';
279279

280280
const serverInclude = isServerless
281281
? [{ paths: [`${distDir}/serverless/`], urlPrefix: `${urlPrefix}/serverless` }]

packages/nextjs/test/config.test.ts

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ describe('webpack config', () => {
315315

316316
expect(finalWebpackConfig.entry).toEqual(
317317
expect.objectContaining({
318-
// original entry point value is a string
318+
// original entrypoint value is a string
319319
// (was 'private-next-pages/api/dogs/[name].js')
320320
'pages/api/dogs/[name]': [rewriteFramesHelper, serverConfigFilePath, 'private-next-pages/api/dogs/[name].js'],
321321

322-
// original entry point value is a string array
322+
// original entrypoint value is a string array
323323
// (was ['./node_modules/smellOVision/index.js', 'private-next-pages/_app.js'])
324324
'pages/_app': [
325325
rewriteFramesHelper,
@@ -328,14 +328,14 @@ describe('webpack config', () => {
328328
'private-next-pages/_app.js',
329329
],
330330

331-
// original entry point value is an object containing a string `import` value
332-
// (`import` was 'private-next-pages/api/simulator/dogStats/[name].js')
331+
// original entrypoint value is an object containing a string `import` value
332+
// (was { import: 'private-next-pages/api/simulator/dogStats/[name].js' })
333333
'pages/api/simulator/dogStats/[name]': {
334334
import: [rewriteFramesHelper, serverConfigFilePath, 'private-next-pages/api/simulator/dogStats/[name].js'],
335335
},
336336

337-
// original entry point value is an object containing a string array `import` value
338-
// (`import` was ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'])
337+
// original entrypoint value is an object containing a string array `import` value
338+
// (was { import: ['./node_modules/dogPoints/converter.js', 'private-next-pages/api/simulator/leaderboard.js'] })
339339
'pages/api/simulator/leaderboard': {
340340
import: [
341341
rewriteFramesHelper,
@@ -345,16 +345,64 @@ describe('webpack config', () => {
345345
],
346346
},
347347

348-
// original entry point value is an object containg properties besides `import`
349-
// (`dependOn` remains untouched)
348+
// original entrypoint value is an object containg properties besides `import`
349+
// (was { import: 'private-next-pages/api/tricks/[trickName].js', dependOn: 'treats', })
350350
'pages/api/tricks/[trickName]': {
351351
import: [rewriteFramesHelper, serverConfigFilePath, 'private-next-pages/api/tricks/[trickName].js'],
352-
dependOn: 'treats',
352+
dependOn: 'treats', // untouched
353353
},
354354
}),
355355
);
356356
});
357357

358+
it('injects user config file into `_app` in both server and client bundles', async () => {
359+
const finalServerWebpackConfig = await materializeFinalWebpackConfig({
360+
userNextConfig,
361+
incomingWebpackConfig: serverWebpackConfig,
362+
incomingWebpackBuildContext: serverBuildContext,
363+
});
364+
const finalClientWebpackConfig = await materializeFinalWebpackConfig({
365+
userNextConfig,
366+
incomingWebpackConfig: clientWebpackConfig,
367+
incomingWebpackBuildContext: clientBuildContext,
368+
});
369+
370+
expect(finalServerWebpackConfig.entry).toEqual(
371+
expect.objectContaining({
372+
'pages/_app': expect.arrayContaining([serverConfigFilePath]),
373+
}),
374+
);
375+
expect(finalClientWebpackConfig.entry).toEqual(
376+
expect.objectContaining({
377+
'pages/_app': expect.arrayContaining([clientConfigFilePath]),
378+
}),
379+
);
380+
});
381+
382+
it('injects user config file into API routes', async () => {
383+
const finalWebpackConfig = await materializeFinalWebpackConfig({
384+
userNextConfig,
385+
incomingWebpackConfig: serverWebpackConfig,
386+
incomingWebpackBuildContext: serverBuildContext,
387+
});
388+
389+
expect(finalWebpackConfig.entry).toEqual(
390+
expect.objectContaining({
391+
'pages/api/simulator/dogStats/[name]': {
392+
import: expect.arrayContaining([serverConfigFilePath]),
393+
},
394+
395+
'pages/api/simulator/leaderboard': {
396+
import: expect.arrayContaining([serverConfigFilePath]),
397+
},
398+
399+
'pages/api/tricks/[trickName]': expect.objectContaining({
400+
import: expect.arrayContaining([serverConfigFilePath]),
401+
}),
402+
}),
403+
);
404+
});
405+
358406
it('does not inject anything into non-_app, non-API routes', async () => {
359407
const finalWebpackConfig = await materializeFinalWebpackConfig({
360408
userNextConfig,

0 commit comments

Comments
 (0)