All files / builder/application library-template-scope.service.ts

100% Statements 28/28
80% Branches 8/10
100% Functions 9/9
100% Lines 28/28

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97  1x   1x                               1x 11x 9x 9x       10x                   10x 10x 10x           10x 150x 150x       150x           10x     10x 10x 10x           10x 150x 140x   10x     10x   9x       10x     10x           9x       10x      
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Injectable } from 'static-injector';
import * as webpack from 'webpack';
import { TemplateScopeSymbol } from './const';
 
export type TemplateScopeOutside = Omit<
  LibraryTemplateScopeService,
  Exclude<
    keyof LibraryTemplateScopeService,
    'setScopeLibraryUseComponents' | 'setScopeExtraUseComponents'
  >
>;
export interface ExtraTemplateData {
  useComponents: Record<string, string>;
  templateList: string[];
  configPath?: string;
  templatePath?: string;
}
 
@Injectable()
export class LibraryTemplateScopeService {
  private scopeExtraUseComponentsMap = new Map<string, ExtraTemplateData>();
  private scopeLibraryUseComponentsMap = new Map<string, ExtraTemplateData[]>();
  // 追加模板
  constructor() {}
  register(compilation: webpack.Compilation) {
    (compilation as any)[TemplateScopeSymbol] = {
      setScopeExtraUseComponents: this.setScopeExtraUseComponents,
      setScopeLibraryUseComponents: this.setScopeLibraryUseComponents,
    } as TemplateScopeOutside;
  }
 
  exportLibraryComponentConfig() {
    const list: {
      filePath: string;
      content: { component: boolean; usingComponents: Record<string, string> };
    }[] = [];
    this.scopeLibraryUseComponentsMap.forEach((obj, libraryScope) => {
      const extraData = this.scopeExtraUseComponentsMap.get(libraryScope) || {
        useComponents: {},
      };
      // if (!extraData) {
      //   throw new Error(`没有找到${libraryScope}对应的配置`);
      // }
      for (const item of obj) {
        const configPath = item.configPath!;
        const usingComponents = {
          ...item.useComponents,
          ...extraData.useComponents,
        };
        list.push({
          filePath: configPath,
          content: { component: true, usingComponents: usingComponents },
        });
      }
    });
    return list;
  }
  exportLibraryTemplate() {
    const fileGroup: Record<string, string> = {};
    this.scopeLibraryUseComponentsMap.forEach((obj, libraryScope) => {
      const extraData = this.scopeExtraUseComponentsMap.get(libraryScope) || {
        templateList: [],
      };
      // if (!extraData) {
      //   throw new Error(`没有找到${libraryScope}对应的配置`);
      // }
      for (const item of obj) {
        if (fileGroup[item.templatePath!]) {
          continue;
        }
        fileGroup[item.templatePath!] = extraData.templateList.join('');
      }
    });
    return fileGroup;
  }
  setScopeExtraUseComponents = (
    libraryScope: string,
    extraData: ExtraTemplateData
  ) => {
    const data: ExtraTemplateData = this.scopeExtraUseComponentsMap.get(
      libraryScope
    ) || { useComponents: {}, templateList: [] };
    this.scopeExtraUseComponentsMap.set(libraryScope, {
      useComponents: { ...data.useComponents, ...extraData.useComponents },
      templateList: [...data.templateList, ...extraData.templateList],
    });
  };
 
  setScopeLibraryUseComponents = (
    libraryScope: string,
    libraryUseComponents: ExtraTemplateData[]
  ) => {
    this.scopeLibraryUseComponentsMap.set(libraryScope, libraryUseComponents);
  };
}