Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support rspack native plugin #710

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the latest canary version of rspack may lead to Segmentation fault (core dumped), use single thread before this is fixed

"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]"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be removed after publishing @rspack/[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
Loading