Skip to content

Commit 407d1d5

Browse files
Alanmgechev
Alan
authored andcommitted
fix(@schematics/angular): generate tsconfig.worker.json outside of the src folder
This is to align with the folder structure of version 8, were tsconfigs are outside of the `src` folder Also, this change remove the dud `tsconfig.json` in the `src` folder and instead we add the triple slash lib reference `/// <reference lib="webworker" />` for IDE support.
1 parent 2ee76e7 commit 407d1d5

File tree

7 files changed

+47
-78
lines changed

7 files changed

+47
-78
lines changed

packages/angular_devkit/build_angular/test/browser/web-worker_spec_large.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ describe('Browser Builder Web Worker support', () => {
2626
const workerFiles: { [k: string]: string } = {
2727
'src/app/dep.ts': `export const foo = 'bar';`,
2828
'src/app/app.worker.ts': `
29+
/// <reference lib="webworker" />
30+
2931
import { foo } from './dep';
3032
console.log('hello from worker');
3133
addEventListener('message', ({ data }) => {

packages/schematics/angular/web-worker/files/project-tsconfig/tsconfig.json.template

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.json",
2+
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.json",
33
"compilerOptions": {
44
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/worker",
55
"lib": [
@@ -9,6 +9,6 @@
99
"types": []
1010
},
1111
"include": [
12-
"**/*.worker.ts"
12+
"src/**/*.worker.ts"
1313
]
1414
}

packages/schematics/angular/web-worker/files/worker/__name@dasherize__.worker.ts.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference lib="webworker" />
2+
13
addEventListener('message', ({ data }) => {
24
const response = `worker response to ${data}`;
35
postMessage(response);

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

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { JsonParseMode, parseJsonAst, strings, tags } from '@angular-devkit/core';
8+
import { JsonParseMode, join, normalize, parseJsonAst, strings, tags } from '@angular-devkit/core';
99
import {
1010
Rule, SchematicContext, SchematicsException, Tree,
1111
apply, applyTemplates, chain, mergeWith, move, noop, url,
@@ -21,54 +21,16 @@ function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string
2121
return (host: Tree, context: SchematicContext) => {
2222
context.logger.debug('updating project configuration.');
2323

24-
const tsConfigRules = [];
25-
26-
// Add tsconfig.worker.json.
27-
const relativePathToWorkspaceRoot = root.split('/').map(x => '..').join('/');
28-
tsConfigRules.push(mergeWith(apply(url('./files/worker-tsconfig'), [
29-
applyTemplates({ ...options, relativePathToWorkspaceRoot }),
30-
move(root),
31-
])));
32-
33-
// Add project tsconfig.json.
34-
// The project level tsconfig.json with webworker lib is for editor support since
35-
// the dom and webworker libs are mutually exclusive.
36-
// Note: this schematic does not change other tsconfigs to use the project-level tsconfig.
37-
const projectTsConfigPath = `${root}/tsconfig.json`;
38-
if (host.exists(projectTsConfigPath)) {
39-
// If the file already exists, alter it.
40-
const buffer = host.read(projectTsConfigPath);
41-
if (buffer) {
42-
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
43-
if (tsCfgAst.kind != 'object') {
44-
throw new SchematicsException('Invalid tsconfig. Was expecting an object');
45-
}
46-
const optsAstNode = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
47-
if (optsAstNode && optsAstNode.kind != 'object') {
48-
throw new SchematicsException(
49-
'Invalid tsconfig "compilerOptions" property; Was expecting an object.');
50-
}
51-
const libAstNode = findPropertyInAstObject(tsCfgAst, 'lib');
52-
if (libAstNode && libAstNode.kind != 'array') {
53-
throw new SchematicsException('Invalid tsconfig "lib" property; expected an array.');
54-
}
55-
const newLibProp = 'webworker';
56-
if (libAstNode && !libAstNode.value.includes(newLibProp)) {
57-
const recorder = host.beginUpdate(projectTsConfigPath);
58-
appendValueInAstArray(recorder, libAstNode, newLibProp);
59-
host.commitUpdate(recorder);
60-
}
61-
}
62-
} else {
63-
// Otherwise create it.
64-
tsConfigRules.push(mergeWith(apply(url('./files/project-tsconfig'), [
65-
applyTemplates({ ...options, relativePathToWorkspaceRoot }),
66-
move(root),
67-
])));
68-
}
24+
// todo: replace with the new helper method in a seperate PR
25+
// https://github.com/angular/angular-cli/pull/14207
26+
const rootNormalized = root.endsWith('/') ? root.slice(0, -1) : root;
27+
const relativePathToWorkspaceRoot =
28+
rootNormalized
29+
? rootNormalized.split('/').map(x => '..').join('/')
30+
: '.';
6931

7032
// Add worker glob exclusion to tsconfig.app.json.
71-
const workerGlob = '**/*.worker.ts';
33+
const workerGlob = 'src/**/*.worker.ts';
7234
const buffer = host.read(tsConfigPath);
7335
if (buffer) {
7436
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
@@ -80,17 +42,19 @@ function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string
8042
throw new SchematicsException('Invalid tsconfig "exclude" property; expected an array.');
8143
}
8244

83-
if (filesAstNode && filesAstNode.value.indexOf(workerGlob) == -1) {
45+
if (filesAstNode && !filesAstNode.value.includes(workerGlob)) {
8446
const recorder = host.beginUpdate(tsConfigPath);
8547
appendValueInAstArray(recorder, filesAstNode, workerGlob);
8648
host.commitUpdate(recorder);
8749
}
8850
}
8951

90-
return chain([
91-
// Add tsconfigs.
92-
...tsConfigRules,
93-
]);
52+
return mergeWith(
53+
apply(url('./files/worker-tsconfig'), [
54+
applyTemplates({ ...options, relativePathToWorkspaceRoot }),
55+
move(root),
56+
]),
57+
);
9458
};
9559
}
9660

@@ -145,7 +109,7 @@ export default function (options: WebWorkerOptions): Rule {
145109
throw new SchematicsException('Option "project" is required.');
146110
}
147111
if (!options.target) {
148-
throw new SchematicsException('Option (target) is required.');
112+
throw new SchematicsException('Option "target" is required.');
149113
}
150114

151115
const project = workspace.projects.get(options.project);
@@ -169,11 +133,11 @@ export default function (options: WebWorkerOptions): Rule {
169133
const parsedPath = parseName(options.path, options.name);
170134
options.name = parsedPath.name;
171135
options.path = parsedPath.path;
172-
const root = project.root || project.sourceRoot || '';
136+
const root = project.root || '';
173137

174138
const needWebWorkerConfig = !projectTargetOptions.webWorkerTsConfig;
175139
if (needWebWorkerConfig) {
176-
const workerConfigPath = `${root.endsWith('/') ? root : root + '/'}tsconfig.worker.json`;
140+
const workerConfigPath = join(normalize(root), 'tsconfig.worker.json');
177141
projectTargetOptions.webWorkerTsConfig = workerConfigPath;
178142

179143
// add worker tsconfig to lint architect target

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('Service Worker Schematic', () => {
2020
project: 'bar',
2121
target: 'build',
2222
name: 'app',
23-
// path: 'src/app',
2423
snippet: true,
2524
};
2625

@@ -54,18 +53,14 @@ describe('Service Worker Schematic', () => {
5453
expect(tree.exists(path)).toEqual(true);
5554
});
5655

57-
it('should put a new tsconfig.json file in the project root', async () => {
58-
const tree = await schematicRunner.runSchematicAsync('web-worker', defaultOptions, appTree)
59-
.toPromise();
60-
const path = '/projects/bar/tsconfig.json';
61-
expect(tree.exists(path)).toEqual(true);
62-
});
63-
6456
it('should put the tsconfig.worker.json file in the project root', async () => {
6557
const tree = await schematicRunner.runSchematicAsync('web-worker', defaultOptions, appTree)
6658
.toPromise();
6759
const path = '/projects/bar/tsconfig.worker.json';
6860
expect(tree.exists(path)).toEqual(true);
61+
62+
const { compilerOptions } = JSON.parse(tree.readContent(path));
63+
expect(compilerOptions.outDir).toBe('../../out-tsc/worker');
6964
});
7065

7166
it('should add the webWorkerTsConfig option to workspace', async () => {
@@ -80,7 +75,7 @@ describe('Service Worker Schematic', () => {
8075
const tree = await schematicRunner.runSchematicAsync('web-worker', defaultOptions, appTree)
8176
.toPromise();
8277
const { exclude } = JSON.parse(tree.readContent('/projects/bar/tsconfig.app.json'));
83-
expect(exclude).toContain('**/*.worker.ts');
78+
expect(exclude).toContain('src/**/*.worker.ts');
8479
});
8580

8681
it('should add snippet to sibling file', async () => {
@@ -103,4 +98,20 @@ describe('Service Worker Schematic', () => {
10398
'projects/bar/tsconfig.worker.json',
10499
]);
105100
});
101+
102+
it(`should add 'tsconfig.worker.json' outside of 'src' directory in root app`, async () => {
103+
const rootAppOptions = { ...appOptions, projectRoot: '', name: 'foo' };
104+
const workerOptions = { ...defaultOptions, project: 'foo' };
105+
106+
appTree = await schematicRunner.runSchematicAsync('application', rootAppOptions, appTree)
107+
.toPromise();
108+
const tree = await schematicRunner.runSchematicAsync('web-worker', workerOptions, appTree)
109+
.toPromise();
110+
111+
const path = '/tsconfig.worker.json';
112+
expect(tree.exists(path)).toEqual(true);
113+
114+
const { compilerOptions } = JSON.parse(tree.readContent(path));
115+
expect(compilerOptions.outDir).toBe('./out-tsc/worker');
116+
});
106117
});

tests/legacy-cli/e2e/tests/build/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default async function () {
1414

1515
const workerPath = join('src', 'app', 'app.worker.ts');
1616
const snippetPath = join('src', 'app', 'app.component.ts');
17-
const projectTsConfig = join('src', 'tsconfig.json');
18-
const workerTsConfig = join('src', 'tsconfig.worker.json');
17+
const projectTsConfig = 'tsconfig.json';
18+
const workerTsConfig = 'tsconfig.worker.json';
1919

2020
await ng('generate', 'web-worker', 'app');
2121
await expectFileToExist(workerPath);

0 commit comments

Comments
 (0)