cdn-utils.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. /*
  3. Copyright 2021 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  9. if (k2 === undefined) k2 = k;
  10. var desc = Object.getOwnPropertyDescriptor(m, k);
  11. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  12. desc = { enumerable: true, get: function() { return m[k]; } };
  13. }
  14. Object.defineProperty(o, k2, desc);
  15. }) : (function(o, m, k, k2) {
  16. if (k2 === undefined) k2 = k;
  17. o[k2] = m[k];
  18. }));
  19. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  20. Object.defineProperty(o, "default", { enumerable: true, value: v });
  21. }) : function(o, v) {
  22. o["default"] = v;
  23. });
  24. var __importStar = (this && this.__importStar) || function (mod) {
  25. if (mod && mod.__esModule) return mod;
  26. var result = {};
  27. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  28. __setModuleDefault(result, mod);
  29. return result;
  30. };
  31. Object.defineProperty(exports, "__esModule", { value: true });
  32. exports.getModuleURL = void 0;
  33. const assert_1 = require("assert");
  34. const errors_1 = require("./errors");
  35. const cdn = __importStar(require("../cdn-details.json"));
  36. function getVersionedURL() {
  37. return `${getCDNPrefix()}/${cdn.latestVersion}`;
  38. }
  39. function getCDNPrefix() {
  40. return `${cdn.origin}/${cdn.bucketName}/${cdn.releasesDir}`;
  41. }
  42. function getModuleURL(moduleName, buildType) {
  43. (0, assert_1.ok)(moduleName, errors_1.errors['no-module-name']);
  44. if (buildType) {
  45. // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  46. const pkgJson = require(`${moduleName}/package.json`);
  47. if (buildType === 'dev' && pkgJson.workbox && pkgJson.workbox.prodOnly) {
  48. // This is not due to a public-facing exception, so just throw an Error(),
  49. // without creating an entry in errors.js.
  50. throw Error(`The 'dev' build of ${moduleName} is not available.`);
  51. }
  52. return `${getVersionedURL()}/${moduleName}.${buildType.slice(0, 4)}.js`;
  53. }
  54. return `${getVersionedURL()}/${moduleName}.js`;
  55. }
  56. exports.getModuleURL = getModuleURL;