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 | 1x 1x 1x 1x 1x 1x 1x 20x 20x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 181x 181x 171x 10x 9x 10x 10x 9x 9x 9x 18x 18x 9x 2x 9x 9x 10x 10x 10x 10x 10x 10x 10x 221x 221x 20x 20x 20x 20x 20x 20x 221x 221x 221x 221x 221x 221x 221x 221x 221x 221x 20x | import type { BuilderContext } from '@angular-devkit/architect'; import type { AssetPattern } from '@angular-devkit/build-angular'; import { normalizeAssetPatterns } from '@angular-devkit/build-angular/src/utils'; import { Path, getSystemPath, normalize, resolve } from '@angular-devkit/core'; import * as glob from 'glob'; import * as path from 'path'; import { BehaviorSubject, firstValueFrom } from 'rxjs'; import { filter, take } from 'rxjs/operators'; import { Injectable } from 'static-injector'; import * as webpack from 'webpack'; import { BuildPlatform } from '../../platform/platform'; import type { PagePattern } from '../type'; function globAsync(pattern: string, options: glob.IOptions) { return new Promise<string[]>((resolve, reject) => glob.default(pattern, options, (e, m) => (e ? reject(e) : resolve(m))) ); } @Injectable() export class DynamicWatchEntryPlugin { pageList!: PagePattern[]; componentList!: PagePattern[]; entryPattern$ = new BehaviorSubject< | { pageList: PagePattern[]; componentList: PagePattern[]; } | undefined >(undefined); private first = true; absoluteProjectRoot!: Path; absoluteProjectSourceRoot!: Path; constructor( private options: { pages: AssetPattern[]; components: AssetPattern[]; workspaceRoot: Path; context: BuilderContext; config: webpack.Configuration; }, private buildPlatform: BuildPlatform ) {} async init() { const projectName = this.options.context.target && this.options.context.target.project; Iif (!projectName) { throw new Error('The builder requires a target.'); } const projectMetadata = await this.options.context.getProjectMetadata( projectName ); this.absoluteProjectRoot = normalize( getSystemPath( resolve( this.options.workspaceRoot, normalize((projectMetadata.root as string) || '') ) ) ); const relativeSourceRoot = projectMetadata.sourceRoot as string | undefined; const absoluteSourceRootPath = typeof relativeSourceRoot === 'string' ? resolve(this.options.workspaceRoot, normalize(relativeSourceRoot)) : undefined; Eif (relativeSourceRoot) { this.absoluteProjectSourceRoot = normalize( getSystemPath(absoluteSourceRootPath!) )!; } } apply(compiler: webpack.Compiler) { let rootCompilation: boolean = false; compiler.hooks.beforeCompile.tapPromise( 'DynamicWatchEntryPlugin', async (compilationParams) => { if (rootCompilation) { return; } this.entryPattern$.next({ pageList: await this.generateModuleInfo( this.options.pages || [], 'page' ), componentList: await this.generateModuleInfo( this.options.components || [], 'component' ), }); } ); compiler.hooks.thisCompilation.tap( 'DynamicWatchEntryPlugin', (compilation) => { rootCompilation = true; if (this.first) { this.first = false; const patternList = normalizeAssetPatterns( [...(this.options.pages || []), ...(this.options.components || [])], this.options.workspaceRoot, this.absoluteProjectRoot, this.absoluteProjectSourceRoot ); for (const pattern of patternList) { const cwd = path.resolve( this.options.context.workspaceRoot, pattern.input ); compilation.fileDependencies.add(cwd); } } } ); // 因为监听更新的时候beforeCompile会拦截所有的,所以这么实现(因为还有一次性构建不触发watchRun,所以不能代替) compiler.hooks.watchRun.tap('DynamicWatchEntryPlugin', async () => { rootCompilation = false; }); // 入口移动到这里是因为ng新增了一个插件也同时修改了入口, compiler.hooks.environment.tap( { name: `DynamicWatchEntryPlugin`, stage: 9999 }, () => { const originEntryConfig = compiler.options.entry; compiler.options.entry = async () => { await firstValueFrom( this.entryPattern$.pipe(filter(Boolean), take(1)) ); const entryPattern = this.entryPattern$.value!; const list = [ ...entryPattern.pageList, ...entryPattern.componentList, ]; const result = (await (typeof originEntryConfig === 'function' ? originEntryConfig() : originEntryConfig))!; Iif (result['app']) { throw new Error( '资源文件不能指定为app文件名或bundleName,请重新修改(不影响导出)' ); } return { ...result, ...list.reduce((pre, cur) => { pre[cur.entryName] = { import: [cur.src] }; return pre; }, {} as Record<string, Exclude<webpack.EntryNormalized, Function>[string]>), }; }; } ); } private async generateModuleInfo( list: AssetPattern[], type: 'page' | 'component' ) { const patternList = normalizeAssetPatterns( list, this.options.workspaceRoot, this.absoluteProjectRoot, this.absoluteProjectSourceRoot ); const moduleList: PagePattern[] = []; for (const pattern of patternList) { const cwd = path.resolve( this.options.context.workspaceRoot, pattern.input ); /** 当前匹配匹配到的文件 */ const files = await globAsync(pattern.glob, { cwd, dot: true, nodir: true, ignore: pattern.ignore || [], follow: pattern.followSymlinks, }); moduleList.push( ...files.map((file) => { const object: Partial<PagePattern> = { entryName: path.basename(file, '.ts').replace(/\./g, '-'), fileName: file, src: path.join(cwd, file), ...pattern, // eslint-disable-next-line @typescript-eslint/no-explicit-any outputFiles: {} as any, // eslint-disable-next-line @typescript-eslint/no-explicit-any inputFiles: {} as any, }; object.inputFiles!.config = object.src!.replace( /\.ts$/, this.buildPlatform.fileExtname.config! ); const outputFileName = object.fileName!.replace(/\.ts$/, '').replace(/\./g, '-') + '.ts'; object.outputFiles!.path = path .join(pattern.output, outputFileName) .replace(/\.ts$/, ''); object.outputFiles!.logic = object.outputFiles!.path + this.buildPlatform.fileExtname.logic; object.outputFiles!.style = object.outputFiles!.path + this.buildPlatform.fileExtname.style; object.outputFiles!.content = object.outputFiles!.path + this.buildPlatform.fileExtname.content; object.outputFiles!.config = object.outputFiles!.path + this.buildPlatform.fileExtname.config; object.type = type; return object as PagePattern; }) ); } return moduleList; } } |