-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathngsw-config.ts
More file actions
75 lines (62 loc) · 2.52 KB
/
ngsw-config.ts
File metadata and controls
75 lines (62 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Rule } from '@angular-devkit/schematics';
import { appendValueInAstArray, findPropertyInAstObject } from '../../utility/json-utils';
import { Builders } from '../../utility/workspace-models';
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
/**
* Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277
*/
export function updateNGSWConfig(): Rule {
return (tree, context) => {
const workspace = getWorkspace(tree);
const logger = context.logger;
for (const { target } of getTargets(workspace, 'build', Builders.Browser)) {
for (const options of getAllOptions(target)) {
const ngswConfigPath = findPropertyInAstObject(options, 'ngswConfigPath');
if (!ngswConfigPath || ngswConfigPath.kind !== 'string') {
logger.warn(`Cannot find file: ${ngswConfigPath}`);
continue;
}
const path = ngswConfigPath.value;
const ngswConfigAst = readJsonFileAsAstObject(tree, path);
if (!ngswConfigAst || ngswConfigAst.kind !== 'object') {
continue;
}
const assetGroups = findPropertyInAstObject(ngswConfigAst, 'assetGroups');
if (!assetGroups || assetGroups.kind !== 'array') {
continue;
}
const prefetchElement = assetGroups.elements.find(element => {
const installMode = element.kind === 'object' && findPropertyInAstObject(element, 'installMode');
return installMode && installMode.value === 'prefetch';
});
if (!prefetchElement || prefetchElement.kind !== 'object') {
continue;
}
const resources = findPropertyInAstObject(prefetchElement, 'resources');
if (!resources || resources.kind !== 'object') {
continue;
}
const files = findPropertyInAstObject(resources, 'files');
if (!files || files.kind !== 'array') {
continue;
}
const hasManifest = files.elements
.some(({ value }) => typeof value === 'string' && value.endsWith('manifest.webmanifest'));
if (hasManifest) {
continue;
}
const recorder = tree.beginUpdate(path);
appendValueInAstArray(recorder, files, '/manifest.webmanifest', 10);
tree.commitUpdate(recorder);
}
}
return tree;
};
}