diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts
index 96fa2c08e58c..fc5b40203427 100644
--- a/packages/angular/pwa/pwa/index.ts
+++ b/packages/angular/pwa/pwa/index.ts
@@ -12,7 +12,6 @@ import {
SchematicsException,
Tree,
apply,
- branchAndMerge,
chain,
externalSchematic,
mergeWith,
@@ -39,15 +38,14 @@ function addServiceWorker(options: PwaOptions): Rule {
function getIndent(text: string): string {
let indent = '';
- let hitNonSpace = false;
- text.split('')
- .forEach(char => {
- if (char === ' ' && !hitNonSpace) {
- indent += ' ';
- } else {
- hitNonSpace = true;
- }
- }, 0);
+
+ for (const char of text) {
+ if (char === ' ' || char === '\t') {
+ indent += char;
+ } else {
+ break;
+ }
+ }
return indent;
}
@@ -70,43 +68,32 @@ function updateIndexFile(options: PwaOptions): Rule {
const content = buffer.toString();
const lines = content.split('\n');
let closingHeadTagLineIndex = -1;
- let closingHeadTagLine = '';
let closingBodyTagLineIndex = -1;
- let closingBodyTagLine = '';
- lines.forEach((line: string, index: number) => {
- if (/<\/head>/.test(line) && closingHeadTagLineIndex === -1) {
- closingHeadTagLine = line;
+ lines.forEach((line, index) => {
+ if (closingHeadTagLineIndex === -1 && /<\/head>/.test(line)) {
closingHeadTagLineIndex = index;
- }
-
- if (/<\/body>/.test(line) && closingBodyTagLineIndex === -1) {
- closingBodyTagLine = line;
+ } else if (closingBodyTagLineIndex === -1 && /<\/body>/.test(line)) {
closingBodyTagLineIndex = index;
}
});
- const headTagIndent = getIndent(closingHeadTagLine) + ' ';
+ const headIndent = getIndent(lines[closingHeadTagLineIndex]) + ' ';
const itemsToAddToHead = [
'',
'',
];
- const textToInsertIntoHead = itemsToAddToHead
- .map(text => headTagIndent + text)
- .join('\n');
-
- const bodyTagIndent = getIndent(closingBodyTagLine) + ' ';
- const itemsToAddToBody
- = '';
-
- const textToInsertIntoBody = bodyTagIndent + itemsToAddToBody;
+ const bodyIndent = getIndent(lines[closingBodyTagLineIndex]) + ' ';
+ const itemsToAddToBody = [
+ '',
+ ];
const updatedIndex = [
...lines.slice(0, closingHeadTagLineIndex),
- textToInsertIntoHead,
+ ...itemsToAddToHead.map(line => headIndent + line),
...lines.slice(closingHeadTagLineIndex, closingBodyTagLineIndex),
- textToInsertIntoBody,
- ...lines.slice(closingBodyTagLineIndex),
+ ...itemsToAddToBody.map(line => bodyIndent + line),
+ ...lines.slice(closingHeadTagLineIndex),
].join('\n');
host.overwrite(path, updatedIndex);
@@ -137,12 +124,9 @@ function addManifestToAssetsConfig(options: PwaOptions) {
['build', 'test'].forEach((target) => {
const applyTo = architect[target].options;
+ const assets = applyTo.assets || (applyTo.assets = []);
- if (!applyTo.assets) {
- applyTo.assets = [assetEntry];
- } else {
- applyTo.assets.push(assetEntry);
- }
+ assets.push(assetEntry);
});
@@ -163,27 +147,24 @@ export default function (options: PwaOptions): Rule {
throw new SchematicsException(`PWA requires a project type of "application".`);
}
- const assetPath = join(project.root as Path, 'src', 'assets');
const sourcePath = join(project.root as Path, 'src');
+ const assetsPath = join(sourcePath, 'assets');
options.title = options.title || options.project;
- const templateSource = apply(url('./files/assets'), [
- template({
- ...options,
- }),
- move(assetPath),
+ const rootTemplateSource = apply(url('./files/root'), [
+ template({ ...options }),
+ move(sourcePath),
+ ]);
+ const assetsTemplateSource = apply(url('./files/assets'), [
+ template({ ...options }),
+ move(assetsPath),
]);
return chain([
addServiceWorker(options),
- branchAndMerge(chain([
- mergeWith(templateSource),
- ])),
- mergeWith(apply(url('./files/root'), [
- template({...options}),
- move(sourcePath),
- ])),
+ mergeWith(rootTemplateSource),
+ mergeWith(assetsTemplateSource),
updateIndexFile(options),
addManifestToAssetsConfig(options),
]);
diff --git a/packages/schematics/angular/service-worker/files/ngsw-config.json b/packages/schematics/angular/service-worker/files/ngsw-config.json
index 7c6b82cdb4e8..fa9410f347f6 100644
--- a/packages/schematics/angular/service-worker/files/ngsw-config.json
+++ b/packages/schematics/angular/service-worker/files/ngsw-config.json
@@ -1,24 +1,26 @@
{
"index": "/index.html",
- "assetGroups": [{
- "name": "app",
- "installMode": "prefetch",
- "resources": {
- "files": [
- "/favicon.ico",
- "/index.html",
- "/*.css",
- "/*.js"
- ]
+ "assetGroups": [
+ {
+ "name": "app",
+ "installMode": "prefetch",
+ "resources": {
+ "files": [
+ "/favicon.ico",
+ "/index.html",
+ "/*.css",
+ "/*.js"
+ ]
+ }
+ }, {
+ "name": "assets",
+ "installMode": "lazy",
+ "updateMode": "prefetch",
+ "resources": {
+ "files": [
+ "/assets/**"
+ ]
+ }
}
- }, {
- "name": "assets",
- "installMode": "lazy",
- "updateMode": "prefetch",
- "resources": {
- "files": [
- "/assets/**"
- ]
- }
- }]
-}
\ No newline at end of file
+ ]
+}
diff --git a/packages/schematics/angular/service-worker/index.ts b/packages/schematics/angular/service-worker/index.ts
index 77c6902576f3..795637a7b12d 100644
--- a/packages/schematics/angular/service-worker/index.ts
+++ b/packages/schematics/angular/service-worker/index.ts
@@ -75,11 +75,11 @@ function addDependencies(): Rule {
if (coreDep === null) {
throw new SchematicsException('Could not find version.');
}
- const platformServerDep = {
+ const serviceWorkerDep = {
...coreDep,
name: packageName,
};
- addPackageJsonDependency(host, platformServerDep);
+ addPackageJsonDependency(host, serviceWorkerDep);
return host;
};
@@ -131,7 +131,7 @@ function updateAppModule(options: ServiceWorkerOptions): Rule {
// register SW in app module
const importText =
- `ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production })`;
+ `ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })`;
moduleSource = getTsSourceFile(host, modulePath);
const metadataChanges = addSymbolToNgModuleMetadata(
moduleSource, modulePath, 'imports', importText);
diff --git a/packages/schematics/angular/service-worker/index_spec.ts b/packages/schematics/angular/service-worker/index_spec.ts
index 9a724e4dcf81..fae453d35327 100644
--- a/packages/schematics/angular/service-worker/index_spec.ts
+++ b/packages/schematics/angular/service-worker/index_spec.ts
@@ -87,8 +87,8 @@ describe('Service Worker Schematic', () => {
const tree = schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
// tslint:disable-next-line:max-line-length
- const regex = /ServiceWorkerModule\.register\('\/ngsw-worker.js\', { enabled: environment.production }\)/;
- expect(pkgText).toMatch(regex);
+ const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })';
+ expect(pkgText).toContain(expectedText);
});
it('should put the ngsw-config.json file in the project root', () => {
diff --git a/packages/schematics/angular/utility/dependencies.ts b/packages/schematics/angular/utility/dependencies.ts
index 892dece42926..92d827182b71 100644
--- a/packages/schematics/angular/utility/dependencies.ts
+++ b/packages/schematics/angular/utility/dependencies.ts
@@ -37,7 +37,7 @@ export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency)
// Haven't found the dependencies key, add it to the root of the package.json.
appendPropertyInAstObject(recorder, packageJsonAst, dependency.type, {
[dependency.name]: dependency.version,
- }, 4);
+ }, 2);
} else if (depsNode.kind === 'object') {
// check if package already added
const depNode = findPropertyInAstObject(depsNode, dependency.name);
diff --git a/packages/schematics/angular/utility/json-utils.ts b/packages/schematics/angular/utility/json-utils.ts
index 2894b01c2f07..6a509ca7cd4c 100644
--- a/packages/schematics/angular/utility/json-utils.ts
+++ b/packages/schematics/angular/utility/json-utils.ts
@@ -84,7 +84,7 @@ export function insertPropertyInAstObjectInOrder(
recorder.insertRight(
insertIndex,
- `${indentStr}`
+ indentStr
+ `"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}`
+ ',',
);