From d879279fc1296ca0a3919b6e94e4249abf3f28db Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Mon, 13 May 2024 12:48:27 -0600 Subject: [PATCH] =?UTF-8?q?fix(nextjs):=20Moving=20a=20library=20using=20@?= =?UTF-8?q?nx/workspace:move=20should=20update=20=E2=80=A6=20(#23311)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Current When you using `@nx/workspace:move` after create a Next.js library the server path remains unchanged. ## Expected The server path is changed as well as the main entry point for the library path. Fixes: #20821 --- .../move/lib/update-imports.spec.ts | 37 +++++++++++++++++++ .../src/generators/move/lib/update-imports.ts | 28 ++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/packages/workspace/src/generators/move/lib/update-imports.spec.ts b/packages/workspace/src/generators/move/lib/update-imports.spec.ts index 49139aa146ebe..cff2940ca9fd9 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.spec.ts @@ -493,4 +493,41 @@ export MyExtendedClass extends MyClass {};` '@proj/my-source': ['my-destination/src/index.ts'], }); }); + + it("should update project ref in the root tsconfig file if it contains a secondary entry point for Next.js's server", async () => { + await libraryGenerator(tree, { + name: 'my-source', + projectNameAndRootFormat: 'as-provided', + }); + + tree.write('my-source/src/server.ts', ''); + + updateJson(tree, '/tsconfig.base.json', (json) => { + json.compilerOptions.paths['@proj/my-source/server'] = [ + 'my-source/src/server.ts', + ]; + return json; + }); + + const projectConfig = readProjectConfiguration(tree, 'my-source'); + updateImports( + tree, + await normalizeSchema( + tree, + { + ...schema, + updateImportPath: false, + }, + projectConfig + ), + + projectConfig + ); + + const tsConfig = readJson(tree, '/tsconfig.base.json'); + expect(tsConfig.compilerOptions.paths).toEqual({ + '@proj/my-source': ['my-destination/src/index.ts'], + '@proj/my-source/server': ['my-destination/src/server.ts'], + }); + }); }); diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index e5c3df0f3d9f0..712fd0dc2c37a 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -47,6 +47,7 @@ export function updateImports( let tsConfig: any; let mainEntryPointImportPath: string; let secondaryEntryPointImportPaths: string[]; + let serverEntryPointImportPath: string; if (tree.exists(tsConfigPath)) { tsConfig = readJson(tree, tsConfigPath); const sourceRoot = @@ -68,6 +69,19 @@ export function updateImports( !x.startsWith(ensureTrailingSlash(sourceRoot)) ) ); + + // Next.js libs have a custom path for the server we need to update that as well + // example "paths": { @acme/lib/server : ['libs/lib/src/server.ts'] } + serverEntryPointImportPath = Object.keys( + tsConfig.compilerOptions?.paths ?? {} + ).find((path) => + tsConfig.compilerOptions.paths[path].some( + (x) => + x.startsWith(ensureTrailingSlash(sourceRoot)) && + x.includes('server') && + path.endsWith('server') + ) + ); } mainEntryPointImportPath ??= normalizePathSlashes( @@ -94,6 +108,20 @@ export function updateImports( })), ]; + if ( + serverEntryPointImportPath && + schema.importPath && + serverEntryPointImportPath.startsWith(mainEntryPointImportPath) + ) { + projectRefs.push({ + from: serverEntryPointImportPath, + to: serverEntryPointImportPath.replace( + mainEntryPointImportPath, + schema.importPath + ), + }); + } + for (const projectRef of projectRefs) { if (schema.updateImportPath && projectRef.to) { const replaceProjectRef = new RegExp(projectRef.from, 'g');