From bc564a1bb11ee0162aea6e1aa448eb410adade2c Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 14 Oct 2022 21:52:35 +0200 Subject: [PATCH 1/5] Fix typing issue with `withSentryConfig` and `NextConfig` --- .gitignore | 1 + packages/nextjs/package.json | 3 ++- packages/nextjs/src/config/types.ts | 9 +++++++-- packages/nextjs/src/config/webpack.ts | 4 +++- packages/nextjs/test/types/next.config.ts | 17 +++++++++++++++++ packages/nextjs/test/types/package.json | 6 ++++++ packages/nextjs/test/types/tsconfig.json | 6 ++++++ 7 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 packages/nextjs/test/types/next.config.ts create mode 100644 packages/nextjs/test/types/package.json create mode 100644 packages/nextjs/test/types/tsconfig.json diff --git a/.gitignore b/.gitignore index bc3f92fa4202..ba365848ecf3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # dependencies node_modules/ packages/*/package-lock.json +packages/**/test/**/yarn.lock package-lock.json # build and test diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index f4c4544654ac..c244f8b95465 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -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 && tsc", "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" diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index b39c3777bfa0..76b9f5932b13 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -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") @@ -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: { babel: any }; + totalPages: number; + nextRuntime?: 'nodejs' | 'edge'; }; /** diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 387d3344b8e8..4c0990f20b9c 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -401,7 +401,9 @@ export function getWebpackPluginOptions( userPluginOptions: Partial, 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'); diff --git a/packages/nextjs/test/types/next.config.ts b/packages/nextjs/test/types/next.config.ts new file mode 100644 index 000000000000..abc325171f34 --- /dev/null +++ b/packages/nextjs/test/types/next.config.ts @@ -0,0 +1,17 @@ +import { withSentryConfig } from '../../src/config'; +import { NextConfig } from 'next'; + +const config: NextConfig = { + hideSourceMaps: true, + webpack: config => ({ + ...config, + module: { + ...config.module, + rules: [...config.module.rules], + }, + }), +}; + +module.exports = withSentryConfig(config, { + validate: true, +}); diff --git a/packages/nextjs/test/types/package.json b/packages/nextjs/test/types/package.json new file mode 100644 index 000000000000..37352bf558e4 --- /dev/null +++ b/packages/nextjs/test/types/package.json @@ -0,0 +1,6 @@ +{ + "description": "This is only used to install the nextjs v12 package so we can test against those types", + "dependencies": { + "next": "12.3.1" + } +} diff --git a/packages/nextjs/test/types/tsconfig.json b/packages/nextjs/test/types/tsconfig.json new file mode 100644 index 000000000000..adedc2fafa6c --- /dev/null +++ b/packages/nextjs/test/types/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "include": [ + "./**/*" + ] +} From 3c8385ecf36c9225ab834db6d107c5053d3cecd3 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 14 Oct 2022 22:33:00 +0200 Subject: [PATCH 2/5] Lint --- packages/nextjs/test/types/next.config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/test/types/next.config.ts b/packages/nextjs/test/types/next.config.ts index abc325171f34..56d47a0d203b 100644 --- a/packages/nextjs/test/types/next.config.ts +++ b/packages/nextjs/test/types/next.config.ts @@ -1,6 +1,7 @@ -import { withSentryConfig } from '../../src/config'; import { NextConfig } from 'next'; +import { withSentryConfig } from '../../src/config'; + const config: NextConfig = { hideSourceMaps: true, webpack: config => ({ From b3322574b0e78662e738247dba7085181578ccb6 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 15 Oct 2022 00:12:55 +0200 Subject: [PATCH 3/5] Fix tests --- packages/nextjs/src/config/types.ts | 2 +- packages/nextjs/test/config/fixtures.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 76b9f5932b13..6324b35fa36e 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -97,7 +97,7 @@ export type BuildContext = { config: any; webpack: { version: string }; // eslint-disable-next-line @typescript-eslint/no-explicit-any - defaultLoaders: { babel: any }; + defaultLoaders: any; totalPages: number; nextRuntime?: 'nodejs' | 'edge'; }; diff --git a/packages/nextjs/test/config/fixtures.ts b/packages/nextjs/test/config/fixtures.ts index afaf51e7ee25..ef73a1a08010 100644 --- a/packages/nextjs/test/config/fixtures.ts +++ b/packages/nextjs/test/config/fixtures.ts @@ -93,6 +93,8 @@ export function getBuildContext( ...materializedNextConfig, } as NextConfigObject, webpack: { version: webpackVersion }, + defaultLoaders: true, + totalPages: 2, isServer: buildTarget === 'server', }; } From 355bf74142023b67b3151d53b76be1d8807f2965 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 15 Oct 2022 13:16:16 +0200 Subject: [PATCH 4/5] Only run type tests for node >= 12 --- packages/nextjs/package.json | 2 +- packages/nextjs/test/types/package.json | 5 ++++- packages/nextjs/test/types/test.ts | 12 ++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 packages/nextjs/test/types/test.ts diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index c244f8b95465..e60aeddf7efc 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -67,7 +67,7 @@ "test:all": "run-s test:unit test:integration", "test:unit": "jest", "test:integration": "test/run-integration-tests.sh && yarn test:types", - "test:types": "cd test/types/ && yarn && tsc", + "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" diff --git a/packages/nextjs/test/types/package.json b/packages/nextjs/test/types/package.json index 37352bf558e4..b73620071d6c 100644 --- a/packages/nextjs/test/types/package.json +++ b/packages/nextjs/test/types/package.json @@ -1,5 +1,8 @@ { - "description": "This is only used to install the nextjs v12 package so we can test against those types", + "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" } diff --git a/packages/nextjs/test/types/test.ts b/packages/nextjs/test/types/test.ts new file mode 100644 index 000000000000..5ff6045952fb --- /dev/null +++ b/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' }); +} From da5a3b6a5f580efffd3ea603792a738b16fa19e6 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 18 Oct 2022 13:37:28 +0200 Subject: [PATCH 5/5] Add an extra .gitignore --- .gitignore | 1 - packages/nextjs/test/types/.gitignore | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 packages/nextjs/test/types/.gitignore diff --git a/.gitignore b/.gitignore index ba365848ecf3..bc3f92fa4202 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ # dependencies node_modules/ packages/*/package-lock.json -packages/**/test/**/yarn.lock package-lock.json # build and test diff --git a/packages/nextjs/test/types/.gitignore b/packages/nextjs/test/types/.gitignore new file mode 100644 index 000000000000..23d67fc10447 --- /dev/null +++ b/packages/nextjs/test/types/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +yarn.lock