Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit ead024f

Browse files
committed
feat(@schematics/angular): service worker project generation
This adds a feature to the Angular application schematic to generate a project which includes @angular/service-worker's ServiceWorkerModule, conditionally enabled for production builds. An ngsw.json file with configuration suited for most projects is also generated. Both operations are gated on the --service-worker flag which is added.
1 parent 64529cc commit ead024f

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"index": "/index.html",
3+
"assetGroups": [{
4+
"name": "app",
5+
"installMode": "prefetch",
6+
"resources": {
7+
"files": [
8+
"/favicon.ico",
9+
"/index.html"
10+
],
11+
"versionedFiles": [
12+
"/*.bundle.css",
13+
"/*.bundle.js",
14+
"/*.chunk.js"
15+
]
16+
}
17+
}, {
18+
"name": "assets",
19+
"installMode": "lazy",
20+
"updateMode": "prefetch",
21+
"resources": {
22+
"files": [
23+
"/assets/**"
24+
]
25+
}
26+
}]
27+
}

packages/schematics/angular/application/files/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"@angular/http": "^5.0.0-rc.7",
2121
"@angular/platform-browser": "^5.0.0-rc.7",
2222
"@angular/platform-browser-dynamic": "^5.0.0-rc.7",
23-
"@angular/router": "^5.0.0-rc.7",
23+
"@angular/router": "^5.0.0-rc.7",<% if (serviceWorker) { %>
24+
"@angular/service-worker": "^5.0.0-rc.7",<% } %>
2425
"core-js": "^2.4.1",
2526
"rxjs": "^5.4.2",
2627
"zone.js": "^0.8.14"

packages/schematics/angular/application/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default function (options: ApplicationOptions): Rule {
9595
apply(url('./files'), [
9696
options.minimal ? filter(minimalPathFilter) : noop(),
9797
options.skipGit ? filter(path => !path.endsWith('/__dot__gitignore')) : noop(),
98+
options.serviceWorker ? noop() : filter(path => !path.endsWith('/ngsw-config.json')),
9899
template({
99100
utils: stringUtils,
100101
'dot': '.',

packages/schematics/angular/application/other-files/app.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33
<% if (routing) { %>
44
import { AppRoutingModule } from './app-routing.module';<% } %>
5+
<% if (serviceWorker) { %>
6+
import { ServiceWorkerModule } from '@angular/service-worker';<% } %>
57
import { AppComponent } from './app.component';
8+
<% if (serviceWorker) { %>
9+
import { environment } from '../environments/environment';<% } %>
610

711
@NgModule({
812
declarations: [
913
AppComponent
1014
],
1115
imports: [
1216
BrowserModule<% if (routing) { %>,
13-
AppRoutingModule<% } %>
17+
AppRoutingModule<% } %><% if (serviceWorker) { %>,
18+
environment.production ? ServiceWorkerModule.register('/ngsw-worker.js') : []<% } %>
1419
],
1520
providers: [],
1621
bootstrap: [AppComponent]

packages/schematics/angular/application/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ export interface Schema {
5252
* Should create a minimal app.
5353
*/
5454
minimal?: boolean;
55+
/**
56+
* Should install the @angular/service-worker.
57+
*/
58+
serviceWorker?: boolean;
5559
}

packages/schematics/angular/application/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
"description": "Should create a minimal app.",
8080
"type": "boolean",
8181
"default": false
82+
},
83+
"serviceWorker": {
84+
"description": "Should install the @angular/service-worker.",
85+
"type": "boolean",
86+
"default": false
8287
}
8388
},
8489
"required": [

0 commit comments

Comments
 (0)
close