Skip to content

Commit

Permalink
fix(js): Configure typescript plugin to handle non-buildable libs (#2…
Browse files Browse the repository at this point in the history
…9393)

<!-- 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 #
  • Loading branch information
ndcunningham authored Dec 18, 2024
1 parent 1a01346 commit a675bd2
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 119 deletions.
21 changes: 0 additions & 21 deletions e2e/react/src/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,6 @@ describe('React Applications', () => {
}
}, 250_000);

it('None buildable libs using (useTsSolution = true) should be excluded from js/ts plugin', async () => {
const appName = uniq('app');
const libName = uniq('lib');

runCLI(
`generate @nx/react:app apps/${appName} --name=${appName} --useTsSolution=true --bundler=vite --no-interactive --skipFormat --linter=eslint --unitTestRunner=vitest`
);
runCLI(
`generate @nx/react:lib ${libName} --bundler=none --no-interactive --unit-test-runner=vitest --skipFormat --linter=eslint`
);

const nxJson = readJson('nx.json');

const jsTypescriptPlugin = nxJson.plugins.find(
(plugin) => plugin.plugin === '@nx/js/typescript'
);
expect(jsTypescriptPlugin).toBeDefined();

expect(jsTypescriptPlugin.exclude.includes(`${libName}/*`)).toBeTruthy();
}, 250_000);

