Skip to content

Commit 0acd4bc

Browse files
committed
feat: support rspack native plugin
1 parent 162c03a commit 0acd4bc

File tree

23 files changed

+851
-260
lines changed

23 files changed

+851
-260
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test": "cross-env NODE_OPTIONS=--max-old-space-size=8192 pnpm run ut",
2222
"test:all": "pnpm run test && pnpm run e2e",
2323
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
24-
"ut": "vitest run",
24+
"ut": "vitest run --poolOptions.threads.singleThread",
2525
"ut:watch": "vitest"
2626
},
2727
"nano-staged": {
@@ -50,7 +50,8 @@
5050
},
5151
"pnpm": {
5252
"overrides": {
53-
"react-dom": "18.3.1"
53+
"react-dom": "18.3.1",
54+
"@rspack/core": "npm:@rspack-canary/[email protected]"
5455
}
5556
},
5657
"packageManager": "[email protected]",
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './assetsModules';
22
export * from './chunkTransform';
33
export * from './generateTileGraph';
4+
export * from './rspack/transform';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { Asset, Chunk, EntryPoint } from '@rsdoctor/graph';
2+
import { Plugin, SDK } from '@rsdoctor/types';
3+
4+
/**
5+
* Patch native chunk graph data from Rspack into ChunkGraph instance
6+
* @param cg The ChunkGraph instance to be patched
7+
* @param rawChunkGraph Raw chunk graph data from Rspack native plugin
8+
*/
9+
export function patchNativeChunkGraph(
10+
cg: SDK.ChunkGraphInstance,
11+
rawChunkGraph: Plugin.RspackNativeChunkGraph,
12+
) {
13+
const { chunks: rawChunks, entrypoints: rawEntrypoints } = rawChunkGraph;
14+
/** set chunks */
15+
const chunks = rawChunks.map(
16+
(chunk) =>
17+
new Chunk(
18+
chunk.ukey.toString(),
19+
chunk.name,
20+
0,
21+
chunk.initial,
22+
chunk.entry,
23+
),
24+
);
25+
cg.setChunks(chunks);
26+
/** set entrypoints */
27+
const entrypoints = rawEntrypoints.map((entrypoint) => {
28+
const res = new EntryPoint(entrypoint.name);
29+
res.setId(entrypoint.ukey);
30+
return res;
31+
});
32+
cg.setEntrypoints(entrypoints);
33+
/** set chunk dependencies */
34+
for (const rawChunk of rawChunks) {
35+
const chunk = cg.getChunkById(rawChunk.ukey.toString())!;
36+
chunk.setDependencies(
37+
rawChunk.dependencies.map((ukey) => cg.getChunkById(ukey.toString())!),
38+
);
39+
chunk.setImported(
40+
rawChunk.imported.map((ukey) => cg.getChunkById(ukey.toString())!),
41+
);
42+
}
43+
/** set entrypoint dependencies */
44+
for (const rawEntrypoint of rawEntrypoints) {
45+
const entrypoint = cg.getEntryPointById(rawEntrypoint.ukey)!;
46+
entrypoint.setChunks(
47+
rawEntrypoint.chunks.map((ukey) => cg.getChunkById(ukey.toString())!),
48+
);
49+
}
50+
}
51+
52+
/**
53+
* Patch native assets data from Rspack into ChunkGraph instance
54+
* @param cg The ChunkGraph instance to be patched
55+
* @param rawAssetPatch Raw assets patch data from Rspack native plugin
56+
*/
57+
export function patchNativeAssets(
58+
cg: SDK.ChunkGraphInstance,
59+
rawAssetPatch: Plugin.RspackNativeAssetPatch,
60+
) {
61+
const {
62+
assets: rawAssets,
63+
chunkAssets: rawChunkAssets,
64+
entrypointAssets: rawEntrypointAssets,
65+
} = rawAssetPatch;
66+
67+
/** set assets */
68+
const assets = rawAssets.map((asset) => {
69+
const res = new Asset(asset.path, asset.size, [], '');
70+
res.setId(asset.ukey);
71+
return res;
72+
});
73+
cg.setAssets(assets);
74+
/** set assets chunks */
75+
for (const rawAsset of rawAssets) {
76+
const asset = cg.getAssetById(rawAsset.ukey)!;
77+
asset.setChunks(
78+
rawAsset.chunks.map((ukey) => cg.getChunkById(ukey.toString())!),
79+
);
80+
}
81+
82+
/** set chunk assets */
83+
for (const rawChunkAsset of rawChunkAssets) {
84+
const chunk = cg.getChunkById(rawChunkAsset.chunk.toString())!;
85+
chunk.setAssets(rawChunkAsset.assets.map((ukey) => cg.getAssetById(ukey)!));
86+
}
87+
/** set assets entrypoints */
88+
for (const rawEntrypointAsset of rawEntrypointAssets) {
89+
const entrypoint = cg.getEntryPointById(rawEntrypointAsset.entrypoint)!;
90+
entrypoint.setAssets(
91+
rawEntrypointAsset.assets.map((ukey) => cg.getAssetById(ukey)!),
92+
);
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './webpack/transform';
22
export * from './transform';
33
export * from './treeShaking';
4+
export * from './rspack/transform';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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

Comments
 (0)