cdn-utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. const assert = require('assert');
  9. const cdn = require('../cdn-details.json');
  10. const errors = require('./errors');
  11. const getCDNOrigin = () => {
  12. return `${cdn.origin}/${cdn.bucketName}/${cdn.releasesDir}`;
  13. };
  14. const getVersionedCDNURL = () => {
  15. return `${getCDNOrigin()}/${cdn.latestVersion}`;
  16. };
  17. const getModuleURL = (moduleName, buildType) => {
  18. assert(moduleName, errors['no-module-name']);
  19. if (buildType) {
  20. const pkgJson = require(`${moduleName}/package.json`);
  21. if (buildType === 'dev' && pkgJson.workbox.prodOnly) {
  22. // This is not due to a public-facing exception, so just throw an Error(),
  23. // without creating an entry in errors.js.
  24. throw Error(`The 'dev' build of ${moduleName} is not available.`);
  25. }
  26. return `${getVersionedCDNURL()}/${moduleName}.${buildType.slice(0, 4)}.js`;
  27. }
  28. return `${getVersionedCDNURL()}/${moduleName}.js`;
  29. };
  30. module.exports = {
  31. getCDNOrigin,
  32. getModuleURL
  33. };