From 4c7093c3a22996bbf08208c3021767a7477ed555 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 21 Apr 2023 15:11:03 -0400 Subject: [PATCH] fix(nextjs): support workspace libs with standalone Next.js app --- .../application/lib/create-application-files.ts | 4 ++++ packages/next/src/generators/library/library.spec.ts | 4 ++-- packages/next/src/generators/library/library.ts | 11 ++++++++--- packages/react/src/generators/library/library.ts | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/next/src/generators/application/lib/create-application-files.ts b/packages/next/src/generators/application/lib/create-application-files.ts index d70728da10ce52..ae9fe71e7e1410 100644 --- a/packages/next/src/generators/application/lib/create-application-files.ts +++ b/packages/next/src/generators/application/lib/create-application-files.ts @@ -63,6 +63,10 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) { let { extends: _, ...updatedJson } = json; + // Don't generate the `paths` object or else workspace libs will not work later. + // It'll be generated as needed when a lib is first added. + delete json.compilerOptions.paths; + updatedJson = { ...updateJson, compilerOptions: { diff --git a/packages/next/src/generators/library/library.spec.ts b/packages/next/src/generators/library/library.spec.ts index 5417ebf7642fcf..e74c0b9d3d22b2 100644 --- a/packages/next/src/generators/library/library.spec.ts +++ b/packages/next/src/generators/library/library.spec.ts @@ -152,14 +152,14 @@ describe('next library', () => { expect(appTree.read('my-lib/src/index.ts', 'utf-8')).toContain( 'React client components' ); - expect(appTree.read('my-lib/src/server.ts', 'utf-8')).toContain( + expect(appTree.read('my-lib/src/server.tsx', 'utf-8')).toContain( 'React server components' ); expect( readJson(appTree, 'tsconfig.base.json').compilerOptions.paths ).toMatchObject({ '@proj/my-lib': ['my-lib/src/index.ts'], - '@proj/my-lib/server': ['my-lib/src/server.ts'], + '@proj/my-lib/server': ['my-lib/src/server.tsx'], }); }); }); diff --git a/packages/next/src/generators/library/library.ts b/packages/next/src/generators/library/library.ts index c548ee0d9f705e..f616600ca72566 100644 --- a/packages/next/src/generators/library/library.ts +++ b/packages/next/src/generators/library/library.ts @@ -46,15 +46,20 @@ export async function libraryGenerator(host: Tree, rawOptions: Schema) { const serverEntryPath = joinPathFragments( options.projectRoot, './src', - 'server.' + (options.js ? 'js' : 'ts') + 'server.' + (options.js ? 'js' : 'tsx') ); host.write( joinPathFragments( options.projectRoot, 'src', - `server.${options.js ? 'js' : 'ts'}` + `server.${options.js ? 'js' : 'tsx'}` ), - `// Use this file to export React server components\n` + `// Use this file to export React server components +export async function MyServerComponent() { + // Server components can perform async operations + // e.g const response = await fetch('...'); + return

Hello Server

+}` ); addTsConfigPath(host, `${options.importPath}/server`, [serverEntryPath]); diff --git a/packages/react/src/generators/library/library.ts b/packages/react/src/generators/library/library.ts index ab36bb87002403..8da30b7efbdf3b 100644 --- a/packages/react/src/generators/library/library.ts +++ b/packages/react/src/generators/library/library.ts @@ -43,7 +43,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) { ...options, e2eTestRunner: 'none', skipFormat: true, - skipBabelConfig: options.bundler === 'vite', + skipBabelConfig: options.bundler === 'vite' || options.compiler === 'swc', skipHelperLibs: options.bundler === 'vite', }); tasks.push(initTask);