-
Notifications
You must be signed in to change notification settings - Fork 47
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
LingyuCoder
wants to merge
1
commit into
main
Choose a base branch
from
feat/rspack-native-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": { | ||
|
@@ -50,7 +50,8 @@ | |
}, | ||
"pnpm": { | ||
"overrides": { | ||
"react-dom": "18.3.1" | ||
"react-dom": "18.3.1", | ||
"@rspack/core": "npm:@rspack-canary/[email protected]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be removed after publishing @rspack/[email protected] |
||
} | ||
}, | ||
"packageManager": "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
94
packages/core/src/build-utils/build/chunks/rspack/transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)!), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
173
packages/core/src/build-utils/build/module-graph/rspack/transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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