Skip to content

Commit

Permalink
feat: support rspack native plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Jan 17, 2025
1 parent 162c03a commit a556f69
Show file tree
Hide file tree
Showing 30 changed files with 872 additions and 260 deletions.
3 changes: 3 additions & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
"playwright": "1.33.0",
"typescript": "^5.2.2",
"webpack": "^5.95.0"
},
"overrides": {
"@rspack/core": "npm:@rspack-canary/[email protected]"
}
}
3 changes: 3 additions & 0 deletions examples/rspack-banner-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"overrides": {
"@rspack/core": "npm:@rspack-canary/[email protected]"
}
}
3 changes: 3 additions & 0 deletions examples/rspack-layers-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"overrides": {
"@rspack/core": "npm:@rspack-canary/[email protected]"
}
}
3 changes: 3 additions & 0 deletions examples/rspack-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"overrides": {
"@rspack/core": "npm:@rspack-canary/[email protected]"
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"test": "cross-env NODE_OPTIONS=--max-old-space-size=8192 pnpm run ut",
"test:all": "pnpm run test && pnpm run e2e",
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
"ut": "vitest run",
"ut": "vitest run --poolOptions.threads.singleThread",
"ut:watch": "vitest"
},
"nano-staged": {
Expand Down Expand Up @@ -50,7 +50,8 @@
},
"pnpm": {
"overrides": {
"react-dom": "18.3.1"
"react-dom": "18.3.1",
"@rspack/core": "npm:@rspack-canary/[email protected]"
}
},
"packageManager": "[email protected]",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/build-utils/build/chunks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './assetsModules';
export * from './chunkTransform';
export * from './generateTileGraph';
export * from './rspack/transform';
94 changes: 94 additions & 0 deletions packages/core/src/build-utils/build/chunks/rspack/transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Asset, Chunk, EntryPoint } from '@rsdoctor/graph';
import { Plugin, SDK } from '@rsdoctor/types';

/**
* Patch native chunk graph data from Rspack into ChunkGraph instance
* @param cg The ChunkGraph instance to be patched
* @param rawChunkGraph Raw chunk graph data from Rspack native plugin
*/
export function patchNativeChunkGraph(
cg: SDK.ChunkGraphInstance,
rawChunkGraph: Plugin.RspackNativeChunkGraph,
) {
const { chunks: rawChunks, entrypoints: rawEntrypoints } = rawChunkGraph;
/** set chunks */
const chunks = rawChunks.map(
(chunk) =>
new Chunk(
chunk.ukey.toString(),
chunk.name,
0,
chunk.initial,
chunk.entry,
),
);
cg.setChunks(chunks);
/** set entrypoints */
const entrypoints = rawEntrypoints.map((entrypoint) => {
const res = new EntryPoint(entrypoint.name);
res.setId(entrypoint.ukey);
return res;
});
cg.setEntrypoints(entrypoints);
/** set chunk dependencies */
for (const rawChunk of rawChunks) {
const chunk = cg.getChunkById(rawChunk.ukey.toString())!;
chunk.setDependencies(
rawChunk.dependencies.map((ukey) => cg.getChunkById(ukey.toString())!),
);
chunk.setImported(
rawChunk.imported.map((ukey) => cg.getChunkById(ukey.toString())!),
);
}
/** set entrypoint dependencies */
for (const rawEntrypoint of rawEntrypoints) {
const entrypoint = cg.getEntryPointById(rawEntrypoint.ukey)!;
entrypoint.setChunks(
rawEntrypoint.chunks.map((ukey) => cg.getChunkById(ukey.toString())!),
);
}
}

/**
* Patch native assets data from Rspack into ChunkGraph instance
* @param cg The ChunkGraph instance to be patched
* @param rawAssetPatch Raw assets patch data from Rspack native plugin
*/
export function patchNativeAssets(
cg: SDK.ChunkGraphInstance,
rawAssetPatch: Plugin.RspackNativeAssetPatch,
) {
const {
assets: rawAssets,
chunkAssets: rawChunkAssets,
entrypointAssets: rawEntrypointAssets,
} = rawAssetPatch;

/** set assets */
const assets = rawAssets.map((asset) => {
const res = new Asset(asset.path, asset.size, [], '');
res.setId(asset.ukey);
return res;
});
cg.setAssets(assets);
/** set assets chunks */
for (const rawAsset of rawAssets) {
const asset = cg.getAssetById(rawAsset.ukey)!;
asset.setChunks(
rawAsset.chunks.map((ukey) => cg.getChunkById(ukey.toString())!),
);
}

/** set chunk assets */
for (const rawChunkAsset of rawChunkAssets) {
const chunk = cg.getChunkById(rawChunkAsset.chunk.toString())!;
chunk.setAssets(rawChunkAsset.assets.map((ukey) => cg.getAssetById(ukey)!));
}
/** set assets entrypoints */
for (const rawEntrypointAsset of rawEntrypointAssets) {
const entrypoint = cg.getEntryPointById(rawEntrypointAsset.entrypoint)!;
entrypoint.setAssets(
rawEntrypointAsset.assets.map((ukey) => cg.getAssetById(ukey)!),
);
}
}
1 change: 1 addition & 0 deletions packages/core/src/build-utils/build/module-graph/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './webpack/transform';
export * from './transform';
export * from './treeShaking';
export * from './rspack/transform';
173 changes: 173 additions & 0 deletions packages/core/src/build-utils/build/module-graph/rspack/transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { Dependency, Module } from '@rsdoctor/graph';
import { Plugin, SDK } from '@rsdoctor/types';

