Skip to content

Commit 82c438b

Browse files
committed
refactor(@schematics/angular): clean up web-worker schematic
Remove code that used to handle version 8 applications. These should have migrated to the new structure when using ng-update.
1 parent a537c75 commit 82c438b

File tree

3 files changed

+32
-100
lines changed

3 files changed

+32
-100
lines changed

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

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { dirname, join, normalize, strings, tags } from '@angular-devkit/core';
9+
import { join, normalize, strings, tags } from '@angular-devkit/core';
1010
import {
1111
Rule,
1212
SchematicContext,
@@ -20,42 +20,11 @@ import {
2020
noop,
2121
url,
2222
} from '@angular-devkit/schematics';
23-
import { JSONFile } from '../utility/json-file';
2423
import { parseName } from '../utility/parse-name';
2524
import { relativePathToWorkspaceRoot } from '../utility/paths';
2625
import { buildDefaultPath, getWorkspace, updateWorkspace } from '../utility/workspace';
27-
import { BrowserBuilderOptions } from '../utility/workspace-models';
2826
import { Schema as WebWorkerOptions } from './schema';
2927

30-
function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string): Rule {
31-
return (host: Tree, context: SchematicContext) => {
32-
context.logger.debug('updating project configuration.');
33-
34-
// Add worker glob exclusion to tsconfig.app.json.
35-
// Projects pre version 8 should to have tsconfig.app.json inside their application
36-
const isInSrc = dirname(normalize(tsConfigPath)).endsWith('src');
37-
const workerGlob = `${isInSrc ? '' : 'src/'}**/*.worker.ts`;
38-
39-
try {
40-
const json = new JSONFile(host, tsConfigPath);
41-
const exclude = json.get(['exclude']);
42-
if (exclude && Array.isArray(exclude) && !exclude.includes(workerGlob)) {
43-
json.modify(['exclude'], [...exclude, workerGlob]);
44-
}
45-
} catch {}
46-
47-
return mergeWith(
48-
apply(url('./files/worker-tsconfig'), [
49-
applyTemplates({
50-
...options,
51-
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(root),
52-
}),
53-
move(root),
54-
]),
55-
);
56-
};
57-
}
58-
5928
function addSnippet(options: WebWorkerOptions): Rule {
6029
return (host: Tree, context: SchematicContext) => {
6130
context.logger.debug('Updating appmodule');
@@ -109,63 +78,60 @@ export default function (options: WebWorkerOptions): Rule {
10978
if (!options.project) {
11079
throw new SchematicsException('Option "project" is required.');
11180
}
112-
if (!options.target) {
113-
throw new SchematicsException('Option "target" is required.');
114-
}
81+
11582
const project = workspace.projects.get(options.project);
11683
if (!project) {
11784
throw new SchematicsException(`Invalid project name (${options.project})`);
11885
}
86+
11987
const projectType = project.extensions['projectType'];
12088
if (projectType !== 'application') {
12189
throw new SchematicsException(`Web Worker requires a project type of "application".`);
12290
}
12391

124-
const projectTarget = project.targets.get(options.target);
125-
if (!projectTarget) {
126-
throw new Error(`Target is not defined for this project.`);
127-
}
128-
const projectTargetOptions = (projectTarget.options || {}) as unknown as BrowserBuilderOptions;
129-
13092
if (options.path === undefined) {
13193
options.path = buildDefaultPath(project);
13294
}
13395
const parsedPath = parseName(options.path, options.name);
13496
options.name = parsedPath.name;
13597
options.path = parsedPath.path;
136-
const root = project.root || '';
13798

138-
const needWebWorkerConfig = !projectTargetOptions.webWorkerTsConfig;
139-
if (needWebWorkerConfig) {
140-
const workerConfigPath = join(normalize(root), 'tsconfig.worker.json');
141-
projectTargetOptions.webWorkerTsConfig = workerConfigPath;
142-
}
143-
144-
const projectTestTarget = project.targets.get('test');
145-
if (projectTestTarget) {
146-
const projectTestTargetOptions = (projectTestTarget.options ||
147-
{}) as unknown as BrowserBuilderOptions;
148-
149-
const needWebWorkerConfig = !projectTestTargetOptions.webWorkerTsConfig;
150-
if (needWebWorkerConfig) {
151-
const workerConfigPath = join(normalize(root), 'tsconfig.worker.json');
152-
projectTestTargetOptions.webWorkerTsConfig = workerConfigPath;
153-
}
154-
}
155-
156-
const templateSource = apply(url('./files/worker'), [
99+
const templateSourceWorkerCode = apply(url('./files/worker'), [
157100
applyTemplates({ ...options, ...strings }),
158101
move(parsedPath.path),
159102
]);
160103

104+
const root = project.root || '';
105+
const templateSourceWorkerConfig = apply(url('./files/worker-tsconfig'), [
106+
applyTemplates({
107+
...options,
108+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(root),
109+
}),
110+
move(root),
111+
]);
112+
161113
return chain([
162114
// Add project configuration.
163-
needWebWorkerConfig ? addConfig(options, root, projectTargetOptions.tsConfig) : noop(),
164-
needWebWorkerConfig ? updateWorkspace(workspace) : noop(),
115+
updateWorkspace((workspace) => {
116+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
117+
const project = workspace.projects.get(options.project)!;
118+
const buildTarget = project.targets.get('build');
119+
const testTarget = project.targets.get('test');
120+
if (!buildTarget) {
121+
throw new Error(`Build target is not defined for this project.`);
122+
}
123+
124+
const workerConfigPath = join(normalize(root), 'tsconfig.worker.json');
125+
(buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
126+
if (testTarget) {
127+
(testTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
128+
}
129+
}),
165130
// Create the worker in a sibling module.
166131
options.snippet ? addSnippet(options) : noop(),
167132
// Add the worker.
168-
mergeWith(templateSource),
133+
mergeWith(templateSourceWorkerCode),
134+
mergeWith(templateSourceWorkerConfig),
169135
]);
170136
};
171137
}

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,6 @@ describe('Web Worker Schematic', () => {
7777
);
7878
});
7979

80-
it('should add exclusions to tsconfig.app.json', async () => {
81-
const oldTsConfig = {
82-
extends: '../../tsconfig.json',
83-
include: ['src/**/*.ts'],
84-
exclude: ['src/test.ts', 'src/**/*.spec.ts'],
85-
};
86-
appTree.overwrite('projects/bar/tsconfig.app.json', JSON.stringify(oldTsConfig, undefined, 2));
87-
88-
const tree = await schematicRunner
89-
.runSchematicAsync('web-worker', defaultOptions, appTree)
90-
.toPromise();
91-
const { exclude } = JSON.parse(tree.readContent('/projects/bar/tsconfig.app.json'));
92-
expect(exclude).toContain('src/**/*.worker.ts');
93-
});
94-
9580
it('should add snippet to sibling file', async () => {
9681
const tree = await schematicRunner
9782
.runSchematicAsync('web-worker', defaultOptions, appTree)
@@ -118,24 +103,4 @@ describe('Web Worker Schematic', () => {
118103
const { compilerOptions } = parseJson(tree.readContent(path).toString());
119104
expect(compilerOptions.outDir).toBe('./out-tsc/worker');
120105
});
121-
122-
it('supports pre version 8 structure', async () => {
123-
const workspace = JSON.parse(appTree.readContent('/angular.json'));
124-
const tsConfigPath = '/projects/bar/src/tsconfig.app.json';
125-
workspace.projects.bar.architect.build.options.tsConfig = tsConfigPath;
126-
appTree.overwrite('/angular.json', JSON.stringify(workspace));
127-
128-
const oldTsConfig = {
129-
extends: '../../../tsconfig.json',
130-
include: ['**/*.ts'],
131-
exclude: ['test.ts', '**/*.spec.ts'],
132-
};
133-
appTree.create('projects/bar/src/tsconfig.app.json', JSON.stringify(oldTsConfig, undefined, 2));
134-
135-
const tree = await schematicRunner
136-
.runSchematicAsync('web-worker', defaultOptions, appTree)
137-
.toPromise();
138-
const { exclude } = JSON.parse(tree.readContent(tsConfigPath));
139-
expect(exclude).toContain('**/*.worker.ts');
140-
});
141106
});

packages/schematics/angular/web-worker/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"target": {
2323
"type": "string",
2424
"description": "The target to apply web worker to.",
25-
"default": "build"
25+
"default": "build",
26+
"x-deprecated": "No longer has an effect."
2627
},
2728
"name": {
2829
"type": "string",

0 commit comments

Comments
 (0)