@@ -20,12 +20,13 @@ import {
20
20
url ,
21
21
} from '@angular-devkit/schematics' ;
22
22
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
23
+ import { addFunctionalProvidersToStandaloneBootstrap } from '../private/standalone' ;
23
24
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript' ;
24
25
import { readWorkspace , writeWorkspace } from '../utility' ;
25
26
import { addSymbolToNgModuleMetadata , insertImport } from '../utility/ast-utils' ;
26
27
import { applyToUpdateRecorder } from '../utility/change' ;
27
28
import { addPackageJsonDependency , getPackageJsonDependency } from '../utility/dependencies' ;
28
- import { getAppModulePath } from '../utility/ng-ast-utils' ;
29
+ import { getAppModulePath , isStandaloneApp } from '../utility/ng-ast-utils' ;
29
30
import { relativePathToWorkspaceRoot } from '../utility/paths' ;
30
31
import { targetBuildNotFoundError } from '../utility/project-targets' ;
31
32
import { BrowserBuilderOptions } from '../utility/workspace-models' ;
@@ -85,6 +86,44 @@ function updateAppModule(mainPath: string): Rule {
85
86
} ;
86
87
}
87
88
89
+ function addProvideServiceWorker ( mainPath : string ) : Rule {
90
+ return ( host : Tree ) => {
91
+ const updatedFilePath = addFunctionalProvidersToStandaloneBootstrap (
92
+ host ,
93
+ mainPath ,
94
+ 'provideServiceWorker' ,
95
+ '@angular/service-worker' ,
96
+ [
97
+ ts . factory . createStringLiteral ( 'ngsw-worker.js' , true ) ,
98
+ ts . factory . createObjectLiteralExpression (
99
+ [
100
+ ts . factory . createPropertyAssignment (
101
+ ts . factory . createIdentifier ( 'enabled' ) ,
102
+ ts . factory . createPrefixUnaryExpression (
103
+ ts . SyntaxKind . ExclamationToken ,
104
+ ts . factory . createCallExpression (
105
+ ts . factory . createIdentifier ( 'isDevMode' ) ,
106
+ undefined ,
107
+ [ ] ,
108
+ ) ,
109
+ ) ,
110
+ ) ,
111
+ ts . factory . createPropertyAssignment (
112
+ ts . factory . createIdentifier ( 'registrationStrategy' ) ,
113
+ ts . factory . createStringLiteral ( 'registerWhenStable:30000' , true ) ,
114
+ ) ,
115
+ ] ,
116
+ true ,
117
+ ) ,
118
+ ] ,
119
+ ) ;
120
+
121
+ addImport ( host , updatedFilePath , 'isDevMode' , '@angular/core' ) ;
122
+
123
+ return host ;
124
+ } ;
125
+ }
126
+
88
127
function getTsSourceFile ( host : Tree , path : string ) : ts . SourceFile {
89
128
const content = host . readText ( path ) ;
90
129
const source = ts . createSourceFile ( path , content , ts . ScriptTarget . Latest , true ) ;
@@ -116,23 +155,25 @@ export default function (options: ServiceWorkerOptions): Rule {
116
155
resourcesOutputPath = normalize ( `/${ resourcesOutputPath } ` ) ;
117
156
}
118
157
119
- const templateSource = apply ( url ( './files' ) , [
120
- applyTemplates ( {
121
- ...options ,
122
- resourcesOutputPath,
123
- relativePathToWorkspaceRoot : relativePathToWorkspaceRoot ( project . root ) ,
124
- } ) ,
125
- move ( project . root ) ,
126
- ] ) ;
127
-
128
158
context . addTask ( new NodePackageInstallTask ( ) ) ;
129
159
130
160
await writeWorkspace ( host , workspace ) ;
131
161
162
+ const { main } = buildOptions ;
163
+
132
164
return chain ( [
133
- mergeWith ( templateSource ) ,
165
+ mergeWith (
166
+ apply ( url ( './files' ) , [
167
+ applyTemplates ( {
168
+ ...options ,
169
+ resourcesOutputPath,
170
+ relativePathToWorkspaceRoot : relativePathToWorkspaceRoot ( project . root ) ,
171
+ } ) ,
172
+ move ( project . root ) ,
173
+ ] ) ,
174
+ ) ,
134
175
addDependencies ( ) ,
135
- updateAppModule ( buildOptions . main ) ,
176
+ isStandaloneApp ( host , main ) ? addProvideServiceWorker ( main ) : updateAppModule ( main ) ,
136
177
] ) ;
137
178
} ;
138
179
}
0 commit comments