| 1 | /*
|
|---|
| 2 | Copyright 2019 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 {oneLine as ol} from 'common-tags';
|
|---|
| 10 | import upath from 'upath';
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Class for keeping track of which Workbox modules are used by the generated
|
|---|
| 14 | * service worker script.
|
|---|
| 15 | *
|
|---|
| 16 | * @private
|
|---|
| 17 | */
|
|---|
| 18 | export class ModuleRegistry {
|
|---|
| 19 | private readonly _modulesUsed: Map<string, {moduleName: string; pkg: string}>;
|
|---|
| 20 | /**
|
|---|
| 21 | * @private
|
|---|
| 22 | */
|
|---|
| 23 | constructor() {
|
|---|
| 24 | this._modulesUsed = new Map();
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * @return {Array<string>} A list of all of the import statements that are
|
|---|
| 29 | * needed for the modules being used.
|
|---|
| 30 | * @private
|
|---|
| 31 | */
|
|---|
| 32 | getImportStatements(): Array<string> {
|
|---|
| 33 | const workboxModuleImports: Array<string> = [];
|
|---|
| 34 |
|
|---|
| 35 | for (const [localName, {moduleName, pkg}] of this._modulesUsed) {
|
|---|
| 36 | // By default require.resolve returns the resolved path of the 'main'
|
|---|
| 37 | // field, which might be deeper than the package root. To work around
|
|---|
| 38 | // this, we can find the package's root by resolving its package.json and
|
|---|
| 39 | // strip the '/package.json' from the resolved path.
|
|---|
| 40 | const pkgJsonPath = require.resolve(`${pkg}/package.json`);
|
|---|
| 41 | const pkgRoot = upath.dirname(pkgJsonPath);
|
|---|
| 42 | const importStatement = ol`import {${moduleName} as ${localName}} from
|
|---|
| 43 | '${pkgRoot}/${moduleName}.mjs';`;
|
|---|
| 44 |
|
|---|
| 45 | workboxModuleImports.push(importStatement);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | return workboxModuleImports;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * @param {string} pkg The workbox package that the module belongs to.
|
|---|
| 53 | * @param {string} moduleName The name of the module to import.
|
|---|
| 54 | * @return {string} The local variable name that corresponds to that module.
|
|---|
| 55 | * @private
|
|---|
| 56 | */
|
|---|
| 57 | getLocalName(pkg: string, moduleName: string): string {
|
|---|
| 58 | return `${pkg.replace(/-/g, '_')}_${moduleName}`;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * @param {string} pkg The workbox package that the module belongs to.
|
|---|
| 63 | * @param {string} moduleName The name of the module to import.
|
|---|
| 64 | * @return {string} The local variable name that corresponds to that module.
|
|---|
| 65 | * @private
|
|---|
| 66 | */
|
|---|
| 67 | use(pkg: string, moduleName: string): string {
|
|---|
| 68 | const localName = this.getLocalName(pkg, moduleName);
|
|---|
| 69 | this._modulesUsed.set(localName, {moduleName, pkg});
|
|---|
| 70 |
|
|---|
| 71 | return localName;
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|