From f20f5e3c2a84e39d09946ec3a4ec4af36f80c4e9 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Fri, 2 Feb 2024 11:28:40 -0700 Subject: [PATCH] fix(nextjs): vite workspace libs --- .../application/application.spec.ts | 80 ++++++++++++++++++- .../files/common/tsconfig.json__tmpl__ | 10 +-- .../lib/create-application-files.ts | 10 +++ 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/packages/next/src/generators/application/application.spec.ts b/packages/next/src/generators/application/application.spec.ts index 1be8bce2e2f4e..e5aad0b1763fa 100644 --- a/packages/next/src/generators/application/application.spec.ts +++ b/packages/next/src/generators/application/application.spec.ts @@ -104,10 +104,10 @@ describe('app', () => { const tsConfig = readJson(tree, 'tsconfig.json'); expect(tsConfig.include).toEqual([ - '**/*.ts', - '**/*.tsx', - '**/*.js', - '**/*.jsx', + 'src/**/*.ts', + 'src/**/*.tsx', + 'src/**/*.js', + 'src/**/*.jsx', '.next/types/**/*.ts', `dist/${name}/.next/types/**/*.ts`, 'next-env.d.ts', @@ -675,6 +675,78 @@ describe('app', () => { ` ); }); + + it('should scope tsconfig to the src/ project directory', async () => { + const name = uniq(); + + await applicationGenerator(tree, { + name, + style: 'css', + appDir: true, + rootProject: true, + projectNameAndRootFormat: 'as-provided', + src: true, + }); + + const tsconfigJSON = readJson(tree, `tsconfig.json`); + + expect(tsconfigJSON.include).toEqual([ + 'src/**/*.ts', + 'src/**/*.tsx', + 'src/**/*.js', + 'src/**/*.jsx', + '.next/types/**/*.ts', + `dist/${name}/.next/types/**/*.ts`, + 'next-env.d.ts', + ]); + }); + + it('should scope tsconfig to the app/ project directory', async () => { + const name = uniq(); + + await applicationGenerator(tree, { + name, + style: 'css', + appDir: true, + rootProject: true, + projectNameAndRootFormat: 'as-provided', + src: false, + }); + + const tsconfigJSON = readJson(tree, `tsconfig.json`); + + expect(tsconfigJSON.include).toEqual([ + 'app/**/*.ts', + 'app/**/*.tsx', + 'app/**/*.js', + 'app/**/*.jsx', + '.next/types/**/*.ts', + `dist/${name}/.next/types/**/*.ts`, + 'next-env.d.ts', + ]); + }); + + it('should scope tsconfig to the pages/ project directory', async () => { + const name = uniq(); + + await applicationGenerator(tree, { + name, + style: 'css', + appDir: false, + rootProject: true, + projectNameAndRootFormat: 'as-provided', + src: false, + }); + + const tsconfigJSON = readJson(tree, `tsconfig.json`); + expect(tsconfigJSON.include).toEqual([ + 'pages/**/*.ts', + 'pages/**/*.tsx', + 'pages/**/*.js', + 'pages/**/*.jsx', + 'next-env.d.ts', + ]); + }); }); }); diff --git a/packages/next/src/generators/application/files/common/tsconfig.json__tmpl__ b/packages/next/src/generators/application/files/common/tsconfig.json__tmpl__ index b6c3c4a68cf09..6ec03249199f3 100644 --- a/packages/next/src/generators/application/files/common/tsconfig.json__tmpl__ +++ b/packages/next/src/generators/application/files/common/tsconfig.json__tmpl__ @@ -15,15 +15,15 @@ "plugins": [{ "name": "next" }] }, "include": [ - "**/*.ts", - "**/*.tsx", - "**/*.js", - "**/*.jsx", + "<%= rootPath %>**/*.ts", + "<%= rootPath %>**/*.tsx", + "<%= rootPath %>**/*.js", + "<%= rootPath %>**/*.jsx", <% if (appDir) { %> "<%= layoutTypeSrcPath %>", "<%= layoutTypeDistPath %>", <% } %> "next-env.d.ts" ], - "exclude": ["node_modules", "jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"] + "exclude": ["node_modules", "jest.config.ts", "<%= rootPath %>**/*.spec.ts", "<%= rootPath %>**/*.test.ts"] } 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 47806a3fd21a0..1525f3ef23edf 100644 --- a/packages/next/src/generators/application/lib/create-application-files.ts +++ b/packages/next/src/generators/application/lib/create-application-files.ts @@ -29,6 +29,15 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) { options.outputPath, '.next/types/**/*.ts' ); + + // scope tsconfig to the project directory so that it doesn't include other projects/libs + const rootPath = options.rootProject + ? options.src + ? 'src/' + : options.appDir + ? 'app/' + : 'pages/' + : ''; const templateVariables = { ...names(options.name), ...options, @@ -36,6 +45,7 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) { tmpl: '', offsetFromRoot, layoutTypeSrcPath, + rootPath, layoutTypeDistPath, rootTsConfigPath: getRelativePathToRootTsConfig( host,