|
| 1 | +import { Dependency, Module } from '@rsdoctor/graph'; |
| 2 | +import { Plugin, SDK } from '@rsdoctor/types'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Create dependency kind from dependency type string |
| 6 | + * @param type The dependency type string from Rspack |
| 7 | + * @returns The normalized dependency kind |
| 8 | + */ |
| 9 | +const createDependencyKind = (type: string) => { |
| 10 | + if (type.includes('harmony')) { |
| 11 | + return SDK.DependencyKind.ImportStatement; |
| 12 | + } |
| 13 | + if (type.includes('cjs')) { |
| 14 | + return SDK.DependencyKind.RequireCall; |
| 15 | + } |
| 16 | + if (type.includes('import()')) { |
| 17 | + return SDK.DependencyKind.DynamicImport; |
| 18 | + } |
| 19 | + if (type.includes('amd')) { |
| 20 | + return SDK.DependencyKind.AMDRequire; |
| 21 | + } |
| 22 | + return SDK.DependencyKind.Unknown; |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Patch native module graph data from Rspack into ModuleGraph instance |
| 27 | + * @param mg The ModuleGraph instance to be patched |
| 28 | + * @param cg The ChunkGraph instance to be patched |
| 29 | + * @param rawModuleGraph Raw module graph data from Rspack native plugin |
| 30 | + */ |
| 31 | +export function patchNativeModuleGraph( |
| 32 | + mg: SDK.ModuleGraphInstance, |
| 33 | + cg: SDK.ChunkGraphInstance, |
| 34 | + rawModuleGraph: Plugin.RspackNativeModuleGraph, |
| 35 | +) { |
| 36 | + const { |
| 37 | + modules: rawModules, |
| 38 | + dependencies: rawDependencies, |
| 39 | + chunkModules: rawChunkModules, |
| 40 | + } = rawModuleGraph; |
| 41 | + /** set modules */ |
| 42 | + const modules = rawModules.map((module) => { |
| 43 | + const res = new Module( |
| 44 | + module.identifier, |
| 45 | + module.path, |
| 46 | + module.isEntry, |
| 47 | + module.kind === 'concatenated' |
| 48 | + ? SDK.ModuleKind.Concatenation |
| 49 | + : SDK.ModuleKind.Normal, |
| 50 | + module.layer, |
| 51 | + ); |
| 52 | + res.setId(module.ukey); |
| 53 | + return res; |
| 54 | + }); |
| 55 | + mg.setModules(modules); |
| 56 | + /** set module imported */ |
| 57 | + for (const rawModule of rawModules) { |
| 58 | + const module = mg.getModuleById(rawModule.ukey); |
| 59 | + if (module) { |
| 60 | + module.setImported( |
| 61 | + rawModule.imported |
| 62 | + .map((ukey) => mg.getModuleById(ukey)!) |
| 63 | + .filter(Boolean), |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + /** set module concatenated children modules */ |
| 68 | + for (const rawModule of rawModules) { |
| 69 | + const module = mg.getModuleById(rawModule.ukey)!; |
| 70 | + module.setModules( |
| 71 | + rawModule.modules.map((ukey) => mg.getModuleById(ukey)!).filter(Boolean), |
| 72 | + ); |
| 73 | + } |
| 74 | + /** set module concatenated parent modules */ |
| 75 | + for (const rawModule of rawModules) { |
| 76 | + const module = mg.getModuleById(rawModule.ukey); |
| 77 | + if (module) { |
| 78 | + module.setConcatenationModules( |
| 79 | + rawModule.belongModules |
| 80 | + .map((ukey) => mg.getModuleById(ukey)!) |
| 81 | + .filter(Boolean), |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + /** set module chunks */ |
| 86 | + for (const rawModule of rawModules) { |
| 87 | + const module = mg.getModuleById(rawModule.ukey); |
| 88 | + if (module) { |
| 89 | + module.setChunks( |
| 90 | + rawModule.chunks |
| 91 | + .map((ukey) => cg.getChunkById(ukey.toString())!) |
| 92 | + .filter(Boolean), |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + /** set chunk modules */ |
| 97 | + for (const rawChunkModule of rawChunkModules) { |
| 98 | + const chunk = cg.getChunkById(rawChunkModule.chunk.toString()); |
| 99 | + if (chunk) { |
| 100 | + chunk.setModules( |
| 101 | + rawChunkModule.modules |
| 102 | + .map((ukey) => mg.getModuleById(ukey)!) |
| 103 | + .filter(Boolean), |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + /** set dependencies */ |
| 108 | + const deppendencies = rawDependencies.map((dep) => { |
| 109 | + const res = new Dependency( |
| 110 | + dep.request, |
| 111 | + mg.getModuleById(dep.module)!, |
| 112 | + mg.getModuleById(dep.dependency)!, |
| 113 | + createDependencyKind(dep.kind), |
| 114 | + ); |
| 115 | + res.setId(dep.ukey); |
| 116 | + return res; |
| 117 | + }); |
| 118 | + mg.setDependencies(deppendencies); |
| 119 | + |
| 120 | + /** set module dependencies */ |
| 121 | + for (const rawModule of rawModules) { |
| 122 | + const module = mg.getModuleById(rawModule.ukey)!; |
| 123 | + module.setDependencies( |
| 124 | + rawModule.dependencies |
| 125 | + .map((ukey) => mg.getDependencyById(ukey)!) |
| 126 | + .filter(Boolean), |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Patch native ids data from Rspack into ModuleGraph instance |
| 133 | + * @param mg The ModuleGraph instance to be patched |
| 134 | + * @param rawModuleIdsPatch Raw ids patch data from Rspack native plugin |
| 135 | + */ |
| 136 | +export function patchNativeModuleIds( |
| 137 | + mg: SDK.ModuleGraphInstance, |
| 138 | + rawModuleIdsPatch: Plugin.RspackNativeModuleIdsPatch, |
| 139 | +) { |
| 140 | + const { moduleIds: rawModuleIds } = rawModuleIdsPatch; |
| 141 | + /** set module ids */ |
| 142 | + for (const rawModuleId of rawModuleIds) { |
| 143 | + const module = mg.getModuleById(rawModuleId.module); |
| 144 | + if (module) { |
| 145 | + module.setRenderId(rawModuleId.renderId); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Patch native sources data from Rspack into ModuleGraph instance |
| 152 | + * @param mg The ModuleGraph instance to be patched |
| 153 | + * @param rawModuleIdsPatch Raw sources patch data from Rspack native plugin |
| 154 | + */ |
| 155 | +export function patchNativeModuleSources( |
| 156 | + mg: SDK.ModuleGraphInstance, |
| 157 | + rawModuleSourcesPatch: Plugin.RspackNativeModuleSourcePatch, |
| 158 | +) { |
| 159 | + const { moduleOriginalSources: rawModuleOriginalSources } = |
| 160 | + rawModuleSourcesPatch; |
| 161 | + /** set module original sources */ |
| 162 | + for (const rawModuleOriginalSource of rawModuleOriginalSources) { |
| 163 | + const module = mg.getModuleById(rawModuleOriginalSource.module); |
| 164 | + if (module) { |
| 165 | + module.setSource({ |
| 166 | + source: rawModuleOriginalSource.source, |
| 167 | + }); |
| 168 | + module.setSize({ |
| 169 | + sourceSize: rawModuleOriginalSource.size, |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments