Skip to content

Commit

Permalink
fix(nextjs): Moving a library using @nx/workspace:move should update …
Browse files Browse the repository at this point in the history
…server path

closes: #20821
  • Loading branch information
ndcunningham committed May 10, 2024
1 parent e4223b3 commit e0c42b9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/workspace/src/generators/move/lib/update-imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
});
});
26 changes: 26 additions & 0 deletions packages/workspace/src/generators/move/lib/update-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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(
Expand All @@ -92,6 +106,18 @@ export function updateImports(
? p.replace(mainEntryPointImportPath, schema.importPath)
: null,
})),
{
from: serverEntryPointImportPath,
to:
schema.importPath &&
serverEntryPointImportPath &&
serverEntryPointImportPath.startsWith(mainEntryPointImportPath)
? serverEntryPointImportPath.replace(
mainEntryPointImportPath,
schema.importPath
)
: null,
},
];

for (const projectRef of projectRefs) {
Expand Down

0 comments on commit e0c42b9

Please sign in to comment.