Skip to content

Commit a675bd2

Browse files
authored
fix(js): Configure typescript plugin to handle non-buildable libs (#29393)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> Currently, we are excluding non-buildable libs from the `@nx/js/typescript` plugin. Although that allows non-buildable projects from have the build target being inferred it also as a side-effect removes the `typecheck` target which is unintended. Additionally, to breaks the pattern of being self containment that TS project solutions brings as we were modifying the root `nx.json` ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> 1. Non-buildable libs should not have a build target. 2. Non-buildable libs should have the typecheck target. 3. Buildable libs remain unchanged and should have both a build and typecheck target. 4. Remove the `exclude` from `nx.json` for non-buildable libs. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 1a01346 commit a675bd2

File tree

7 files changed

+177
-119
lines changed

7 files changed

+177
-119
lines changed

e2e/react/src/react.test.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,6 @@ describe('React Applications', () => {
6262
}
6363
}, 250_000);
6464

65-
it('None buildable libs using (useTsSolution = true) should be excluded from js/ts plugin', async () => {
66-
const appName = uniq('app');
67-
const libName = uniq('lib');
68-
69-
runCLI(
70-
`generate @nx/react:app apps/${appName} --name=${appName} --useTsSolution=true --bundler=vite --no-interactive --skipFormat --linter=eslint --unitTestRunner=vitest`
71-
);
72-
runCLI(
73-
`generate @nx/react:lib ${libName} --bundler=none --no-interactive --unit-test-runner=vitest --skipFormat --linter=eslint`
74-
);
75-
76-
const nxJson = readJson('nx.json');
77-
78-
const jsTypescriptPlugin = nxJson.plugins.find(
79-
(plugin) => plugin.plugin === '@nx/js/typescript'
80-
);
81-
expect(jsTypescriptPlugin).toBeDefined();
82-
83-
expect(jsTypescriptPlugin.exclude.includes(`${libName}/*`)).toBeTruthy();
84-
}, 250_000);
85-
8665
it('should be able to use Rspack to build and test apps', async () => {
8766
const appName = uniq('app');
8867
const libName = uniq('lib');

packages/js/src/generators/library/library.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ import { addSwcConfig } from '../../utils/swc/add-swc-config';
4343
import { getSwcDependencies } from '../../utils/swc/add-swc-dependencies';
4444
import { getNeededCompilerOptionOverrides } from '../../utils/typescript/configuration';
4545
import { tsConfigBaseOptions } from '../../utils/typescript/create-ts-config';
46-
import {
47-
ensureProjectIsExcludedFromPluginRegistrations,
48-
ensureProjectIsIncludedInPluginRegistrations,
49-
} from '../../utils/typescript/plugin';
46+
import { ensureProjectIsIncludedInPluginRegistrations } from '../../utils/typescript/plugin';
5047
import {
5148
addTsConfigPath,
5249
getRelativePathToRootTsConfig,
@@ -255,14 +252,7 @@ async function configureProject(
255252
) {
256253
if (options.hasPlugin) {
257254
const nxJson = readNxJson(tree);
258-
if (options.bundler === 'none') {
259-
ensureProjectIsExcludedFromPluginRegistrations(
260-
nxJson,
261-
options.projectRoot
262-
);
263-
} else {
264-
ensureProjectIsIncludedInPluginRegistrations(nxJson, options.projectRoot);
265-
}
255+
ensureProjectIsIncludedInPluginRegistrations(nxJson, options.projectRoot);
266256
updateNxJson(tree, nxJson);
267257
}
268258

packages/js/src/plugins/typescript/plugin.spec.ts

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,9 +1794,9 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
17941794
// Sibling package.json
17951795
await applyFilesToTempFsAndContext(tempFs, context, {
17961796
'libs/my-lib/tsconfig.json': `{}`,
1797-
'libs/my-lib/tsconfig.lib.json': `{}`,
1797+
'libs/my-lib/tsconfig.lib.json': `{"compilerOptions": {"rootDir": "src"}}`,
17981798
'libs/my-lib/tsconfig.build.json': `{}`,
1799-
'libs/my-lib/package.json': `{}`,
1799+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
18001800
});
18011801
expect(
18021802
await invokeCreateNodesOnMatchingFiles(context, {
@@ -1855,8 +1855,8 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
18551855

18561856
// Sibling project.json
18571857
await applyFilesToTempFsAndContext(tempFs, context, {
1858-
'libs/my-lib/tsconfig.json': `{}`,
1859-
'libs/my-lib/tsconfig.lib.json': `{}`,
1858+
'libs/my-lib/tsconfig.json': `{"compilerOptions": {"rootDir": "src"}}`,
1859+
'libs/my-lib/tsconfig.lib.json': `{"compilerOptions": {"rootDir": "src"}}`,
18601860
'libs/my-lib/tsconfig.build.json': `{}`,
18611861
'libs/my-lib/project.json': `{}`,
18621862
});
@@ -1920,9 +1920,9 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
19201920
// Sibling package.json
19211921
await applyFilesToTempFsAndContext(tempFs, context, {
19221922
'libs/my-lib/tsconfig.json': `{}`,
1923-
'libs/my-lib/tsconfig.lib.json': `{}`,
1923+
'libs/my-lib/tsconfig.lib.json': `{"compilerOptions": {"rootDir": "src"}}`,
19241924
'libs/my-lib/tsconfig.build.json': `{}`,
1925-
'libs/my-lib/package.json': `{}`,
1925+
'libs/my-lib/package.json': `{ "main": "dist/index.js" }`,
19261926
});
19271927
expect(
19281928
await invokeCreateNodesOnMatchingFiles(context, {
@@ -1987,7 +1987,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
19871987
await applyFilesToTempFsAndContext(tempFs, context, {
19881988
'libs/my-lib/tsconfig.json': `{}`,
19891989
'libs/my-lib/tsconfig.lib.json': `{}`,
1990-
'libs/my-lib/tsconfig.build.json': `{}`,
1990+
'libs/my-lib/tsconfig.build.json': `{"compilerOptions": {"rootDir": "src"}}`,
19911991
'libs/my-lib/project.json': `{}`,
19921992
});
19931993
expect(
@@ -2069,11 +2069,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
20692069
it('should add the config file and the `include` and `exclude` patterns', async () => {
20702070
await applyFilesToTempFsAndContext(tempFs, context, {
20712071
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2072+
compilerOptions: {
2073+
rootDir: 'src',
2074+
},
20722075
include: ['src/**/*.ts'],
20732076
exclude: ['src/**/*.spec.ts'],
20742077
}),
20752078
'libs/my-lib/tsconfig.json': `{}`,
2076-
'libs/my-lib/package.json': `{}`,
2079+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
20772080
});
20782081
expect(
20792082
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2132,7 +2135,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
21322135
`);
21332136
});
21342137

2135-
it('should add extended config files', async () => {
2138+
it('should be able to extended config files', async () => {
21362139
await applyFilesToTempFsAndContext(tempFs, context, {
21372140
'tsconfig.base.json': JSON.stringify({
21382141
exclude: ['node_modules', 'tmp'],
@@ -2144,8 +2147,11 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
21442147
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
21452148
extends: '../../tsconfig.foo.json',
21462149
include: ['src/**/*.ts'],
2150+
compilerOptions: {
2151+
rootDir: 'src',
2152+
},
21472153
}),
2148-
'libs/my-lib/package.json': `{}`,
2154+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
21492155
});
21502156
expect(
21512157
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2219,9 +2225,12 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
22192225
'libs/my-lib/tsconfig.json': '{}',
22202226
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
22212227
extends: '../../tsconfig.foo.json',
2228+
compilerOptions: {
2229+
rootDir: 'src',
2230+
},
22222231
include: ['src/**/*.ts'],
22232232
}),
2224-
'libs/my-lib/package.json': `{}`,
2233+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
22252234
});
22262235
// simulate @tsconfig/strictest package
22272236
tempFs.createFilesSync({
@@ -2293,6 +2302,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
22932302
await applyFilesToTempFsAndContext(tempFs, context, {
22942303
'libs/my-lib/tsconfig.json': '{}',
22952304
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2305+
compilerOptions: { rootDir: 'src' },
22962306
include: ['src/**/*.ts'],
22972307
exclude: ['src/**/foo.ts'], // should be ignored because a referenced internal project includes this same pattern
22982308
references: [
@@ -2306,7 +2316,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
23062316
'libs/other-lib/tsconfig.json': JSON.stringify({
23072317
include: ['**/*.ts'], // different pattern that should not be included because it's an external project
23082318
}),
2309-
'libs/my-lib/package.json': `{}`,
2319+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`, // Should be defined so that the project is considered buildable
23102320
});
23112321
expect(
23122322
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2374,14 +2384,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
23742384
await applyFilesToTempFsAndContext(tempFs, context, {
23752385
'libs/my-lib/tsconfig.json': '{}',
23762386
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2387+
compilerOptions: { rootDir: 'src' }, // rootDir is required to determine if the project is buildable
23772388
include: ['src/**/*.ts'],
23782389
exclude: ['src/**/foo.ts'], // should be ignored
23792390
references: [{ path: './tsconfig.other.json' }],
23802391
}),
23812392
'libs/my-lib/tsconfig.other.json': JSON.stringify({
23822393
include: ['other/**/*.ts', 'src/**/foo.ts'],
23832394
}),
2384-
'libs/my-lib/package.json': `{}`,
2395+
'libs/my-lib/package.json': `{"main": "dist/index.js" }`,
23852396
});
23862397

23872398
let result = await invokeCreateNodesOnMatchingFiles(context, {
@@ -2409,14 +2420,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
24092420
await applyFilesToTempFsAndContext(tempFs, context, {
24102421
'libs/my-lib/tsconfig.json': '{}',
24112422
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2423+
compilerOptions: { rootDir: 'src' },
24122424
include: ['**/*.ts'],
24132425
exclude: ['**/foo.ts'], // should be ignored
24142426
references: [{ path: './tsconfig.other.json' }],
24152427
}),
24162428
'libs/my-lib/tsconfig.other.json': JSON.stringify({
24172429
include: ['other/**/*.ts', 'src/**/foo.ts'],
24182430
}),
2419-
'libs/my-lib/package.json': `{}`,
2431+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
24202432
});
24212433
result = await invokeCreateNodesOnMatchingFiles(context, {
24222434
typecheck: false,
@@ -2443,14 +2455,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
24432455
await applyFilesToTempFsAndContext(tempFs, context, {
24442456
'libs/my-lib/tsconfig.json': '{}',
24452457
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2458+
compilerOptions: { rootDir: 'src' }, // rooDir is required to determine if the project is buildable
24462459
include: ['src/**/*.ts'],
24472460
exclude: ['src/**/foo.ts'], // should be ignored
24482461
references: [{ path: './tsconfig.other.json' }],
24492462
}),
24502463
'libs/my-lib/tsconfig.other.json': JSON.stringify({
24512464
include: ['other/**/*.ts', '**/foo.ts'],
24522465
}),
2453-
'libs/my-lib/package.json': `{}`,
2466+
'libs/my-lib/package.json': `{"main": "dist/index.js" }`, // Should be defined so that the project is considered buildable
24542467
});
24552468
result = await invokeCreateNodesOnMatchingFiles(context, {
24562469
typecheck: false,
@@ -2477,14 +2490,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
24772490
await applyFilesToTempFsAndContext(tempFs, context, {
24782491
'libs/my-lib/tsconfig.json': '{}',
24792492
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2493+
compilerOptions: { rootDir: 'src' },
24802494
include: ['src/**/*.ts'],
24812495
exclude: ['src/**/foo.ts'], // should be ignored
24822496
references: [{ path: './tsconfig.other.json' }],
24832497
}),
24842498
'libs/my-lib/tsconfig.other.json': JSON.stringify({
24852499
include: ['./other/**/*.ts', './**/foo.ts'],
24862500
}),
2487-
'libs/my-lib/package.json': `{}`,
2501+
'libs/my-lib/package.json': `{"main": "dist/index.js" }`,
24882502
});
24892503
result = await invokeCreateNodesOnMatchingFiles(context, {
24902504
typecheck: false,
@@ -2511,6 +2525,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
25112525
await applyFilesToTempFsAndContext(tempFs, context, {
25122526
'libs/my-lib/tsconfig.json': '{}',
25132527
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2528+
compilerOptions: { rootDir: 'src' },
25142529
include: ['src/**/*.ts'],
25152530
exclude: [
25162531
'src/**/foo.ts', // should be ignored
@@ -2521,7 +2536,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
25212536
'libs/my-lib/tsconfig.other.json': JSON.stringify({
25222537
include: ['other/**/*.ts', 'src/**/foo.ts'],
25232538
}),
2524-
'libs/my-lib/package.json': `{}`,
2539+
'libs/my-lib/package.json': `{"main": "dist/index.js" }`,
25252540
});
25262541
result = await invokeCreateNodesOnMatchingFiles(context, {
25272542
typecheck: false,
@@ -2549,10 +2564,11 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
25492564
it('should fall back to named inputs when not using include', async () => {
25502565
await applyFilesToTempFsAndContext(tempFs, context, {
25512566
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2567+
compilerOptions: { rootDir: 'src' },
25522568
files: ['main.ts'],
25532569
}),
25542570
'libs/my-lib/tsconfig.json': `{}`,
2555-
'libs/my-lib/package.json': `{}`,
2571+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
25562572
});
25572573
expect(
25582574
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2628,11 +2644,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
26282644
it('should add the `outFile`', async () => {
26292645
await applyFilesToTempFsAndContext(tempFs, context, {
26302646
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2631-
compilerOptions: { outFile: '../../dist/libs/my-lib/index.js' },
2647+
compilerOptions: {
2648+
outFile: '../../dist/libs/my-lib/index.js',
2649+
rootDir: 'src',
2650+
},
26322651
files: ['main.ts'],
26332652
}),
26342653
'libs/my-lib/tsconfig.json': `{}`,
2635-
'libs/my-lib/package.json': `{}`,
2654+
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
26362655
});
26372656
expect(
26382657
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2698,11 +2717,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
26982717
it('should add the `outDir`', async () => {
26992718
await applyFilesToTempFsAndContext(tempFs, context, {
27002719
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2701-
compilerOptions: { outDir: '../../dist/libs/my-lib' },
2720+
compilerOptions: {
2721+
outDir: '../../dist/libs/my-lib',
2722+
rootDir: 'src',
2723+
},
27022724
files: ['main.ts'],
27032725
}),
27042726
'libs/my-lib/tsconfig.json': `{}`,
2705-
'libs/my-lib/package.json': `{}`,
2727+
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
27062728
});
27072729
expect(
27082730
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2764,6 +2786,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
27642786
it('should add the inline output files when `outDir` is not defined', async () => {
27652787
await applyFilesToTempFsAndContext(tempFs, context, {
27662788
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2789+
compilerOptions: { rootDir: 'src' },
27672790
files: ['main.ts'],
27682791
}),
27692792
'libs/my-lib/tsconfig.json': `{}`,
@@ -2842,15 +2865,21 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
28422865
await applyFilesToTempFsAndContext(tempFs, context, {
28432866
'libs/my-lib/tsconfig.json': '{}',
28442867
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
2845-
compilerOptions: { outFile: '../../dist/libs/my-lib/lib.js' },
2868+
compilerOptions: {
2869+
outFile: '../../dist/libs/my-lib/lib.js',
2870+
rootDir: 'src',
2871+
},
28462872
files: ['main.ts'],
28472873
references: [{ path: './tsconfig.other.json' }],
28482874
}),
28492875
'libs/my-lib/tsconfig.other.json': JSON.stringify({
2850-
compilerOptions: { outDir: '../../dist/libs/my-lib/other' },
2876+
compilerOptions: {
2877+
outDir: '../../dist/libs/my-lib/other',
2878+
rootDir: 'src',
2879+
},
28512880
include: ['other/**/*.ts'],
28522881
}),
2853-
'libs/my-lib/package.json': `{}`,
2882+
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/lib.js"}`,
28542883
});
28552884
expect(
28562885
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2923,11 +2952,12 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
29232952
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
29242953
compilerOptions: {
29252954
outFile: '../../dist/libs/my-lib/index.js',
2955+
rootDir: 'src',
29262956
tsBuildInfoFile: '../../dist/libs/my-lib/my-lib.tsbuildinfo',
29272957
},
29282958
files: ['main.ts'],
29292959
}),
2930-
'libs/my-lib/package.json': `{}`,
2960+
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
29312961
});
29322962
expect(
29332963
await invokeCreateNodesOnMatchingFiles(context, {
@@ -2994,11 +3024,12 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
29943024
'libs/my-lib/tsconfig.json': '{}',
29953025
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
29963026
compilerOptions: {
3027+
rootDir: 'src',
29973028
tsBuildInfoFile: '../../dist/libs/my-lib/my-lib.tsbuildinfo',
29983029
},
29993030
files: ['main.ts'],
30003031
}),
3001-
'libs/my-lib/package.json': `{}`,
3032+
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
30023033
});
30033034
expect(
30043035
await invokeCreateNodesOnMatchingFiles(context, {
@@ -3073,13 +3104,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
30733104
await applyFilesToTempFsAndContext(tempFs, context, {
30743105
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
30753106
compilerOptions: {
3107+
rootDir: 'src',
30763108
outDir: 'dist',
30773109
tsBuildInfoFile: 'my-lib.tsbuildinfo',
30783110
},
30793111
files: ['main.ts'],
30803112
}),
30813113
'libs/my-lib/tsconfig.json': `{}`,
3082-
'libs/my-lib/package.json': `{}`,
3114+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
30833115
});
30843116

30853117
expect(
@@ -3145,12 +3177,13 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
31453177
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
31463178
compilerOptions: {
31473179
outDir: 'dist',
3180+
rootDir: 'src',
31483181
tsBuildInfoFile: 'dist/my-lib.tsbuildinfo',
31493182
},
31503183
files: ['main.ts'],
31513184
}),
31523185
'libs/my-lib/tsconfig.json': `{}`,
3153-
'libs/my-lib/package.json': `{}`,
3186+
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
31543187
});
31553188

31563189
expect(

0 commit comments

Comments
 (0)