# All files / src/External ExternalManager.ts

34.61% Statements 9/26
0% Branches 0/2
14.28% Functions 1/7
37.5% Lines 9/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    751x
    1x
    1x
    1x
    1x

    1x

    1x

    3x
    3x

     import path from 'path';
    import uniqBy from 'lodash/uniqBy';
    import { External } from './External';
    import * as fsUtil from '../utils/fsUtil';
    import * as urlUtil from '../utils/urlUtil';
    import type { DynamicSrc } from '../Page/PageSources';
    import type { VariableProcessor } from '../variables/VariableProcessor';
    import type { SiteLinkManager } from '../html/SiteLinkManager';
    import type { PluginManager } from '../plugins/PluginManager';
    import { NodeProcessorConfig } from '../html/NodeProcessor';

    const _ = {
      uniqBy,
    };

    export type ExternalManagerConfig = NodeProcessorConfig & {
      variableProcessor: VariableProcessor,
      siteLinkManager: SiteLinkManager,
      pluginManager: PluginManager,
    };

    /**
     * Manages and generates external files (<panel src="...">) referenced in pages and layouts.
     */
    export class ExternalManager {
      config: ExternalManagerConfig;
      builtFiles: Record<string, Promise<External>>;

      constructor(cfg: ExternalManagerConfig) {
        this.config = cfg;
        this.builtFiles = {};
      }

      reset() {
        this.builtFiles = {};
      }

      /**
       * Generates the dependencies referenced by the dependencies provided, and adds any
       * collected sources to the includedFiles set.
       * @param dependencies
       * @param {Set<string>} includedFiles
       * @return {Promise<unknown[]>}
       */
      async generateDependencies(dependencies: DynamicSrc[],
                                 includedFiles: Set<string>,
                                 userScriptsAndStyles: string[]) {
        const resolvingExternals: Promise<External>[] = [];

        _.uniqBy(dependencies, d => d.asIfTo).forEach((src) => {
          Iif (urlUtil.isUrl(src.to)) {
            return;
          }

          const relativePath = path.relative(this.config.rootPath, src.asIfTo);
          const resultPath = path.join(this.config.outputPath, relativePath);
          const resultPathWithExternalExt = fsUtil.setExtension(resultPath, '._include_.html');

          Iif (!(resultPathWithExternalExt in this.builtFiles)) {
            const external = new External(this, src.to, userScriptsAndStyles);
            this.builtFiles[resultPathWithExternalExt] = external.resolveDependency(src.asIfTo,
                                                                                    resultPathWithExternalExt,
                                                                                    this.config);
          }

          resolvingExternals.push(this.builtFiles[resultPathWithExternalExt]);
        });

        const externals = await Promise.all(resolvingExternals);
        externals.forEach((external) => {
          external.includedFiles.forEach(filePath => includedFiles.add(filePath));
        });
      }
    }