Skip to content

Commit abbd4a6

Browse files
alxhubhansl
authored andcommitted
fix(@angular/cli): support ejection of service worker apps
1 parent 4e47e9b commit abbd4a6

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/@angular/cli/tasks/eject.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getAppFromConfig } from '../utilities/app-utils';
77
import { EjectTaskOptions } from '../commands/eject';
88
import { NgCliWebpackConfig } from '../models/webpack-config';
99
import { CliConfig } from '../models/config';
10+
import { usesServiceWorker } from '../utilities/service-worker';
1011
import { stripBom } from '../utilities/strip-bom';
1112
import { AotPlugin, AngularCompilerPlugin } from '@ngtools/webpack';
1213
import { PurifyPlugin } from '@angular-devkit/build-optimizer';
@@ -484,7 +485,6 @@ class JsonWebpackSerializer {
484485
}
485486
}
486487

487-
488488
export default Task.extend({
489489
run: function (runTaskOptions: EjectTaskOptions) {
490490
const project = this.project;
@@ -554,6 +554,16 @@ export default Task.extend({
554554
packageJson['scripts']['pree2e'] = pree2eNpmScript;
555555
packageJson['scripts']['e2e'] = 'protractor ./protractor.conf.js';
556556

557+
if (!!appConfig.serviceWorker && runTaskOptions.target === 'production' &&
558+
usesServiceWorker(project.root) && !!runTaskOptions.serviceWorker) {
559+
packageJson['scripts']['build'] += ' && npm run sw-config && npm run sw-copy';
560+
packageJson['scripts']['sw-config'] = `ngsw-config ${outputPath} src/ngsw-config.json`;
561+
packageJson['scripts']['sw-copy'] =
562+
`cpx node_modules/@angular/service-worker/ngsw-worker.js ${outputPath}`;
563+
564+
packageJson['devDependencies']['cpx'] = '^1.5.0';
565+
}
566+
557567
// Add new dependencies based on our dependencies.
558568
const ourPackageJson = require('../package.json');
559569
if (!packageJson['devDependencies']) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {join} from 'path';
22
import {getGlobalVariable} from '../../utils/env';
33
import {expectFileNotToExist, expectFileToExist, expectFileToMatch, writeFile} from '../../utils/fs';
4-
import {ng, silentNpm} from '../../utils/process';
4+
import {ng, npm, silentNpm} from '../../utils/process';
55

66
const MANIFEST = {
77
index: '/index.html',
@@ -42,5 +42,11 @@ export default function() {
4242
.then(() => expectFileToMatch('dist/ngsw.json', /"\/foo\/bar\/index.html"/))
4343
.then(() => ng('build', '--prod', '--service-worker=false'))
4444
.then(() => expectFileNotToExist('dist/ngsw.json'))
45+
.then(() => ng('eject', '--prod'))
46+
.then(() => silentNpm('install'))
47+
.then(() => npm('run', 'build'))
48+
.then(() => expectFileToMatch('package.json', /"sw-config"/))
49+
.then(() => expectFileToExist(join(process.cwd(), 'dist/ngsw-worker.js')))
50+
.then(() => expectFileToExist(join(process.cwd(), 'dist/ngsw.json')))
4551
.then(() => ng('set', 'apps.0.serviceWorker=false'));
4652
}

0 commit comments

Comments
 (0)