|
| 1 | +import { Manifest, Plugin, SDK } from '@rsdoctor/types'; |
| 2 | +import { Time } from '@rsdoctor/utils/common'; |
| 3 | +import { InternalBasePlugin } from './base'; |
| 4 | +import type { Compiler as WebpackCompiler } from 'webpack'; |
| 5 | + |
| 6 | +export class InternalResolverPlugin< |
| 7 | + T extends Plugin.BaseCompiler, |
| 8 | +> extends InternalBasePlugin<T> { |
| 9 | + public readonly name = 'resolver'; |
| 10 | + |
| 11 | + protected resolveDataMap = new Map< |
| 12 | + string, |
| 13 | + { startAt: number; startHRTime: [number, number]; request: string } |
| 14 | + >(); |
| 15 | + |
| 16 | + public apply(compiler: T) { |
| 17 | + // resolver depends on module graph |
| 18 | + this.scheduler.ensureModulesChunksGraphApplied(compiler); |
| 19 | + compiler.hooks.normalModuleFactory.tap( |
| 20 | + this.tapPostOptions, |
| 21 | + this.handleNormalModuleFactory, |
| 22 | + ); |
| 23 | + |
| 24 | + // add resolver page to client |
| 25 | + this.sdk.addClientRoutes([ |
| 26 | + Manifest.RsdoctorManifestClientRoutes.ModuleResolve, |
| 27 | + ]); |
| 28 | + } |
| 29 | + |
| 30 | + protected handleNormalModuleFactory = ( |
| 31 | + normalModuleFactory: |
| 32 | + | Plugin.RspackNormalModuleFactory |
| 33 | + | ReturnType<WebpackCompiler['createNormalModuleFactory']>, |
| 34 | + ) => { |
| 35 | + // Hook into beforeResolve to capture the start time |
| 36 | + normalModuleFactory.hooks.beforeResolve.tap( |
| 37 | + this.tapPostOptions, |
| 38 | + (resolveData: any) => { |
| 39 | + if (!resolveData) return; |
| 40 | + |
| 41 | + const issuer = |
| 42 | + resolveData.contextInfo?.issuer || resolveData.context || ''; |
| 43 | + const request = resolveData.request; |
| 44 | + |
| 45 | + if (issuer && request) { |
| 46 | + const key = `${issuer}::${request}`; |
| 47 | + this.resolveDataMap.set(key, { |
| 48 | + startAt: Date.now(), |
| 49 | + startHRTime: process.hrtime(), |
| 50 | + request, |
| 51 | + }); |
| 52 | + } |
| 53 | + }, |
| 54 | + ); |
| 55 | + |
| 56 | + // Hook into afterResolve to capture the result and report |
| 57 | + normalModuleFactory.hooks.afterResolve.tap( |
| 58 | + this.tapPostOptions, |
| 59 | + (resolveData: any) => { |
| 60 | + if (!resolveData) return; |
| 61 | + |
| 62 | + const issuer = |
| 63 | + resolveData.contextInfo?.issuer || resolveData.context || ''; |
| 64 | + const request = resolveData.request; |
| 65 | + const result = |
| 66 | + resolveData.createData?.resource || |
| 67 | + resolveData.resourceResolveData?.path; |
| 68 | + |
| 69 | + if (issuer && request) { |
| 70 | + const key = `${issuer}::${request}`; |
| 71 | + const startData = this.resolveDataMap.get(key); |
| 72 | + |
| 73 | + if (startData) { |
| 74 | + const data: SDK.PathResolverSuccessData = { |
| 75 | + isEntry: Boolean(issuer), |
| 76 | + issuerPath: issuer, |
| 77 | + request: startData.request, |
| 78 | + startAt: startData.startAt, |
| 79 | + endAt: Time.getCurrentTimestamp( |
| 80 | + startData.startAt, |
| 81 | + startData.startHRTime, |
| 82 | + ), |
| 83 | + result: result || '', |
| 84 | + pid: process.pid, |
| 85 | + ppid: process.ppid, |
| 86 | + }; |
| 87 | + |
| 88 | + this.sdk.reportResolver([data]); |
| 89 | + this.resolveDataMap.delete(key); |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + ); |
| 94 | + }; |
| 95 | +} |
0 commit comments