Skip to content

Commit 56945a0

Browse files
committed
fix(@schematics/angular): keep existing worker tsconfig when generating another web worker
second `ng g web-worker` failed with `A merge conflicted on path "/tsconfig.worker.json"` in any workspace from `ng new` since 21.2.0, CLI formats first file with prettier and it no longer matches template config is shared by all workers of project, so it is now written only when file is missing
1 parent ef600f2 commit 56945a0

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎packages/schematics/angular/web-worker/index.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if (typeof Worker !== 'undefined') {
7676
}
7777

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

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

117-
const workerConfigPath = join(root, 'tsconfig.worker.json');
118118
(buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
119119
// The unit-test builder has no `webWorkerTsConfig` option and reads it from the build target.
120120
if (testTarget && testTarget.builder !== Builders.BuildUnitTest) {
@@ -125,7 +125,8 @@ const webWorkerSchematic: RuleFactory<WebWorkerOptions> = createProjectSchematic
125125
options.snippet ? addSnippet(options) : noop(),
126126
// Add the worker.
127127
mergeWith(templateSourceWorkerCode),
128-
mergeWith(templateSourceWorkerConfig),
128+
// The configuration is shared by all the workers of the project.
129+
tree.exists(workerConfigPath) ? noop() : mergeWith(templateSourceWorkerConfig),
129130
]);
130131
},
131132
);

‎packages/schematics/angular/web-worker/index_spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,21 @@ describe('Web Worker Schematic', () => {
147147
const { compilerOptions } = parseJson(tree.readContent(path).toString());
148148
expect(compilerOptions.outDir).toBe('./out-tsc/worker');
149149
});
150+
151+
it(`should keep the existing 'tsconfig.worker.json' when adding another worker`, async () => {
152+
let tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
153+
// The file no longer matches the template once it was customized or formatted by the CLI.
154+
const path = '/projects/bar/tsconfig.worker.json';
155+
const customized = `{ "compilerOptions": { "lib": ["es2022", "webworker"] } }\n`;
156+
tree.overwrite(path, customized);
157+
158+
tree = await schematicRunner.runSchematic(
159+
'web-worker',
160+
{ ...defaultOptions, name: 'other' },
161+
tree,
162+
);
163+
164+
expect(tree.exists('/projects/bar/src/app/other.worker.ts')).toBeTrue();
165+
expect(tree.readContent(path)).toBe(customized);
166+
});
150167
});

0 commit comments

Comments
 (0)