Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): update tsconfig included and excluded files when generating a library secondary entry point #19366

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './add-files';
export * from './add-path-mapping';
export * from './normalize-options';
export * from './update-linting-file-patterns';
export * from './update-tsconfig-included-files';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { joinPathFragments, updateJson, type Tree } from '@nx/devkit';
import type { NormalizedGeneratorOptions } from '../schema';

export function updateTsConfigIncludedFiles(
tree: Tree,
options: NormalizedGeneratorOptions
): void {
const candidateTsConfigPaths = [
options.libraryProject.targets?.build?.options?.tsConfig,
joinPathFragments(options.libraryProject.root, 'tsconfig.lib.json'),
joinPathFragments(options.libraryProject.root, 'tsconfig.json'),
];

const tsConfigPath = candidateTsConfigPaths.find(
(path) => path && tree.exists(path)
);
if (!tsConfigPath) {
// ignore if the library has a custom tsconfig setup
return;
}

updateJson(tree, tsConfigPath, (json) => {
if (json.include?.length) {
json.include = json.include.map((path: string) =>
path.replace(/^(?:\.\/)?src\//, '')
);
}

if (json.exclude?.length) {
json.exclude = json.exclude.map((path: string) =>
path.replace(/^(?:\.\/)?src\//, '')
);
}

return json;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Tree,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { generateTestLibrary } from '../utils/testing';
import { librarySecondaryEntryPointGenerator } from './library-secondary-entry-point';

describe('librarySecondaryEntryPoint generator', () => {
Expand Down Expand Up @@ -180,6 +181,38 @@ describe('librarySecondaryEntryPoint generator', () => {
);
});

it('should update the tsconfig "include" and "exclude" options', async () => {
await generateTestLibrary(tree, {
name: 'lib1',
directory: 'libs/lib1',
importPath: '@my-org/lib1',
publishable: true,
});
// verify initial state
let tsConfig = readJson(tree, 'libs/lib1/tsconfig.lib.json');
expect(tsConfig.include).toStrictEqual(['src/**/*.ts']);
expect(tsConfig.exclude).toStrictEqual([
'src/**/*.spec.ts',
'src/test-setup.ts',
'jest.config.ts',
'src/**/*.test.ts',
]);

await librarySecondaryEntryPointGenerator(tree, {
name: 'testing',
library: 'lib1',
});

tsConfig = readJson(tree, 'libs/lib1/tsconfig.lib.json');
expect(tsConfig.include).toStrictEqual(['**/*.ts']);
expect(tsConfig.exclude).toStrictEqual([
'**/*.spec.ts',
'test-setup.ts',
'jest.config.ts',
'**/*.test.ts',
]);
});

it('should format files', async () => {
jest.spyOn(devkit, 'formatFiles');
addProjectConfiguration(tree, 'lib1', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
addPathMapping,
normalizeOptions,
updateLintingFilePatterns,
updateTsConfigIncludedFiles,
} from './lib';
import { GeneratorOptions } from './schema';

Expand All @@ -15,6 +16,7 @@ export async function librarySecondaryEntryPointGenerator(

addFiles(tree, options);
addPathMapping(tree, options);
updateTsConfigIncludedFiles(tree, options);
updateLintingFilePatterns(tree, options);

await formatFiles(tree);
Expand Down