| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import upath from 'upath';
|
|---|
| 10 |
|
|---|
| 11 | import {BuildResult, GetManifestOptions, GenerateSWOptions} from './types';
|
|---|
| 12 | import {getFileManifestEntries} from './lib/get-file-manifest-entries';
|
|---|
| 13 | import {rebasePath} from './lib/rebase-path';
|
|---|
| 14 | import {validateGenerateSWOptions} from './lib/validate-options';
|
|---|
| 15 | import {writeSWUsingDefaultTemplate} from './lib/write-sw-using-default-template';
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * This method creates a list of URLs to precache, referred to as a "precache
|
|---|
| 19 | * manifest", based on the options you provide.
|
|---|
| 20 | *
|
|---|
| 21 | * It also takes in additional options that configures the service worker's
|
|---|
| 22 | * behavior, like any `runtimeCaching` rules it should use.
|
|---|
| 23 | *
|
|---|
| 24 | * Based on the precache manifest and the additional configuration, it writes
|
|---|
| 25 | * a ready-to-use service worker file to disk at `swDest`.
|
|---|
| 26 | *
|
|---|
| 27 | * ```
|
|---|
| 28 | * // The following lists some common options; see the rest of the documentation
|
|---|
| 29 | * // for the full set of options and defaults.
|
|---|
| 30 | * const {count, size, warnings} = await generateSW({
|
|---|
| 31 | * dontCacheBustURLsMatching: [new RegExp('...')],
|
|---|
| 32 | * globDirectory: '...',
|
|---|
| 33 | * globPatterns: ['...', '...'],
|
|---|
| 34 | * maximumFileSizeToCacheInBytes: ...,
|
|---|
| 35 | * navigateFallback: '...',
|
|---|
| 36 | * runtimeCaching: [{
|
|---|
| 37 | * // Routing via a matchCallback function:
|
|---|
| 38 | * urlPattern: ({request, url}) => ...,
|
|---|
| 39 | * handler: '...',
|
|---|
| 40 | * options: {
|
|---|
| 41 | * cacheName: '...',
|
|---|
| 42 | * expiration: {
|
|---|
| 43 | * maxEntries: ...,
|
|---|
| 44 | * },
|
|---|
| 45 | * },
|
|---|
| 46 | * }, {
|
|---|
| 47 | * // Routing via a RegExp:
|
|---|
| 48 | * urlPattern: new RegExp('...'),
|
|---|
| 49 | * handler: '...',
|
|---|
| 50 | * options: {
|
|---|
| 51 | * cacheName: '...',
|
|---|
| 52 | * plugins: [..., ...],
|
|---|
| 53 | * },
|
|---|
| 54 | * }],
|
|---|
| 55 | * skipWaiting: ...,
|
|---|
| 56 | * swDest: '...',
|
|---|
| 57 | * });
|
|---|
| 58 | * ```
|
|---|
| 59 | *
|
|---|
| 60 | * @memberof workbox-build
|
|---|
| 61 | */
|
|---|
| 62 | export async function generateSW(
|
|---|
| 63 | config: GenerateSWOptions,
|
|---|
| 64 | ): Promise<BuildResult> {
|
|---|
| 65 | const options = validateGenerateSWOptions(config);
|
|---|
| 66 | let entriesResult;
|
|---|
| 67 |
|
|---|
| 68 | if (options.globDirectory) {
|
|---|
| 69 | // Make sure we leave swDest out of the precache manifest.
|
|---|
| 70 | options.globIgnores!.push(
|
|---|
| 71 | rebasePath({
|
|---|
| 72 | baseDirectory: options.globDirectory,
|
|---|
| 73 | file: options.swDest,
|
|---|
| 74 | }),
|
|---|
| 75 | );
|
|---|
| 76 |
|
|---|
| 77 | // If we create an extra external runtime file, ignore that, too.
|
|---|
| 78 | // See https://rollupjs.org/guide/en/#outputchunkfilenames for naming.
|
|---|
| 79 | if (!options.inlineWorkboxRuntime) {
|
|---|
| 80 | const swDestDir = upath.dirname(options.swDest);
|
|---|
| 81 | const workboxRuntimeFile = upath.join(swDestDir, 'workbox-*.js');
|
|---|
| 82 | options.globIgnores!.push(
|
|---|
| 83 | rebasePath({
|
|---|
| 84 | baseDirectory: options.globDirectory,
|
|---|
| 85 | file: workboxRuntimeFile,
|
|---|
| 86 | }),
|
|---|
| 87 | );
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | // We've previously asserted that options.globDirectory is set, so this
|
|---|
| 91 | // should be a safe cast.
|
|---|
| 92 | entriesResult = await getFileManifestEntries(options as GetManifestOptions);
|
|---|
| 93 | } else {
|
|---|
| 94 | entriesResult = {
|
|---|
| 95 | count: 0,
|
|---|
| 96 | manifestEntries: [],
|
|---|
| 97 | size: 0,
|
|---|
| 98 | warnings: [],
|
|---|
| 99 | };
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | const filePaths = await writeSWUsingDefaultTemplate(
|
|---|
| 103 | Object.assign(
|
|---|
| 104 | {
|
|---|
| 105 | manifestEntries: entriesResult.manifestEntries,
|
|---|
| 106 | },
|
|---|
| 107 | options,
|
|---|
| 108 | ),
|
|---|
| 109 | );
|
|---|
| 110 |
|
|---|
| 111 | return {
|
|---|
| 112 | filePaths,
|
|---|
| 113 | count: entriesResult.count,
|
|---|
| 114 | size: entriesResult.size,
|
|---|
| 115 | warnings: entriesResult.warnings,
|
|---|
| 116 | };
|
|---|
| 117 | }
|
|---|