diff --git a/packages/schematics/angular/web-worker/index.ts b/packages/schematics/angular/web-worker/index.ts index 9718f0407c39..b42f1c877d51 100644 --- a/packages/schematics/angular/web-worker/index.ts +++ b/packages/schematics/angular/web-worker/index.ts @@ -76,7 +76,7 @@ if (typeof Worker !== 'undefined') { } const webWorkerSchematic: RuleFactory = 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".`); @@ -95,6 +95,7 @@ const webWorkerSchematic: RuleFactory = createProjectSchematic ]); const root = project.root || ''; + const workerConfigPath = join(root, 'tsconfig.worker.json'); const templateSourceWorkerConfig = apply(url('./files/worker-tsconfig'), [ applyTemplates({ ...options, @@ -114,7 +115,6 @@ const webWorkerSchematic: RuleFactory = 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) { @@ -125,7 +125,8 @@ const webWorkerSchematic: RuleFactory = 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), ]); }, ); diff --git a/packages/schematics/angular/web-worker/index_spec.ts b/packages/schematics/angular/web-worker/index_spec.ts index 0f59ed82f0b9..c100bda5b5e1 100644 --- a/packages/schematics/angular/web-worker/index_spec.ts +++ b/packages/schematics/angular/web-worker/index_spec.ts @@ -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); + }); });