diff --git a/bench.config.js b/bench.config.js index 1eb8bae2b..718a080f4 100644 --- a/bench.config.js +++ b/bench.config.js @@ -3,14 +3,15 @@ export default { "10000_development-mode", "10000_development-mode_hmr", "10000_production-mode", - "10000_big_production-mode_disable-minimize", + "10000_production-mode_traverse-module-graph", "arco-pro_development-mode", "arco-pro_development-mode_hmr", "arco-pro_production-mode", "arco-pro_production-mode_generate-package-json-webpack-plugin", "threejs_development-mode_10x", "threejs_development-mode_10x_hmr", - "threejs_production-mode_10x" + "threejs_production-mode_10x", + "10000_big_production-mode", ], rspackDirectory: process.env.RSPACK_DIR }; diff --git a/lib/addons/traverse-module-graph/index.js b/lib/addons/traverse-module-graph/index.js new file mode 100644 index 000000000..3e8cc1270 --- /dev/null +++ b/lib/addons/traverse-module-graph/index.js @@ -0,0 +1,20 @@ +import path from "path"; +import url from "url"; +import { Addon } from "../common.js"; + +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); + +export default class extends Addon { + options = { + times: 10 + }; + async afterSetup(ctx) { + ctx.config = + ctx.config + + ` +module.exports.plugins = module.exports.plugins || []; +const TraverseModuleGraphPlugin = require("${path.join(__dirname, 'plugin.cjs')}"); +module.exports.plugins.push(new TraverseModuleGraphPlugin()); +`; + } +} diff --git a/lib/addons/traverse-module-graph/plugin.cjs b/lib/addons/traverse-module-graph/plugin.cjs new file mode 100644 index 000000000..b266a5b61 --- /dev/null +++ b/lib/addons/traverse-module-graph/plugin.cjs @@ -0,0 +1,34 @@ +const PLUGIN_NAME = "TraverseModuleGraphPlugin"; + +class TraverseModuleGraphPlugin { + apply(compiler) { + compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => { + compilation.hooks.finishModules.tap(PLUGIN_NAME, () => { + const entries = compilation.entries.values(); + + const visitedModules = new Set(); + + function traverse(dependency) { + const module = compilation.moduleGraph.getModule(dependency); + if (module) { + if (visitedModules.has(module)) { + return; + } + visitedModules.add(module); + for (const dep of module.dependencies) { + traverse(dep) + } + } + } + + for (const entry of entries) { + for (const dependency of entry.dependencies) { + traverse(dependency) + } + } + }); + }); + } +} + +module.exports = TraverseModuleGraphPlugin;