(mainPath: string)
| 46 | } |
| 47 | |
| 48 | function updateAppModule(mainPath: string): Rule { |
| 49 | return (host: Tree, context: SchematicContext) => { |
| 50 | context.logger.debug('Updating appmodule'); |
| 51 | |
| 52 | const modulePath = getAppModulePath(host, mainPath); |
| 53 | context.logger.debug(`module path: ${modulePath}`); |
| 54 | |
| 55 | addImport(host, modulePath, 'ServiceWorkerModule', '@angular/service-worker'); |
| 56 | addImport(host, modulePath, 'isDevMode', '@angular/core'); |
| 57 | |
| 58 | // register SW in application module |
| 59 | const importText = ` |
| 60 | ServiceWorkerModule.register('ngsw-worker.js', { |
| 61 | enabled: !isDevMode(), |
| 62 | // Register the ServiceWorker as soon as the application is stable |
| 63 | // or after 30 seconds (whichever comes first). |
| 64 | registrationStrategy: 'registerWhenStable:30000' |
| 65 | }) |
| 66 | `; |
| 67 | const moduleSource = getTsSourceFile(host, modulePath); |
| 68 | const metadataChanges = addSymbolToNgModuleMetadata( |
| 69 | moduleSource, |
| 70 | modulePath, |
| 71 | 'imports', |
| 72 | importText, |
| 73 | ); |
| 74 | if (metadataChanges) { |
| 75 | const recorder = host.beginUpdate(modulePath); |
| 76 | applyToUpdateRecorder(recorder, metadataChanges); |
| 77 | host.commitUpdate(recorder); |
| 78 | } |
| 79 | |
| 80 | return host; |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | function addProvideServiceWorker(projectName: string, mainPath: string): Rule { |
| 85 | return (host: Tree) => { |
no test coverage detected