From f04cc2fd78e5e0ca90d4c2c026cdc5792fdacc1b Mon Sep 17 00:00:00 2001 From: Ian Serpa Date: Mon, 3 Apr 2023 21:12:18 +0200 Subject: [PATCH] Fix relative import path for type plugin in NX workspaces Refs: #44363, nrwl/nx#14558 --- .../webpack/plugins/next-types-plugin.test.ts | 50 +++++++++++++++++++ .../webpack/plugins/next-types-plugin.ts | 16 +++--- 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 packages/next/src/build/webpack/plugins/next-types-plugin.test.ts diff --git a/packages/next/src/build/webpack/plugins/next-types-plugin.test.ts b/packages/next/src/build/webpack/plugins/next-types-plugin.test.ts new file mode 100644 index 00000000000000..c37c6c2a294481 --- /dev/null +++ b/packages/next/src/build/webpack/plugins/next-types-plugin.test.ts @@ -0,0 +1,50 @@ +import { NextTypesPlugin } from './next-types-plugin' + +describe('next-types-plugin', () => { + it('should generate correct base import path', () => { + const plugin = new NextTypesPlugin({ + dir: '/Users/myself/myproject', + distDir: '.next', + appDir: '/Users/myself/myproject/app', + dev: false, + isEdgeServer: false, + pageExtensions: ['tsx', 'ts', 'jsx', 'js'], + typedRoutes: false, + originalRewrites: undefined, + originalRedirects: undefined, + }) + expect(plugin.relativeBaseImportPath).toEqual('../../../app') + }) + + it('should generate correct base import path for nx monorepos', () => { + const plugin = new NextTypesPlugin({ + dir: '/Users/myself/code/nx-monorepo/apps/myproject', + distDir: '../../dist/apps/myproject/.next', + appDir: '/Users/myself/code/nx-monorepo/apps/myproject/app', + dev: false, + isEdgeServer: false, + pageExtensions: ['tsx', 'ts', 'jsx', 'js'], + typedRoutes: false, + originalRewrites: undefined, + originalRedirects: undefined, + }) + expect(plugin.relativeBaseImportPath).toEqual( + '../../../../../../apps/myproject/app' + ) + }) + + it('should generate correct base import path for custom projects', () => { + const plugin = new NextTypesPlugin({ + dir: '/Users/myself/code/custom-project/frontend/ui', + distDir: '../dist/ui/.next', + appDir: '/Users/myself/code/custom-project/frontend/ui/app', + dev: false, + isEdgeServer: false, + pageExtensions: ['tsx', 'ts', 'jsx', 'js'], + typedRoutes: false, + originalRewrites: undefined, + originalRedirects: undefined, + }) + expect(plugin.relativeBaseImportPath).toEqual('../../../../../ui/app') + }) +}) diff --git a/packages/next/src/build/webpack/plugins/next-types-plugin.ts b/packages/next/src/build/webpack/plugins/next-types-plugin.ts index 0d5bd01f8f729e..b6617679c563fe 100644 --- a/packages/next/src/build/webpack/plugins/next-types-plugin.ts +++ b/packages/next/src/build/webpack/plugins/next-types-plugin.ts @@ -435,6 +435,8 @@ declare module 'next/link' { }` } +const appTypesBasePath = path.join('types', 'app') + export class NextTypesPlugin { dir: string distDir: string @@ -463,6 +465,11 @@ export class NextTypesPlugin { } } + get relativeBaseImportPath() { + const distDirAbs = path.join(this.dir, this.distDir) + return path.relative(path.join(distDirAbs, appTypesBasePath), this.appDir) + } + collectPage(filePath: string) { if (!this.typedRoutes) return @@ -503,9 +510,6 @@ export class NextTypesPlugin { } apply(compiler: webpack.Compiler) { - // From dist root to project root - const distDirRelative = path.relative(this.distDir + '/..', '.') - // From asset root to dist root const assetDirRelative = this.dev ? '..' @@ -542,14 +546,12 @@ export class NextTypesPlugin { } const typePath = path.join( - 'types', - 'app', + appTypesBasePath, relativePathToApp.replace(/\.(js|jsx|ts|tsx|mjs)$/, '.ts') ) const relativeImportPath = path .join( - distDirRelative, - path.relative(typePath, ''), + this.relativeBaseImportPath, relativePathToRoot.replace(/\.(js|jsx|ts|tsx|mjs)$/, '') ) .replace(/\\/g, '/')