| 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 fse from 'fs-extra';
|
|---|
| 10 | import upath from 'upath';
|
|---|
| 11 |
|
|---|
| 12 | import {bundle} from './bundle';
|
|---|
| 13 | import {errors} from './errors';
|
|---|
| 14 | import {GenerateSWOptions, ManifestEntry} from '../types';
|
|---|
| 15 | import {populateSWTemplate} from './populate-sw-template';
|
|---|
| 16 |
|
|---|
| 17 | export async function writeSWUsingDefaultTemplate({
|
|---|
| 18 | babelPresetEnvTargets,
|
|---|
| 19 | cacheId,
|
|---|
| 20 | cleanupOutdatedCaches,
|
|---|
| 21 | clientsClaim,
|
|---|
| 22 | directoryIndex,
|
|---|
| 23 | disableDevLogs,
|
|---|
| 24 | ignoreURLParametersMatching,
|
|---|
| 25 | importScripts,
|
|---|
| 26 | inlineWorkboxRuntime,
|
|---|
| 27 | manifestEntries,
|
|---|
| 28 | mode,
|
|---|
| 29 | navigateFallback,
|
|---|
| 30 | navigateFallbackDenylist,
|
|---|
| 31 | navigateFallbackAllowlist,
|
|---|
| 32 | navigationPreload,
|
|---|
| 33 | offlineGoogleAnalytics,
|
|---|
| 34 | runtimeCaching,
|
|---|
| 35 | skipWaiting,
|
|---|
| 36 | sourcemap,
|
|---|
| 37 | swDest,
|
|---|
| 38 | }: GenerateSWOptions & {manifestEntries: Array<ManifestEntry>}): Promise<
|
|---|
| 39 | Array<string>
|
|---|
| 40 | > {
|
|---|
| 41 | const outputDir = upath.dirname(swDest);
|
|---|
| 42 | try {
|
|---|
| 43 | await fse.mkdirp(outputDir);
|
|---|
| 44 | } catch (error) {
|
|---|
| 45 | throw new Error(
|
|---|
| 46 | `${errors['unable-to-make-sw-directory']}. ` +
|
|---|
| 47 | `'${error instanceof Error && error.message ? error.message : ''}'`,
|
|---|
| 48 | );
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | const unbundledCode = populateSWTemplate({
|
|---|
| 52 | cacheId,
|
|---|
| 53 | cleanupOutdatedCaches,
|
|---|
| 54 | clientsClaim,
|
|---|
| 55 | directoryIndex,
|
|---|
| 56 | disableDevLogs,
|
|---|
| 57 | ignoreURLParametersMatching,
|
|---|
| 58 | importScripts,
|
|---|
| 59 | manifestEntries,
|
|---|
| 60 | navigateFallback,
|
|---|
| 61 | navigateFallbackDenylist,
|
|---|
| 62 | navigateFallbackAllowlist,
|
|---|
| 63 | navigationPreload,
|
|---|
| 64 | offlineGoogleAnalytics,
|
|---|
| 65 | runtimeCaching,
|
|---|
| 66 | skipWaiting,
|
|---|
| 67 | });
|
|---|
| 68 |
|
|---|
| 69 | try {
|
|---|
| 70 | const files = await bundle({
|
|---|
| 71 | babelPresetEnvTargets,
|
|---|
| 72 | inlineWorkboxRuntime,
|
|---|
| 73 | mode,
|
|---|
| 74 | sourcemap,
|
|---|
| 75 | swDest,
|
|---|
| 76 | unbundledCode,
|
|---|
| 77 | });
|
|---|
| 78 |
|
|---|
| 79 | const filePaths: Array<string> = [];
|
|---|
| 80 |
|
|---|
| 81 | for (const file of files) {
|
|---|
| 82 | const filePath = upath.resolve(file.name);
|
|---|
| 83 | filePaths.push(filePath);
|
|---|
| 84 | await fse.writeFile(filePath, file.contents);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | return filePaths;
|
|---|
| 88 | } catch (error) {
|
|---|
| 89 | const err = error as NodeJS.ErrnoException;
|
|---|
| 90 | if (err.code === 'EISDIR') {
|
|---|
| 91 | // See https://github.com/GoogleChrome/workbox/issues/612
|
|---|
| 92 | throw new Error(errors['sw-write-failure-directory']);
|
|---|
| 93 | }
|
|---|
| 94 | throw new Error(`${errors['sw-write-failure']} '${err.message}'`);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|