Skip to content
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
7 changes: 4 additions & 3 deletions packages/schematics/angular/web-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ if (typeof Worker !== 'undefined') {
}

const webWorkerSchematic: RuleFactory<WebWorkerOptions> = createProjectSchematic(
(options, { project }) => {
(options, { project, tree }) => {
const projectType = project.extensions['projectType'];
if (projectType !== 'application') {
throw new SchematicsException(`Web Worker requires a project type of "application".`);
Expand All @@ -95,6 +95,7 @@ const webWorkerSchematic: RuleFactory<WebWorkerOptions> = createProjectSchematic
]);

const root = project.root || '';
const workerConfigPath = join(root, 'tsconfig.worker.json');
const templateSourceWorkerConfig = apply(url('./files/worker-tsconfig'), [
applyTemplates({
...options,
Expand All @@ -114,7 +115,6 @@ const webWorkerSchematic: RuleFactory<WebWorkerOptions> = createProjectSchematic
throw new Error(`Build target is not defined for this project.`);
}

const workerConfigPath = join(root, 'tsconfig.worker.json');
(buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
// The unit-test builder has no `webWorkerTsConfig` option and reads it from the build target.
if (testTarget && testTarget.builder !== Builders.BuildUnitTest) {
Expand All @@ -125,7 +125,8 @@ const webWorkerSchematic: RuleFactory<WebWorkerOptions> = createProjectSchematic
options.snippet ? addSnippet(options) : noop(),
// Add the worker.
mergeWith(templateSourceWorkerCode),
mergeWith(templateSourceWorkerConfig),
// The configuration is shared by all the workers of the project.
tree.exists(workerConfigPath) ? noop() : mergeWith(templateSourceWorkerConfig),
]);
},
);
Expand Down
17 changes: 17 additions & 0 deletions packages/schematics/angular/web-worker/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,21 @@ describe('Web Worker Schematic', () => {
const { compilerOptions } = parseJson(tree.readContent(path).toString());
expect(compilerOptions.outDir).toBe('./out-tsc/worker');
});

it(`should keep the existing 'tsconfig.worker.json' when adding another worker`, async () => {
let tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
// The file no longer matches the template once it was customized or formatted by the CLI.
const path = '/projects/bar/tsconfig.worker.json';
const customized = `{ "compilerOptions": { "lib": ["es2022", "webworker"] } }\n`;
tree.overwrite(path, customized);

tree = await schematicRunner.runSchematic(
'web-worker',
{ ...defaultOptions, name: 'other' },
tree,
);

expect(tree.exists('/projects/bar/src/app/other.worker.ts')).toBeTrue();
expect(tree.readContent(path)).toBe(customized);
});
});
Loading