it('should be able to use Rspack to build and test apps', async () => {
const appName = uniq('app');
const libName = uniq('lib');
Expand Down
14 changes: 2 additions & 12 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { getSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { getNeededCompilerOptionOverrides } from '../../utils/typescript/configuration';
import { tsConfigBaseOptions } from '../../utils/typescript/create-ts-config';
import {
ensureProjectIsExcludedFromPluginRegistrations,
ensureProjectIsIncludedInPluginRegistrations,
} from '../../utils/typescript/plugin';
import { ensureProjectIsIncludedInPluginRegistrations } from '../../utils/typescript/plugin';
import {
addTsConfigPath,
getRelativePathToRootTsConfig,
Expand Down Expand Up @@ -255,14 +252,7 @@ async function configureProject(
) {
if (options.hasPlugin) {
const nxJson = readNxJson(tree);
if (options.bundler === 'none') {
ensureProjectIsExcludedFromPluginRegistrations(
nxJson,
options.projectRoot
);
} else {
ensureProjectIsIncludedInPluginRegistrations(nxJson, options.projectRoot);
}
ensureProjectIsIncludedInPluginRegistrations(nxJson, options.projectRoot);
updateNxJson(tree, nxJson);
}

Expand Down
91 changes: 62 additions & 29 deletions packages/js/src/plugins/typescript/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,9 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
// Sibling package.json
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/tsconfig.lib.json': `{}`,
'libs/my-lib/tsconfig.lib.json': `{"compilerOptions": {"rootDir": "src"}}`,
'libs/my-lib/tsconfig.build.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -1855,8 +1855,8 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {

// Sibling project.json
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/tsconfig.lib.json': `{}`,
'libs/my-lib/tsconfig.json': `{"compilerOptions": {"rootDir": "src"}}`,
'libs/my-lib/tsconfig.lib.json': `{"compilerOptions": {"rootDir": "src"}}`,
'libs/my-lib/tsconfig.build.json': `{}`,
'libs/my-lib/project.json': `{}`,
});
Expand Down Expand Up @@ -1920,9 +1920,9 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
// Sibling package.json
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/tsconfig.lib.json': `{}`,
'libs/my-lib/tsconfig.lib.json': `{"compilerOptions": {"rootDir": "src"}}`,
'libs/my-lib/tsconfig.build.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{ "main": "dist/index.js" }`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -1987,7 +1987,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/tsconfig.lib.json': `{}`,
'libs/my-lib/tsconfig.build.json': `{}`,
'libs/my-lib/tsconfig.build.json': `{"compilerOptions": {"rootDir": "src"}}`,
'libs/my-lib/project.json': `{}`,
});
expect(
Expand Down Expand Up @@ -2069,11 +2069,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
it('should add the config file and the `include` and `exclude` patterns', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: {
rootDir: 'src',
},
include: ['src/**/*.ts'],
exclude: ['src/**/*.spec.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2132,7 +2135,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
`);
});

it('should add extended config files', async () => {
it('should be able to extended config files', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'tsconfig.base.json': JSON.stringify({
exclude: ['node_modules', 'tmp'],
Expand All @@ -2144,8 +2147,11 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
extends: '../../tsconfig.foo.json',
include: ['src/**/*.ts'],
compilerOptions: {
rootDir: 'src',
},
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2219,9 +2225,12 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
extends: '../../tsconfig.foo.json',
compilerOptions: {
rootDir: 'src',
},
include: ['src/**/*.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});
// simulate @tsconfig/strictest package
tempFs.createFilesSync({
Expand Down Expand Up @@ -2293,6 +2302,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' },
include: ['src/**/*.ts'],
exclude: ['src/**/foo.ts'], // should be ignored because a referenced internal project includes this same pattern
references: [
Expand All @@ -2306,7 +2316,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/other-lib/tsconfig.json': JSON.stringify({
include: ['**/*.ts'], // different pattern that should not be included because it's an external project
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`, // Should be defined so that the project is considered buildable
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2374,14 +2384,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' }, // rootDir is required to determine if the project is buildable
include: ['src/**/*.ts'],
exclude: ['src/**/foo.ts'], // should be ignored
references: [{ path: './tsconfig.other.json' }],
}),
'libs/my-lib/tsconfig.other.json': JSON.stringify({
include: ['other/**/*.ts', 'src/**/foo.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js" }`,
});

let result = await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2409,14 +2420,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' },
include: ['**/*.ts'],
exclude: ['**/foo.ts'], // should be ignored
references: [{ path: './tsconfig.other.json' }],
}),
'libs/my-lib/tsconfig.other.json': JSON.stringify({
include: ['other/**/*.ts', 'src/**/foo.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});
result = await invokeCreateNodesOnMatchingFiles(context, {
typecheck: false,
Expand All @@ -2443,14 +2455,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' }, // rooDir is required to determine if the project is buildable
include: ['src/**/*.ts'],
exclude: ['src/**/foo.ts'], // should be ignored
references: [{ path: './tsconfig.other.json' }],
}),
'libs/my-lib/tsconfig.other.json': JSON.stringify({
include: ['other/**/*.ts', '**/foo.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js" }`, // Should be defined so that the project is considered buildable
});
result = await invokeCreateNodesOnMatchingFiles(context, {
typecheck: false,
Expand All @@ -2477,14 +2490,15 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' },
include: ['src/**/*.ts'],
exclude: ['src/**/foo.ts'], // should be ignored
references: [{ path: './tsconfig.other.json' }],
}),
'libs/my-lib/tsconfig.other.json': JSON.stringify({
include: ['./other/**/*.ts', './**/foo.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js" }`,
});
result = await invokeCreateNodesOnMatchingFiles(context, {
typecheck: false,
Expand All @@ -2511,6 +2525,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' },
include: ['src/**/*.ts'],
exclude: [
'src/**/foo.ts', // should be ignored
Expand All @@ -2521,7 +2536,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/my-lib/tsconfig.other.json': JSON.stringify({
include: ['other/**/*.ts', 'src/**/foo.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js" }`,
});
result = await invokeCreateNodesOnMatchingFiles(context, {
typecheck: false,
Expand Down Expand Up @@ -2549,10 +2564,11 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
it('should fall back to named inputs when not using include', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' },
files: ['main.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2628,11 +2644,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
it('should add the `outFile`', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { outFile: '../../dist/libs/my-lib/index.js' },
compilerOptions: {
outFile: '../../dist/libs/my-lib/index.js',
rootDir: 'src',
},
files: ['main.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2698,11 +2717,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
it('should add the `outDir`', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { outDir: '../../dist/libs/my-lib' },
compilerOptions: {
outDir: '../../dist/libs/my-lib',
rootDir: 'src',
},
files: ['main.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2764,6 +2786,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
it('should add the inline output files when `outDir` is not defined', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { rootDir: 'src' },
files: ['main.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
Expand Down Expand Up @@ -2842,15 +2865,21 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { outFile: '../../dist/libs/my-lib/lib.js' },
compilerOptions: {
outFile: '../../dist/libs/my-lib/lib.js',
rootDir: 'src',
},
files: ['main.ts'],
references: [{ path: './tsconfig.other.json' }],
}),
'libs/my-lib/tsconfig.other.json': JSON.stringify({
compilerOptions: { outDir: '../../dist/libs/my-lib/other' },
compilerOptions: {
outDir: '../../dist/libs/my-lib/other',
rootDir: 'src',
},
include: ['other/**/*.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/lib.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2923,11 +2952,12 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: {
outFile: '../../dist/libs/my-lib/index.js',
rootDir: 'src',
tsBuildInfoFile: '../../dist/libs/my-lib/my-lib.tsbuildinfo',
},
files: ['main.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -2994,11 +3024,12 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/my-lib/tsconfig.json': '{}',
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: {
rootDir: 'src',
tsBuildInfoFile: '../../dist/libs/my-lib/my-lib.tsbuildinfo',
},
files: ['main.ts'],
}),
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/libs/my-lib/index.js"}`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, {
Expand Down Expand Up @@ -3073,13 +3104,14 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: {
rootDir: 'src',
outDir: 'dist',
tsBuildInfoFile: 'my-lib.tsbuildinfo',
},
files: ['main.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});

expect(
Expand Down Expand Up @@ -3145,12 +3177,13 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
tsBuildInfoFile: 'dist/my-lib.tsbuildinfo',
},
files: ['main.ts'],
}),
'libs/my-lib/tsconfig.json': `{}`,
'libs/my-lib/package.json': `{}`,
'libs/my-lib/package.json': `{"main": "dist/index.js"}`,
});

expect(
Expand Down
Loading

0 comments on commit a675bd2

Please sign in to comment.