Skip to content

Commit 8b2b78e

Browse files
authored
fix(nextjs): Fix typing issue with withSentryConfig and NextConfig (#5967)
1 parent d9d1115 commit 8b2b78e

File tree

9 files changed

+61
-4
lines changed

9 files changed

+61
-4
lines changed

Diff for: packages/nextjs/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"test": "run-s test:unit",
6767
"test:all": "run-s test:unit test:integration",
6868
"test:unit": "jest",
69-
"test:integration": "test/run-integration-tests.sh",
69+
"test:integration": "test/run-integration-tests.sh && yarn test:types",
70+
"test:types": "cd test/types && yarn test",
7071
"test:watch": "jest --watch",
7172
"vercel:branch": "source vercel/set-up-branch-for-test-app-use.sh",
7273
"vercel:project": "source vercel/make-project-use-current-branch.sh"

Diff for: packages/nextjs/src/config/types.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type NextConfigFunctionWithSentry = (
2323

2424
export type NextConfigObject = {
2525
// Custom webpack options
26-
webpack?: WebpackConfigFunction;
26+
webpack?: WebpackConfigFunction | null;
2727
// Whether to build serverless functions for all pages, not just API routes. Removed in nextjs 12+.
2828
target?: 'server' | 'experimental-serverless-trace';
2929
// The output directory for the built app (defaults to ".next")
@@ -93,8 +93,13 @@ export type BuildContext = {
9393
isServer: boolean;
9494
buildId: string;
9595
dir: string;
96-
config: NextConfigObject;
96+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
97+
config: any;
9798
webpack: { version: string };
99+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
100+
defaultLoaders: any;
101+
totalPages: number;
102+
nextRuntime?: 'nodejs' | 'edge';
98103
};
99104

100105
/**

Diff for: packages/nextjs/src/config/webpack.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ export function getWebpackPluginOptions(
410410
userPluginOptions: Partial<SentryWebpackPluginOptions>,
411411
userSentryOptions: UserSentryOptions,
412412
): SentryWebpackPluginOptions {
413-
const { buildId, isServer, webpack, config: userNextConfig, dev: isDev, dir: projectDir } = buildContext;
413+
const { buildId, isServer, webpack, config, dev: isDev, dir: projectDir } = buildContext;
414+
const userNextConfig = config as NextConfigObject;
415+
414416
const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory
415417

416418
const isWebpack5 = webpack.version.startsWith('5');

Diff for: packages/nextjs/test/config/fixtures.ts

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export function getBuildContext(
9393
...materializedNextConfig,
9494
} as NextConfigObject,
9595
webpack: { version: webpackVersion },
96+
defaultLoaders: true,
97+
totalPages: 2,
9698
isServer: buildTarget === 'server',
9799
};
98100
}

Diff for: packages/nextjs/test/types/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
yarn.lock

Diff for: packages/nextjs/test/types/next.config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NextConfig } from 'next';
2+
3+
import { withSentryConfig } from '../../src/config';
4+
5+
const config: NextConfig = {
6+
hideSourceMaps: true,
7+
webpack: config => ({
8+
...config,
9+
module: {
10+
...config.module,
11+
rules: [...config.module.rules],
12+
},
13+
}),
14+
};
15+
16+
module.exports = withSentryConfig(config, {
17+
validate: true,
18+
});

Diff for: packages/nextjs/test/types/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"description": "This is used to install the nextjs v12 so we can test against those types",
3+
"scripts": {
4+
"test": "ts-node test.ts"
5+
},
6+
"dependencies": {
7+
"next": "12.3.1"
8+
}
9+
}

Diff for: packages/nextjs/test/types/test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable no-console */
2+
import { parseSemver } from '@sentry/utils';
3+
import { execSync } from 'child_process';
4+
5+
const NODE_VERSION = parseSemver(process.versions.node);
6+
7+
if (NODE_VERSION.major && NODE_VERSION.major >= 12) {
8+
console.log('Installing next@v12...');
9+
execSync('yarn install', { stdio: 'inherit' });
10+
console.log('Testing some types...');
11+
execSync('tsc --noEmit --project tsconfig.json', { stdio: 'inherit' });
12+
}

Diff for: packages/nextjs/test/types/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"include": [
4+
"./**/*"
5+
]
6+
}

0 commit comments

Comments
 (0)