copy-workbox-libraries.js 3.3 KB

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