| [9af201e] | 1 | /*
|
|---|
| 2 | Copyright 2021 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 {ok} from 'assert';
|
|---|
| 10 |
|
|---|
| 11 | import {BuildType, WorkboxPackageJSON} from '../types';
|
|---|
| 12 | import {errors} from './errors';
|
|---|
| 13 | import * as cdn from '../cdn-details.json';
|
|---|
| 14 |
|
|---|
| 15 | function getVersionedURL(): string {
|
|---|
| 16 | return `${getCDNPrefix()}/${cdn.latestVersion}`;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | function getCDNPrefix() {
|
|---|
| 20 | return `${cdn.origin}/${cdn.bucketName}/${cdn.releasesDir}`;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | export function getModuleURL(moduleName: string, buildType: BuildType): string {
|
|---|
| 24 | ok(moduleName, errors['no-module-name']);
|
|---|
| 25 |
|
|---|
| 26 | if (buildType) {
|
|---|
| 27 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|---|
| 28 | const pkgJson: WorkboxPackageJSON = require(`${moduleName}/package.json`);
|
|---|
| 29 | if (buildType === 'dev' && pkgJson.workbox && pkgJson.workbox.prodOnly) {
|
|---|
| 30 | // This is not due to a public-facing exception, so just throw an Error(),
|
|---|
| 31 | // without creating an entry in errors.js.
|
|---|
| 32 | throw Error(`The 'dev' build of ${moduleName} is not available.`);
|
|---|
| 33 | }
|
|---|
| 34 | return `${getVersionedURL()}/${moduleName}.${buildType.slice(0, 4)}.js`;
|
|---|
| 35 | }
|
|---|
| 36 | return `${getVersionedURL()}/${moduleName}.js`;
|
|---|
| 37 | }
|
|---|