Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service worker in the CLI #4544

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"prefix": {
"type": "string"
},
"mobile": {
"serviceWorker": {
"description": "Experimental support for a service worker from @angular/service-worker.",
"type": "boolean",
"default": false
},
Expand Down
60 changes: 59 additions & 1 deletion packages/@angular/cli/models/webpack-configs/production.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as path from 'path';
import * as webpack from 'webpack';
import * as fs from 'fs';
import { stripIndent } from 'common-tags';
import { StaticAssetPlugin } from '../../plugins/static-asset';
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
import { CompressionPlugin } from '../../lib/webpack/compression-plugin';
import { WebpackConfigOptions } from '../webpack-config';

Expand All @@ -8,7 +12,61 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
const { projectRoot, buildOptions, appConfig } = wco;
const appRoot = path.resolve(projectRoot, appConfig.root);

let extraPlugins: any[] = [];
let entryPoints: {[key: string]: string[]} = {};

if (appConfig.serviceWorker) {
const nodeModules = path.resolve(projectRoot, 'node_modules');
const swModule = path.resolve(nodeModules, '@angular/service-worker');

// @angular/service-worker is required to be installed when serviceWorker is true.
if (!fs.existsSync(swModule)) {
throw new Error(stripIndent`
Your project is configured with serviceWorker = true, but @angular/service-worker
is not installed. Run \`npm install --save-dev @angular/service-worker\`
and try again, or run \`ng set apps.0.serviceWorker=false\` in your angular-cli.json.
`);
}

// Path to the worker script itself.
const workerPath = path.resolve(swModule, 'bundles/worker-basic.min.js');

// Path to a small script to register a service worker.
const registerPath = path.resolve(swModule, 'build/assets/register-basic.min.js');

// Sanity check - both of these files should be present in @angular/service-worker.
if (!fs.existsSync(workerPath) || !fs.existsSync(registerPath)) {
throw new Error(stripIndent`
The installed version of @angular/service-worker isn't supported by the CLI.
Please install a supported version. The following files should exist:
- ${registerPath}
- ${workerPath}
`);
}

extraPlugins.push(new GlobCopyWebpackPlugin({
patterns: ['ngsw-manifest.json'],
globOptions: {
optional: true,
},
}));

// Load the Webpack plugin for manifest generation and install it.
const AngularServiceWorkerPlugin = require('@angular/service-worker/build/webpack')
.AngularServiceWorkerPlugin;
extraPlugins.push(new AngularServiceWorkerPlugin());

// Copy the worker script into assets.
const workerContents = fs.readFileSync(workerPath).toString();
extraPlugins.push(new StaticAssetPlugin('worker-basic.min.js', workerContents));

// Add a script to index.html that registers the service worker.
// TODO(alxhub): inline this script somehow.
entryPoints['sw-register'] = [registerPath];
}

return {
entry: entryPoints,
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
Expand All @@ -24,6 +82,6 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
test: /\.js$|\.html$|\.css$/,
threshold: 10240
})
]
].concat(extraPlugins)
};
};
19 changes: 15 additions & 4 deletions packages/@angular/cli/plugins/glob-copy-webpack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import * as denodeify from 'denodeify';
const globPromise = <any>denodeify(glob);
const statPromise = <any>denodeify(fs.stat);

function isDirectory(path: string) {
try {
return fs.statSync(path).isDirectory();
} catch (_) {
return false;
}
}

