| 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 {WorkboxPackageJSON} from '../types';
|
|---|
| 13 | import {errors} from './errors';
|
|---|
| 14 |
|
|---|
| 15 | // Used to filter the libraries to copy based on our package.json dependencies.
|
|---|
| 16 | const WORKBOX_PREFIX = 'workbox-';
|
|---|
| 17 |
|
|---|
| 18 | // The directory within each package containing the final bundles.
|
|---|
| 19 | const BUILD_DIR = 'build';
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * This copies over a set of runtime libraries used by Workbox into a
|
|---|
| 23 | * local directory, which should be deployed alongside your service worker file.
|
|---|
| 24 | *
|
|---|
| 25 | * As an alternative to deploying these local copies, you could instead use
|
|---|
| 26 | * Workbox from its official CDN URL.
|
|---|
| 27 | *
|
|---|
| 28 | * This method is exposed for the benefit of developers using
|
|---|
| 29 | * {@link workbox-build.injectManifest} who would
|
|---|
| 30 | * prefer not to use the CDN copies of Workbox. Developers using
|
|---|
| 31 | * {@link workbox-build.generateSW} don't need to
|
|---|
| 32 | * explicitly call this method.
|
|---|
| 33 | *
|
|---|
| 34 | * @param {string} destDirectory The path to the parent directory under which
|
|---|
| 35 | * the new directory of libraries will be created.
|
|---|
| 36 | * @return {Promise<string>} The name of the newly created directory.
|
|---|
| 37 | *
|
|---|
| 38 | * @alias workbox-build.copyWorkboxLibraries
|
|---|
| 39 | */
|
|---|
| 40 | export async function copyWorkboxLibraries(
|
|---|
| 41 | destDirectory: string,
|
|---|
| 42 | ): Promise<string> {
|
|---|
| 43 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|---|
| 44 | const thisPkg: WorkboxPackageJSON = require('../../package.json');
|
|---|
| 45 | // Use the version string from workbox-build in the name of the parent
|
|---|
| 46 | // directory. This should be safe, because lerna will bump workbox-build's
|
|---|
| 47 | // pkg.version whenever one of the dependent libraries gets bumped, and we
|
|---|
| 48 | // care about versioning the dependent libraries.
|
|---|
| 49 | const workboxDirectoryName = `workbox-v${
|
|---|
| 50 | thisPkg.version ? thisPkg.version : ''
|
|---|
| 51 | }`;
|
|---|
| 52 | const workboxDirectoryPath = upath.join(destDirectory, workboxDirectoryName);
|
|---|
| 53 | await fse.ensureDir(workboxDirectoryPath);
|
|---|
| 54 |
|
|---|
| 55 | const copyPromises: Array<Promise<void>> = [];
|
|---|
| 56 | const librariesToCopy = Object.keys(thisPkg.dependencies || {}).filter(
|
|---|
| 57 | (dependency) => dependency.startsWith(WORKBOX_PREFIX),
|
|---|
| 58 | );
|
|---|
| 59 |
|
|---|
| 60 | for (const library of librariesToCopy) {
|
|---|
| 61 | // Get the path to the package on the user's filesystem by require-ing
|
|---|
| 62 | // the package's `package.json` file via the node resolution algorithm.
|
|---|
| 63 | const libraryPath = upath.dirname(
|
|---|
| 64 | require.resolve(`${library}/package.json`),
|
|---|
| 65 | );
|
|---|
| 66 |
|
|---|
| 67 | const buildPath = upath.join(libraryPath, BUILD_DIR);
|
|---|
| 68 |
|
|---|
| 69 | // fse.copy() copies all the files in a directory, not the directory itself.
|
|---|
| 70 | // See https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md#copysrc-dest-options-callback
|
|---|
| 71 | copyPromises.push(fse.copy(buildPath, workboxDirectoryPath));
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | try {
|
|---|
| 75 | await Promise.all(copyPromises);
|
|---|
| 76 | return workboxDirectoryName;
|
|---|
| 77 | } catch (error) {
|
|---|
| 78 | throw Error(
|
|---|
| 79 | `${errors['unable-to-copy-workbox-libraries']} ${
|
|---|
| 80 | error instanceof Error ? error.toString() : ''
|
|---|
| 81 | }`,
|
|---|
| 82 | );
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|