@@ -8,9 +8,12 @@ import { PurifyPlugin } from '@angular-devkit/build-optimizer';
88import { StaticAssetPlugin } from '../../plugins/static-asset' ;
99import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin' ;
1010import { WebpackConfigOptions } from '../webpack-config' ;
11+ import { NEW_SW_VERSION } from '../../utilities/service-worker' ;
1112
1213const UglifyJSPlugin = require ( 'uglifyjs-webpack-plugin' ) ;
1314
15+ const OLD_SW_VERSION = '>= 1.0.0-beta.5 < 2.0.0' ;
16+
1417/**
1518 * license-webpack-plugin has a peer dependency on webpack-sources, list it in a comment to
1619 * let the dependency validator know it is used.
@@ -40,63 +43,69 @@ export function getProdConfig(wco: WebpackConfigOptions) {
4043
4144 // Read the version of @angular /service-worker and throw if it doesn't match the
4245 // expected version.
43- const allowedVersion = '>= 1.0.0-beta.5 < 2.0.0' ;
4446 const swPackageJson = fs . readFileSync ( `${ swModule } /package.json` ) . toString ( ) ;
4547 const swVersion = JSON . parse ( swPackageJson ) [ 'version' ] ;
46- if ( ! semver . satisfies ( swVersion , allowedVersion ) ) {
48+
49+ const isLegacySw = semver . satisfies ( swVersion , OLD_SW_VERSION ) ;
50+ const isModernSw = semver . satisfies ( swVersion , NEW_SW_VERSION ) ;
51+
52+ if ( ! isLegacySw && ! isModernSw ) {
4753 throw new Error ( stripIndent `
4854 The installed version of @angular/service-worker is ${ swVersion } . This version of the CLI
49- requires the @angular/service-worker version to satisfy ${ allowedVersion } . Please upgrade
55+ requires the @angular/service-worker version to satisfy ${ OLD_SW_VERSION } . Please upgrade
5056 your service worker version.
5157 ` ) ;
5258 }
5359
54- // Path to the worker script itself.
55- const workerPath = path . resolve ( swModule , 'bundles/worker-basic.min.js' ) ;
56-
57- // Path to a small script to register a service worker.
58- const registerPath = path . resolve ( swModule , 'build/assets/register-basic.min.js' ) ;
59-
60- // Sanity check - both of these files should be present in @angular/service-worker.
61- if ( ! fs . existsSync ( workerPath ) || ! fs . existsSync ( registerPath ) ) {
62- throw new Error ( stripIndent `
63- The installed version of @angular/service-worker isn't supported by the CLI.
64- Please install a supported version. The following files should exist:
65- - ${ registerPath }
66- - ${ workerPath }
67- ` ) ;
60+ if ( isLegacySw ) {
61+ // Path to the worker script itself.
62+ const workerPath = path . resolve ( swModule , 'bundles/worker-basic.min.js' ) ;
63+
64+ // Path to a small script to register a service worker.
65+ const registerPath = path . resolve ( swModule , 'build/assets/register-basic.min.js' ) ;
66+
67+ // Sanity check - both of these files should be present in @angular/service-worker.
68+ if ( ! fs . existsSync ( workerPath ) || ! fs . existsSync ( registerPath ) ) {
69+ throw new Error ( stripIndent `
70+ The installed version of @angular/service-worker isn't supported by the CLI.
71+ Please install a supported version. The following files should exist:
72+ - ${ registerPath }
73+ - ${ workerPath }
74+ ` ) ;
75+ }
76+
77+ // CopyWebpackPlugin replaces GlobCopyWebpackPlugin, but AngularServiceWorkerPlugin depends
78+ // on specific behaviour from latter.
79+ // AngularServiceWorkerPlugin expects the ngsw-manifest.json to be present in the 'emit' phase
80+ // but with CopyWebpackPlugin it's only there on 'after-emit'.
81+ // So for now we keep it here, but if AngularServiceWorkerPlugin changes we remove it.
82+ extraPlugins . push ( new GlobCopyWebpackPlugin ( {
83+ patterns : [
84+ 'ngsw-manifest.json' ,
85+ { glob : 'ngsw-manifest.json' ,
86+ input : path . resolve ( projectRoot , appConfig . root ) , output : '' }
87+ ] ,
88+ globOptions : {
89+ cwd : projectRoot ,
90+ optional : true ,
91+ } ,
92+ } ) ) ;
93+
94+ // Load the Webpack plugin for manifest generation and install it.
95+ const AngularServiceWorkerPlugin = require ( '@angular/service-worker/build/webpack' )
96+ . AngularServiceWorkerPlugin ;
97+ extraPlugins . push ( new AngularServiceWorkerPlugin ( {
98+ baseHref : buildOptions . baseHref || '/' ,
99+ } ) ) ;
100+
101+ // Copy the worker script into assets.
102+ const workerContents = fs . readFileSync ( workerPath ) . toString ( ) ;
103+ extraPlugins . push ( new StaticAssetPlugin ( 'worker-basic.min.js' , workerContents ) ) ;
104+
105+ // Add a script to index.html that registers the service worker.
106+ // TODO(alxhub): inline this script somehow.
107+ entryPoints [ 'sw-register' ] = [ registerPath ] ;
68108 }
69-
70- // CopyWebpackPlugin replaces GlobCopyWebpackPlugin, but AngularServiceWorkerPlugin depends
71- // on specific behaviour from latter.
72- // AngularServiceWorkerPlugin expects the ngsw-manifest.json to be present in the 'emit' phase
73- // but with CopyWebpackPlugin it's only there on 'after-emit'.
74- // So for now we keep it here, but if AngularServiceWorkerPlugin changes we remove it.
75- extraPlugins . push ( new GlobCopyWebpackPlugin ( {
76- patterns : [
77- 'ngsw-manifest.json' ,
78- { glob : 'ngsw-manifest.json' , input : path . resolve ( projectRoot , appConfig . root ) , output : '' }
79- ] ,
80- globOptions : {
81- cwd : projectRoot ,
82- optional : true ,
83- } ,
84- } ) ) ;
85-
86- // Load the Webpack plugin for manifest generation and install it.
87- const AngularServiceWorkerPlugin = require ( '@angular/service-worker/build/webpack' )
88- . AngularServiceWorkerPlugin ;
89- extraPlugins . push ( new AngularServiceWorkerPlugin ( {
90- baseHref : buildOptions . baseHref || '/' ,
91- } ) ) ;
92-
93- // Copy the worker script into assets.
94- const workerContents = fs . readFileSync ( workerPath ) . toString ( ) ;
95- extraPlugins . push ( new StaticAssetPlugin ( 'worker-basic.min.js' , workerContents ) ) ;
96-
97- // Add a script to index.html that registers the service worker.
98- // TODO(alxhub): inline this script somehow.
99- entryPoints [ 'sw-register' ] = [ registerPath ] ;
100109 }
101110
102111 if ( buildOptions . extractLicenses ) {
0 commit comments