export interface GlobCopyWebpackPluginOptions {
patterns: string[];
globOptions: any;
Expand All @@ -17,9 +25,10 @@ export class GlobCopyWebpackPlugin {
apply(compiler: any): void {
let { patterns, globOptions } = this.options;
let context = globOptions.cwd || compiler.options.context;
let optional = !!globOptions.optional;

// convert dir patterns to globs
patterns = patterns.map(pattern => fs.statSync(path.resolve(context, pattern)).isDirectory()
patterns = patterns.map(pattern => isDirectory(path.resolve(context, pattern))
? pattern += '/**/*'
: pattern
);
Expand All @@ -37,15 +46,17 @@ export class GlobCopyWebpackPlugin {
.then((stat: any) => compilation.assets[relPath] = {
size: () => stat.size,
source: () => fs.readFileSync(path.resolve(context, relPath))
});
})
.catch((err: any) => optional ? Promise.resolve() : Promise.reject(err));

Promise.all(globs)
// flatten results
.then(globResults => [].concat.apply([], globResults))
// add each file to compilation assets
.then(relPaths => relPaths.forEach((relPath: string) => addAsset(relPath)))
.then((relPaths: string[]) =>
Promise.all(relPaths.map((relPath: string) => addAsset(relPath))))
.catch((err) => compilation.errors.push(err))
.then(cb);
.then(() => cb());
});
}
}
16 changes: 16 additions & 0 deletions packages/@angular/cli/plugins/static-asset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from 'fs';

export class StaticAssetPlugin {

constructor(private name: string, private contents: string) {}

apply(compiler: any): void {
compiler.plugin('emit', (compilation: any, cb: Function) => {
compilation.assets[this.name] = {
size: () => this.contents.length,
source: () => this.contents,
};
cb();
});
}
}
2 changes: 1 addition & 1 deletion packages/@angular/cli/utilities/package-chunk-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ExtraEntry, extraEntryParser } from '../models/webpack-configs/utils';
// Sort chunks according to a predefined order:
// inline, polyfills, all scripts, all styles, vendor, main
export function packageChunkSort(appConfig: any) {
let entryPoints = ['inline', 'polyfills'];
let entryPoints = ['inline', 'polyfills', 'sw-register'];

const pushExtraEntries = (extraEntry: ExtraEntry) => {
if (entryPoints.indexOf(extraEntry.entry) === -1) {
Expand Down
10 changes: 8 additions & 2 deletions scripts/publish/validate_dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const ANGULAR_PACKAGES = [
'@angular/compiler-cli',
'@angular/core'
];
const OPTIONAL_PACKAGES = [
'@angular/service-worker'
];


function listImportedModules(source) {
Expand Down Expand Up @@ -121,7 +124,9 @@ for (const packageName of Object.keys(packages)) {
.concat(Object.keys(packageJson['devDependencies'] || {}))
.concat(Object.keys(packageJson['peerDependencies'] || {}));

const missingDeps = dependencies.filter(d => allDeps.indexOf(d) == -1);
const missingDeps = dependencies
.filter(d => allDeps.indexOf(d) == -1)
.filter(d => OPTIONAL_PACKAGES.indexOf(d) == -1);
reportMissingDependencies(missingDeps);

const overDeps = allDeps.filter(d => dependencies.indexOf(d) == -1)
Expand All @@ -141,7 +146,8 @@ const allRootDeps = []

const internalPackages = Object.keys(packages);
const missingRootDeps = overallDeps.filter(d => allRootDeps.indexOf(d) == -1)
.filter(d => internalPackages.indexOf(d) == -1);
.filter(d => internalPackages.indexOf(d) == -1)
.filter(x => OPTIONAL_PACKAGES.indexOf(x) == -1);
reportMissingDependencies(missingRootDeps);

const overRootDeps = allRootDeps.filter(d => overallDeps.indexOf(d) == -1)
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/tests/build/service-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {join} from 'path';
import {npm} from '../../utils/process';
import {expectFileToExist} from '../../utils/fs';
import {ng} from '../../utils/process';

export default function() {
// Can't use the `ng` helper because somewhere the environment gets
// stuck to the first build done
return npm('install', '@angular/service-worker')
.then(() => ng('set', 'apps.0.serviceWorker=true'))
.then(() => ng('build', '--prod'))
.then(() => expectFileToExist(join(process.cwd(), 'dist')))
.then(() => expectFileToExist(join(process.cwd(), 'dist/ngsw-manifest.json')));
}