@@ -20,12 +20,13 @@ import {
2020 url ,
2121} from '@angular-devkit/schematics' ;
2222import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
23+ import { addFunctionalProvidersToStandaloneBootstrap } from '../private/standalone' ;
2324import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript' ;
2425import { readWorkspace , writeWorkspace } from '../utility' ;
2526import { addSymbolToNgModuleMetadata , insertImport } from '../utility/ast-utils' ;
2627import { applyToUpdateRecorder } from '../utility/change' ;
2728import { addPackageJsonDependency , getPackageJsonDependency } from '../utility/dependencies' ;
28- import { getAppModulePath } from '../utility/ng-ast-utils' ;
29+ import { getAppModulePath , isStandaloneApp } from '../utility/ng-ast-utils' ;
2930import { relativePathToWorkspaceRoot } from '../utility/paths' ;
3031import { targetBuildNotFoundError } from '../utility/project-targets' ;
3132import { BrowserBuilderOptions } from '../utility/workspace-models' ;
@@ -85,6 +86,44 @@ function updateAppModule(mainPath: string): Rule {
8586 } ;
8687}
8788
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+
88127function getTsSourceFile ( host : Tree , path : string ) : ts . SourceFile {
89128 const content = host . readText ( path ) ;
90129 const source = ts . createSourceFile ( path , content , ts . ScriptTarget . Latest , true ) ;
@@ -116,23 +155,25 @@ export default function (options: ServiceWorkerOptions): Rule {
116155 resourcesOutputPath = normalize ( `/${ resourcesOutputPath } ` ) ;
117156 }
118157
119- const templateSource = apply ( url ( './files' ) , [
120- applyTemplates ( {
121- ...options ,
122- resourcesOutputPath,
123- relativePathToWorkspaceRoot : relativePathToWorkspaceRoot ( project . root ) ,
124- } ) ,
125- move ( project . root ) ,
126- ] ) ;
127-
128158 context . addTask ( new NodePackageInstallTask ( ) ) ;
129159
130160 await writeWorkspace ( host , workspace ) ;
131161
162+ const { main } = buildOptions ;
163+
132164 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+ ) ,
134175 addDependencies ( ) ,
135- updateAppModule ( buildOptions . main ) ,
176+ isStandaloneApp ( host , main ) ? addProvideServiceWorker ( main ) : updateAppModule ( main ) ,
136177 ] ) ;
137178 } ;
138179}
0 commit comments