Skip to content

Commit f3644a9

Browse files
alxhubhansl
authored andcommitted
fix(@angular/cli): pass the base href through to the sw plugin
1 parent 7f03b5a commit f3644a9

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/@angular/cli/models/webpack-configs/production.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
22
import * as webpack from 'webpack';
33
import * as fs from 'fs';
4+
import * as semver from 'semver';
45
import { stripIndent } from 'common-tags';
56
import { StaticAssetPlugin } from '../../plugins/static-asset';
67
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
@@ -26,6 +27,19 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
2627
`);
2728
}
2829

30+
// Read the version of @angular/service-worker and throw if it doesn't match the
31+
// expected version.
32+
const allowedVersion = '>= 1.0.0-beta.5 < 2.0.0';
33+
const swPackageJson = fs.readFileSync(`${swModule}/package.json`).toString();
34+
const swVersion = JSON.parse(swPackageJson)['version'];
35+
if (!semver.satisfies(swVersion, allowedVersion)) {
36+
throw new Error(stripIndent`
37+
The installed version of @angular/service-worker is ${swVersion}. This version of the CLI
38+
requires the @angular/service-worker version to satisfy ${allowedVersion}. Please upgrade
39+
your service worker version.
40+
`);
41+
}
42+
2943
// Path to the worker script itself.
3044
const workerPath = path.resolve(swModule, 'bundles/worker-basic.min.js');
3145

@@ -52,7 +66,9 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
5266
// Load the Webpack plugin for manifest generation and install it.
5367
const AngularServiceWorkerPlugin = require('@angular/service-worker/build/webpack')
5468
.AngularServiceWorkerPlugin;
55-
extraPlugins.push(new AngularServiceWorkerPlugin());
69+
extraPlugins.push(new AngularServiceWorkerPlugin({
70+
baseHref: buildOptions.baseHref || '/',
71+
}));
5672

5773
// Copy the worker script into assets.
5874
const workerContents = fs.readFileSync(workerPath).toString();

tests/e2e/tests/build/service-worker.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {join} from 'path';
22
import {getGlobalVariable} from '../../utils/env';
3-
import {expectFileToExist} from '../../utils/fs';
3+
import {expectFileToExist, expectFileToMatch} from '../../utils/fs';
44
import {ng, npm} from '../../utils/process';
55

66
export default function() {
@@ -15,5 +15,8 @@ export default function() {
1515
.then(() => ng('set', 'apps.0.serviceWorker=true'))
1616
.then(() => ng('build', '--prod'))
1717
.then(() => expectFileToExist(join(process.cwd(), 'dist')))
18-
.then(() => expectFileToExist(join(process.cwd(), 'dist/ngsw-manifest.json')));
18+
.then(() => expectFileToExist(join(process.cwd(), 'dist/ngsw-manifest.json')))
19+
.then(() => ng('build', '--prod', '--base-href=/foo/bar'))
20+
.then(() => expectFileToExist(join(process.cwd(), 'dist/ngsw-manifest.json')))
21+
.then(() => expectFileToMatch('dist/ngsw-manifest.json', /"\/foo\/bar\/index.html"/));
1922
}

0 commit comments

Comments
 (0)