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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 477x 477x 477x 477x 477x 226x 226x 195x 226x 477x 477x 477x 477x 33x 11x 11x 226x 226x 142x 142x 344x 344x 344x 344x 344x 142x 344x 172x 344x 142x 226x 226x 226x 226x 21x 21x 21x 21x 11x 226x 226x 11x 226x 226x 226x 226x 195x 1x 1x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 142x 142x 142x 142x 112x 30x 30x | import type { R3ComponentMetadata, R3DirectiveMetadata, SelectorMatcher, } from '@angular/compiler'; import type { NgtscProgram } from '@angular/compiler-cli'; import type { NgCompiler } from '@angular/compiler-cli/src/ngtsc/core'; import type { ClassRecord, TraitCompiler, } from '@angular/compiler-cli/src/ngtsc/transform'; import { createCssSelectorForTs } from 'cyia-code-util'; import path from 'path'; import { Injectable, Injector } from 'static-injector'; import ts, { ClassDeclaration } from 'typescript'; import { LIBRARY_COMPONENT_OUTPUT_PATH_SUFFIX, LIBRARY_DIRECTIVE_LISTENERS_SUFFIX, LIBRARY_DIRECTIVE_PROPERTIES_SUFFIX, } from '../library'; import { BuildPlatform } from '../platform/platform'; import { COMPONENT_META } from '../token/component.token'; import { angularCompilerPromise, literalResolve } from '../util'; import { ComponentCompilerService } from './component-compiler.service'; import { MetaCollection } from './meta-collection'; import { ComponentContext } from './parse-node'; import { ComponentMetaFromLibrary, DirectiveMetaFromLibrary, MetaFromLibrary, ResolvedDataGroup, UseComponent, } from './type'; @Injectable() export class MiniProgramCompilerService { private ngCompiler!: NgCompiler; // eslint-disable-next-line @typescript-eslint/no-explicit-any private componentMap = new Map<ClassDeclaration, R3ComponentMetadata<any>>(); private directiveMap = new Map<ClassDeclaration, R3DirectiveMetadata>(); private resolvedDataGroup: ResolvedDataGroup = { style: new Map<string, string[]>(), outputContent: new Map<string, string>(), useComponentPath: new Map< string, { localPath: UseComponent[]; libraryPath: UseComponent[]; } >(), otherMetaCollectionGroup: {}, }; constructor( private ngTscProgram: NgtscProgram, private injector: Injector, private buildPlatform: BuildPlatform ) {} init() { this.ngCompiler = this.ngTscProgram.compiler; // eslint-disable-next-line @typescript-eslint/no-explicit-any const traitCompiler: TraitCompiler = (this.ngCompiler as any).compilation .traitCompiler; // eslint-disable-next-line @typescript-eslint/no-explicit-any const classes = (traitCompiler as any).classes as Map< ts.ClassDeclaration, ClassRecord >; for (const [classDeclaration, classRecord] of classes) { const fileName = classDeclaration.getSourceFile().fileName; const componentTraits = classRecord.traits.filter( (trait) => trait.handler.name === 'ComponentDecoratorHandler' ); Iif (componentTraits.length > 1) { throw new Error('组件装饰器异常'); } componentTraits.forEach((trait) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const meta: R3ComponentMetadata<any> = { // eslint-disable-next-line @typescript-eslint/no-explicit-any ...(trait as any).analysis?.meta, // eslint-disable-next-line @typescript-eslint/no-explicit-any ...(trait as any).resolution, }; this.resolvedDataGroup.style.set( path.normalize(fileName), // eslint-disable-next-line @typescript-eslint/no-explicit-any ((trait as any)?.analysis?.styleUrls || []).map( (item: { url: string }) => this.resolveStyleUrl(fileName, item.url) ) ); this.componentMap.set( ts.getOriginalNode(classDeclaration) as ts.ClassDeclaration, meta ); }); const directiveTraits = classRecord.traits.filter( (trait) => trait.handler.name === 'DirectiveDecoratorHandler' ); Iif (directiveTraits.length > 1) { throw new Error('指令装饰器异常'); } directiveTraits.forEach((trait) => { this.directiveMap.set( ts.getOriginalNode(classDeclaration) as ts.ClassDeclaration, // eslint-disable-next-line @typescript-eslint/no-explicit-any (trait as any).analysis?.meta ); }); } } async exportComponentBuildMetaMap() { const { SelectorMatcher, CssSelector } = await angularCompilerPromise; for (const [classDeclaration, meta] of this.componentMap) { const fileName = path.normalize( classDeclaration.getSourceFile().fileName ); let directiveMatcher: SelectorMatcher | undefined; // todo 这里断点 if (meta.declarations.length > 0) { const matcher = new SelectorMatcher(); for (const directive of meta.declarations) { const selector = directive.selector; const directiveClassDeclaration = ts.getOriginalNode( // eslint-disable-next-line @typescript-eslint/no-explicit-any (directive as any).ref.node ) as ts.ClassDeclaration; const directiveMeta = this.directiveMap.get( directiveClassDeclaration ); const componentMeta = this.componentMap.get( directiveClassDeclaration ); let libraryMeta: MetaFromLibrary | undefined; if (directive.isComponent) { libraryMeta = this.getLibraryComponentMeta( // eslint-disable-next-line @typescript-eslint/no-explicit-any (directive as any).ref.node ); } if (!directive.isComponent && !directiveMeta) { libraryMeta = this.getLibraryDirectiveMeta( // eslint-disable-next-line @typescript-eslint/no-explicit-any (directive as any).ref.node ); } matcher.addSelectables(CssSelector.parse(selector), { directive, directiveMeta, componentMeta, libraryMeta, }); } directiveMatcher = matcher; } const componentBuildMeta = this.buildComponentMeta( directiveMatcher, meta ); this.resolvedDataGroup.outputContent.set( path.normalize(fileName), componentBuildMeta.content ); this.resolvedDataGroup.useComponentPath.set( path.normalize(fileName), componentBuildMeta.useComponentPath ); for (const key in componentBuildMeta.otherMetaGroup) { Eif ( Object.prototype.hasOwnProperty.call( componentBuildMeta.otherMetaGroup, key ) ) { const element = componentBuildMeta.otherMetaGroup[key]; this.resolvedDataGroup.otherMetaCollectionGroup[key] = this.resolvedDataGroup.otherMetaCollectionGroup[key] || new MetaCollection(); this.resolvedDataGroup.otherMetaCollectionGroup[key].merge(element); } } } this.resolvedDataGroup.useComponentPath.forEach((value, key) => { value.libraryPath = Array.from(new Set(value.libraryPath)); value.localPath = Array.from(new Set(value.localPath)); }); return this.resolvedDataGroup; } private buildComponentMeta( directiveMatcher: SelectorMatcher | undefined, // eslint-disable-next-line @typescript-eslint/no-explicit-any componentMeta: R3ComponentMetadata<any> ) { const injector = Injector.create({ parent: this.injector, providers: [ { provide: ComponentCompilerService }, { provide: COMPONENT_META, useValue: componentMeta }, { provide: ComponentContext, useFactory: () => { return new ComponentContext(directiveMatcher); }, }, ], }); const instance = injector.get(ComponentCompilerService); return instance.compile(); } private resolveStyleUrl(componentPath: string, styleUrl: string) { return path.normalize(path.resolve(path.dirname(componentPath), styleUrl)); } getDirectiveMap() { return this.directiveMap; } getComponentMap() { return this.componentMap; } private getLibraryDirectiveMeta( classDeclaration: ts.ClassDeclaration ): DirectiveMetaFromLibrary | undefined { let listeners: string[] = []; let properties: string[] = []; const directiveName = classDeclaration.name!.getText(); const selector = createCssSelectorForTs(classDeclaration.getSourceFile()); const listenersNode = selector.queryOne( `VariableDeclaration[name=${directiveName}_${LIBRARY_DIRECTIVE_LISTENERS_SUFFIX}]` ) as ts.VariableDeclaration; Eif (listenersNode) { listeners = literalResolve(listenersNode.type!.getText()); } const propertiesNode = selector.queryOne( `VariableDeclaration[name=${directiveName}_${LIBRARY_DIRECTIVE_PROPERTIES_SUFFIX}]` ) as ts.VariableDeclaration; Eif (propertiesNode) { properties = literalResolve(propertiesNode.type!.getText()); } return { isComponent: false, listeners: listeners, properties: properties, }; } private getLibraryComponentMeta( classDeclaration: ts.ClassDeclaration ): ComponentMetaFromLibrary | undefined { const directiveName = classDeclaration.name!.getText(); const selector = createCssSelectorForTs(classDeclaration.getSourceFile()); const exportPathNode = selector.queryOne( `VariableDeclaration[name=${directiveName}_${LIBRARY_COMPONENT_OUTPUT_PATH_SUFFIX}]` ) as ts.VariableDeclaration; if (!exportPathNode) { return undefined; } const exportPath = exportPathNode.type!.getText(); return { exportPath: literalResolve(exportPath), ...this.getLibraryDirectiveMeta(classDeclaration)!, isComponent: true, }; } } |