/**
* Create dependency kind from dependency type string
* @param type The dependency type string from Rspack
* @returns The normalized dependency kind
*/
const createDependencyKind = (type: string) => {
if (type.includes('harmony')) {
return SDK.DependencyKind.ImportStatement;
}
if (type.includes('cjs')) {
return SDK.DependencyKind.RequireCall;
}
if (type.includes('import()')) {
return SDK.DependencyKind.DynamicImport;
}
if (type.includes('amd')) {
return SDK.DependencyKind.AMDRequire;
}
return SDK.DependencyKind.Unknown;
};

/**
* Patch native module graph data from Rspack into ModuleGraph instance
* @param mg The ModuleGraph instance to be patched
* @param cg The ChunkGraph instance to be patched
* @param rawModuleGraph Raw module graph data from Rspack native plugin
*/
export function patchNativeModuleGraph(
mg: SDK.ModuleGraphInstance,
cg: SDK.ChunkGraphInstance,
rawModuleGraph: Plugin.RspackNativeModuleGraph,
) {
const {
modules: rawModules,
dependencies: rawDependencies,
chunkModules: rawChunkModules,
} = rawModuleGraph;
/** set modules */
const modules = rawModules.map((module) => {
const res = new Module(
module.identifier,
module.path,
module.isEntry,
module.kind === 'concatenated'
? SDK.ModuleKind.Concatenation
: SDK.ModuleKind.Normal,
module.layer,
);
res.setId(module.ukey);
return res;
});
mg.setModules(modules);
/** set module imported */
for (const rawModule of rawModules) {
const module = mg.getModuleById(rawModule.ukey);
if (module) {
module.setImported(
rawModule.imported
.map((ukey) => mg.getModuleById(ukey)!)
.filter(Boolean),
);
}
}
/** set module concatenated children modules */
for (const rawModule of rawModules) {
const module = mg.getModuleById(rawModule.ukey)!;
module.setModules(
rawModule.modules.map((ukey) => mg.getModuleById(ukey)!).filter(Boolean),
);
}
/** set module concatenated parent modules */
for (const rawModule of rawModules) {
const module = mg.getModuleById(rawModule.ukey);
if (module) {
module.setConcatenationModules(
rawModule.belongModules
.map((ukey) => mg.getModuleById(ukey)!)
.filter(Boolean),
);
}
}
/** set module chunks */
for (const rawModule of rawModules) {
const module = mg.getModuleById(rawModule.ukey);
if (module) {
module.setChunks(
rawModule.chunks
.map((ukey) => cg.getChunkById(ukey.toString())!)
.filter(Boolean),
);
}
}
/** set chunk modules */
for (const rawChunkModule of rawChunkModules) {
const chunk = cg.getChunkById(rawChunkModule.chunk.toString());
if (chunk) {
chunk.setModules(
rawChunkModule.modules
.map((ukey) => mg.getModuleById(ukey)!)
.filter(Boolean),
);
}
}
/** set dependencies */
const deppendencies = rawDependencies.map((dep) => {
const res = new Dependency(
dep.request,
mg.getModuleById(dep.module)!,
mg.getModuleById(dep.dependency)!,
createDependencyKind(dep.kind),
);
res.setId(dep.ukey);
return res;
});
mg.setDependencies(deppendencies);

/** set module dependencies */
for (const rawModule of rawModules) {
const module = mg.getModuleById(rawModule.ukey)!;
module.setDependencies(
rawModule.dependencies
.map((ukey) => mg.getDependencyById(ukey)!)
.filter(Boolean),
);
}
}

/**
* Patch native ids data from Rspack into ModuleGraph instance
* @param mg The ModuleGraph instance to be patched
* @param rawModuleIdsPatch Raw ids patch data from Rspack native plugin
*/
export function patchNativeModuleIds(
mg: SDK.ModuleGraphInstance,
rawModuleIdsPatch: Plugin.RspackNativeModuleIdsPatch,
) {
const { moduleIds: rawModuleIds } = rawModuleIdsPatch;
/** set module ids */
for (const rawModuleId of rawModuleIds) {
const module = mg.getModuleById(rawModuleId.module);
if (module) {
module.setRenderId(rawModuleId.renderId);
}
}
}

/**
* Patch native sources data from Rspack into ModuleGraph instance
* @param mg The ModuleGraph instance to be patched
* @param rawModuleIdsPatch Raw sources patch data from Rspack native plugin
*/
export function patchNativeModuleSources(
mg: SDK.ModuleGraphInstance,
rawModuleSourcesPatch: Plugin.RspackNativeModuleSourcePatch,
) {
const { moduleOriginalSources: rawModuleOriginalSources } =
rawModuleSourcesPatch;
/** set module original sources */
for (const rawModuleOriginalSource of rawModuleOriginalSources) {
const module = mg.getModuleById(rawModuleOriginalSource.module);
if (module) {
module.setSource({
source: rawModuleOriginalSource.source,
});
module.setSize({
sourceSize: rawModuleOriginalSource.size,
});
}
}
}
Loading

0 comments on commit a556f69

Please sign in to comment.