Skip to content

Commit

Permalink
fix: the module api error in rspack 1.2.0-alpha (#679)
Browse files Browse the repository at this point in the history
Co-authored-by: nyqykk <[email protected]>
  • Loading branch information
easy1090 and nyqykk authored Jan 7, 2025
1 parent ffcff67 commit afddeeb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getDeclarationIdentifier,
getExportIdentifierStatement,
} from './utils';
import { isWebpack5orRspack } from '@/build-utils/common/module-graph';
import { hasModuleGraphApi, isRspack } from '@/build-utils/common/module-graph';

type ExportData = Map<WebExportInfo, ExportInfo>;

Expand Down Expand Up @@ -161,7 +161,8 @@ export function appendTreeShaking(
moduleGraph: ModuleGraph,
compilation: Plugin.BaseCompilation,
) {
if (!isWebpack5orRspack(compilation)) {
// TODO: Rspack does not support tree shaking analysis for the time being. After subsequent verification.
if (!hasModuleGraphApi(compilation) || isRspack(compilation)) {
return moduleGraph;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getImportKind,
isImportDependency,
removeNoImportStyle,
isRspack,
} from '@/build-utils/common/module-graph';
import { hasSetEsModuleStatement } from '../parser';
import { isFunction } from 'lodash';
Expand Down Expand Up @@ -146,6 +147,7 @@ async function appendModuleData(
wbFs: WebpackFs,
features?: Plugin.RsdoctorWebpackPluginFeatures,
context?: TransformContext,
isRspack?: Boolean,
) {
const module = graph.getModuleByWebpackId(getWebpackModuleId(origin));

Expand Down Expand Up @@ -218,8 +220,8 @@ async function appendModuleData(
module.meta.strictHarmonyModule =
origin.buildMeta?.strictHarmonyModule ?? false;
module.meta.packageData = packageData;

if (!features?.lite && origin?.dependencies) {
// TODO: Rspack does not appendDependencies current. After subsequent verification.
if (!features?.lite && origin?.dependencies && !isRspack) {
// lite bundle Mode don't have dependency;
// Record dependent data.
Array.from(origin.dependencies)
Expand Down Expand Up @@ -261,6 +263,7 @@ export async function appendModuleGraphByCompilation(
fileSystemInfo,
features,
context,
isRspack(compilation),
);
}),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/build-utils/build/utils/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Common } from '@rsdoctor/types';
import { isWebpack5orRspack } from '@/build-utils/common/module-graph/compatible';
import { hasModuleGraphApi } from '@/build-utils/common/module-graph/compatible';
import { Plugin } from '@rsdoctor/types';

export type IHook =
Expand Down Expand Up @@ -49,7 +49,7 @@ export function interceptCompilationHooks(
* Compilation.hooks.normalModuleLoader is deprecated
* MIGRATION: Use NormalModule.getCompilationHooks(compilation).loader instead
*/
if (hook === 'normalModuleLoader' && isWebpack5orRspack(compilation)) {
if (hook === 'normalModuleLoader' && hasModuleGraphApi(compilation)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ export function getPositionByStatsLocation(
}
}

export function isWebpack5orRspack(
export function hasModuleGraphApi(
compilation: Plugin.BaseCompilation,
): Boolean {
return 'moduleGraph' in compilation && Boolean(compilation.moduleGraph);
}

export function isRspack(compilation: Plugin.BaseCompilation): Boolean {
return (
'rspackVersion' in compilation.compiler.webpack &&
Boolean(compilation.compiler.webpack.rspackVersion)
);
}
29 changes: 14 additions & 15 deletions packages/core/src/inner-plugins/plugins/ensureModulesChunkGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,21 @@ export const ensureModulesChunksGraphFn = (
'Rspack currently does not support treeShaking capabilities.',
),
);
return;
}

_this.modulesGraph =
ModuleGraphBuildUtils.appendTreeShaking(
_this.modulesGraph,
stats.compilation,
) || _this.modulesGraph;
_this.sdk.addClientRoutes([
Manifest.RsdoctorManifestClientRoutes.TreeShaking,
]);
} else {
_this.modulesGraph =
ModuleGraphBuildUtils.appendTreeShaking(
_this.modulesGraph,
stats.compilation,
) || _this.modulesGraph;
_this.sdk.addClientRoutes([
Manifest.RsdoctorManifestClientRoutes.TreeShaking,
]);

debug(
Process.getMemoryUsageMessage,
'[After AppendTreeShaking to ModuleGraph]',
);
debug(
Process.getMemoryUsageMessage,
'[After AppendTreeShaking to ModuleGraph]',
);
}
}

/** transform modules graph */
Expand Down

0 comments on commit afddeeb

Please sign in to comment.