All files / builder/application/loader library-template.loader.ts

76% Statements 19/25
50% Branches 3/6
100% Functions 1/1
76% Lines 19/25

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  1x         1x   1x 1x 1x     1x         97x 97x 97x 97x 97x     97x     97x 10x 10x   10x                             97x     97x                           97x    
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createCssSelectorForTs } from 'cyia-code-util';
import { Injector } from 'static-injector';
import { VariableDeclaration } from 'typescript';
import * as webpack from 'webpack';
import { TemplateScopeOutside } from '../../application/library-template-scope.service';
import { GLOBAL_TEMPLATE_SUFFIX } from '../../library';
import { ExtraTemplateData } from '../../library/type';
import { BuildPlatform } from '../../platform/platform';
import { literalResolve } from '../../util';
import { InjectorSymbol, TemplateScopeSymbol } from '../const';
import { LibraryTemplateLiteralConvertOptions } from '../type';
 
export default async function (
  this: webpack.LoaderContext<any>,
  data: string,
  map: string
) {
  const callback = this.async();
  const selector = createCssSelectorForTs(data);
  const injector: Injector = (this._compilation! as any)[InjectorSymbol];
  const buildPlatform = injector.get(BuildPlatform);
  const templateScopeOutside = (this._compilation as any)[
    TemplateScopeSymbol
  ] as TemplateScopeOutside;
  const selfTemplateNode = selector.queryOne(
    `VariableDeclaration[name="$self_${GLOBAL_TEMPLATE_SUFFIX}"]`
  ) as VariableDeclaration;
  if (selfTemplateNode) {
    const content = selfTemplateNode.initializer!.getText();
    const config: ExtraTemplateData = literalResolve(content);
 
    this.emitFile(
      config.outputPath + buildPlatform.fileExtname.contentTemplate,
      literalResolve<LibraryTemplateLiteralConvertOptions>(
        `\`${config.template}\``,
        {
          directivePrefix:
            buildPlatform.templateTransform.getData().directivePrefix,
          eventListConvert: buildPlatform.templateTransform.eventListConvert,
          templateInterpolation:
            buildPlatform.templateTransform.templateInterpolation,
          fileExtname: buildPlatform.fileExtname,
        }
      )
    );
  }
  const libraryTemplateNode = selector.queryOne(
    `VariableDeclaration[name="library_${GLOBAL_TEMPLATE_SUFFIX}"]`
  ) as VariableDeclaration;
  Iif (libraryTemplateNode) {
    const content = libraryTemplateNode.initializer!.getText();
    const config: Record<string, ExtraTemplateData> = literalResolve(content);
 
    for (const key in config) {
      if (Object.prototype.hasOwnProperty.call(config, key)) {
        const element = config[key];
        templateScopeOutside.setScopeExtraUseComponents(key, {
          useComponents: element.useComponents!,
          templateList: [element.template],
        });
      }
    }
  }
  callback(undefined, data, map);
}