Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nextjs): Fix typing issue with withSentryConfig and NextConfig #5967

Merged
merged 5 commits into from Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/nextjs/package.json
Expand Up @@ -66,7 +66,8 @@
"test": "run-s test:unit",
"test:all": "run-s test:unit test:integration",
"test:unit": "jest",
"test:integration": "test/run-integration-tests.sh",
"test:integration": "test/run-integration-tests.sh && yarn test:types",
"test:types": "cd test/types && yarn test",
"test:watch": "jest --watch",
"vercel:branch": "source vercel/set-up-branch-for-test-app-use.sh",
"vercel:project": "source vercel/make-project-use-current-branch.sh"
Expand Down
9 changes: 7 additions & 2 deletions packages/nextjs/src/config/types.ts
Expand Up @@ -23,7 +23,7 @@ export type NextConfigFunctionWithSentry = (

export type NextConfigObject = {
// Custom webpack options
webpack?: WebpackConfigFunction;
webpack?: WebpackConfigFunction | null;
// Whether to build serverless functions for all pages, not just API routes. Removed in nextjs 12+.
target?: 'server' | 'experimental-serverless-trace';
// The output directory for the built app (defaults to ".next")
Expand Down Expand Up @@ -93,8 +93,13 @@ export type BuildContext = {
isServer: boolean;
buildId: string;
dir: string;
config: NextConfigObject;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any;
webpack: { version: string };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
defaultLoaders: any;
totalPages: number;
nextRuntime?: 'nodejs' | 'edge';
};

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/src/config/webpack.ts
Expand Up @@ -401,7 +401,9 @@ export function getWebpackPluginOptions(
userPluginOptions: Partial<SentryWebpackPluginOptions>,
userSentryOptions: UserSentryOptions,
): SentryWebpackPluginOptions {
const { buildId, isServer, webpack, config: userNextConfig, dev: isDev, dir: projectDir } = buildContext;
const { buildId, isServer, webpack, config, dev: isDev, dir: projectDir } = buildContext;
const userNextConfig = config as NextConfigObject;

const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory

const isWebpack5 = webpack.version.startsWith('5');
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/test/config/fixtures.ts
Expand Up @@ -93,6 +93,8 @@ export function getBuildContext(
...materializedNextConfig,
} as NextConfigObject,
webpack: { version: webpackVersion },
defaultLoaders: true,
totalPages: 2,
isServer: buildTarget === 'server',
};
}
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/test/types/.gitignore
@@ -0,0 +1,2 @@
node_modules/
yarn.lock
18 changes: 18 additions & 0 deletions packages/nextjs/test/types/next.config.ts
@@ -0,0 +1,18 @@
import { NextConfig } from 'next';

import { withSentryConfig } from '../../src/config';

const config: NextConfig = {
hideSourceMaps: true,
webpack: config => ({
...config,
module: {
...config.module,
rules: [...config.module.rules],
},
}),
};

module.exports = withSentryConfig(config, {
validate: true,
});
9 changes: 9 additions & 0 deletions packages/nextjs/test/types/package.json
@@ -0,0 +1,9 @@
{
"description": "This is used to install the nextjs v12 so we can test against those types",
"scripts": {
"test": "ts-node test.ts"
},
"dependencies": {
"next": "12.3.1"
}
}
12 changes: 12 additions & 0 deletions packages/nextjs/test/types/test.ts
@@ -0,0 +1,12 @@
/* eslint-disable no-console */
import { parseSemver } from '@sentry/utils';
import { execSync } from 'child_process';

const NODE_VERSION = parseSemver(process.versions.node);

if (NODE_VERSION.major && NODE_VERSION.major >= 12) {
console.log('Installing next@v12...');
execSync('yarn install', { stdio: 'inherit' });
console.log('Testing some types...');
execSync('tsc --noEmit --project tsconfig.json', { stdio: 'inherit' });
}
6 changes: 6 additions & 0 deletions packages/nextjs/test/types/tsconfig.json
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"include": [
"./**/*"
]
}