@@ -10,9 +10,12 @@ import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
1010import { WebpackConfigOptions } from '../webpack-config' ;
1111import { readTsconfig } from '../../utilities/read-tsconfig' ;
1212import { requireProjectModule } from '../../utilities/require-project-module' ;
13+ import { NEW_SW_VERSION } from '../../utilities/service-worker' ;
1314
1415const UglifyJSPlugin = require ( 'uglifyjs-webpack-plugin' ) ;
1516
17+ const OLD_SW_VERSION = '>= 1.0.0-beta.5 < 2.0.0' ;
18+
1619/**
1720 * license-webpack-plugin has a peer dependency on webpack-sources, list it in a comment to
1821 * let the dependency validator know it is used.
@@ -44,63 +47,69 @@ export function getProdConfig(wco: WebpackConfigOptions) {
4447
4548 // Read the version of @angular /service-worker and throw if it doesn't match the
4649 // expected version.
47- const allowedVersion = '>= 1.0.0-beta.5 < 2.0.0' ;
4850 const swPackageJson = fs . readFileSync ( `${ swModule } /package.json` ) . toString ( ) ;
4951 const swVersion = JSON . parse ( swPackageJson ) [ 'version' ] ;
50- if ( ! semver . satisfies ( swVersion , allowedVersion ) ) {
52+
53+ const isLegacySw = semver . satisfies ( swVersion , OLD_SW_VERSION ) ;
54+ const isModernSw = semver . satisfies ( swVersion , NEW_SW_VERSION ) ;
55+
56+ if ( ! isLegacySw && ! isModernSw ) {
5157 throw new Error ( stripIndent `
5258 The installed version of @angular/service-worker is ${ swVersion } . This version of the CLI
53- requires the @angular/service-worker version to satisfy ${ allowedVersion } . Please upgrade
59+ requires the @angular/service-worker version to satisfy ${ OLD_SW_VERSION } . Please upgrade
5460 your service worker version.
5561 ` ) ;
5662 }
5763
58- // Path to the worker script itself.
59- const workerPath = path . resolve ( swModule , 'bundles/worker-basic.min.js' ) ;
60-
61- // Path to a small script to register a service worker.
62- const registerPath = path . resolve ( swModule , 'build/assets/register-basic.min.js' ) ;
63-
64- // Sanity check - both of these files should be present in @angular/service-worker.
65- if ( ! fs . existsSync ( workerPath ) || ! fs . existsSync ( registerPath ) ) {
66- throw new Error ( stripIndent `
67- The installed version of @angular/service-worker isn't supported by the CLI.
68- Please install a supported version. The following files should exist:
69- - ${ registerPath }
70- - ${ workerPath }
71- ` ) ;
64+ if ( isLegacySw ) {
65+ // Path to the worker script itself.
66+ const workerPath = path . resolve ( swModule , 'bundles/worker-basic.min.js' ) ;
67+
68+ // Path to a small script to register a service worker.
69+ const registerPath = path . resolve ( swModule , 'build/assets/register-basic.min.js' ) ;
70+
71+ // Sanity check - both of these files should be present in @angular/service-worker.
72+ if ( ! fs . existsSync ( workerPath ) || ! fs . existsSync ( registerPath ) ) {
73+ throw new Error ( stripIndent `
74+ The installed version of @angular/service-worker isn't supported by the CLI.
75+ Please install a supported version. The following files should exist:
76+ - ${ registerPath }
77+ - ${ workerPath }
78+ ` ) ;
79+ }
80+
81+ // CopyWebpackPlugin replaces GlobCopyWebpackPlugin, but AngularServiceWorkerPlugin depends
82+ // on specific behaviour from latter.
83+ // AngularServiceWorkerPlugin expects the ngsw-manifest.json to be present in the 'emit' phase
84+ // but with CopyWebpackPlugin it's only there on 'after-emit'.
85+ // So for now we keep it here, but if AngularServiceWorkerPlugin changes we remove it.
86+ extraPlugins . push ( new GlobCopyWebpackPlugin ( {
87+ patterns : [
88+ 'ngsw-manifest.json' ,
89+ { glob : 'ngsw-manifest.json' ,
90+ input : path . resolve ( projectRoot , appConfig . root ) , output : '' }
91+ ] ,
92+ globOptions : {
93+ cwd : projectRoot ,
94+ optional : true ,
95+ } ,
96+ } ) ) ;
97+
98+ // Load the Webpack plugin for manifest generation and install it.
99+ const AngularServiceWorkerPlugin = require ( '@angular/service-worker/build/webpack' )
100+ . AngularServiceWorkerPlugin ;
101+ extraPlugins . push ( new AngularServiceWorkerPlugin ( {
102+ baseHref : buildOptions . baseHref || '/' ,
103+ } ) ) ;
104+
105+ // Copy the worker script into assets.
106+ const workerContents = fs . readFileSync ( workerPath ) . toString ( ) ;
107+ extraPlugins . push ( new StaticAssetPlugin ( 'worker-basic.min.js' , workerContents ) ) ;
108+
109+ // Add a script to index.html that registers the service worker.
110+ // TODO(alxhub): inline this script somehow.
111+ entryPoints [ 'sw-register' ] = [ registerPath ] ;
72112 }
73-
74- // CopyWebpackPlugin replaces GlobCopyWebpackPlugin, but AngularServiceWorkerPlugin depends
75- // on specific behaviour from latter.
76- // AngularServiceWorkerPlugin expects the ngsw-manifest.json to be present in the 'emit' phase
77- // but with CopyWebpackPlugin it's only there on 'after-emit'.
78- // So for now we keep it here, but if AngularServiceWorkerPlugin changes we remove it.
79- extraPlugins . push ( new GlobCopyWebpackPlugin ( {
80- patterns : [
81- 'ngsw-manifest.json' ,
82- { glob : 'ngsw-manifest.json' , input : path . resolve ( projectRoot , appConfig . root ) , output : '' }
83- ] ,
84- globOptions : {
85- cwd : projectRoot ,
86- optional : true ,
87- } ,
88- } ) ) ;
89-
90- // Load the Webpack plugin for manifest generation and install it.
91- const AngularServiceWorkerPlugin = require ( '@angular/service-worker/build/webpack' )
92- . AngularServiceWorkerPlugin ;
93- extraPlugins . push ( new AngularServiceWorkerPlugin ( {
94- baseHref : buildOptions . baseHref || '/' ,
95- } ) ) ;
96-
97- // Copy the worker script into assets.
98- const workerContents = fs . readFileSync ( workerPath ) . toString ( ) ;
99- extraPlugins . push ( new StaticAssetPlugin ( 'worker-basic.min.js' , workerContents ) ) ;
100-
101- // Add a script to index.html that registers the service worker.
102- // TODO(alxhub): inline this script somehow.
103- entryPoints [ 'sw-register' ] = [ registerPath ] ;
104113 }
105114
106115 if ( buildOptions . extractLicenses ) {
0 commit comments