Skip to content

Commit

Permalink
feat: bench traverse-chunk-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 10, 2024
1 parent b6a59d3 commit 31b54c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions bench.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
"arco-pro_development-mode",
"arco-pro_development-mode_hmr",
"arco-pro_production-mode",
"arco-pro_production-mode_traverse-chunk-modules",
"arco-pro_production-mode_generate-package-json-webpack-plugin",
"threejs_development-mode_10x",
"threejs_development-mode_10x_hmr",
Expand Down
20 changes: 20 additions & 0 deletions lib/addons/traverse-chunk-modules/index.js
Original file line number Diff line number Diff line change
@@ -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 TraverseChunkGraphPlugin = require("${path.join(__dirname, 'plugin.cjs')}");
module.exports.plugins.push(new TraverseChunkGraphPlugin());
`;
}
}
28 changes: 28 additions & 0 deletions lib/addons/traverse-chunk-modules/plugin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const PLUGIN_NAME = "TraverseChunkModulesPlugin";

class TraverseChunkModulesPlugin {
apply(compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
compilation.hooks.processAssets.tap(PLUGIN_NAME, () => {
const visitedModules = new Set();

for (const chunk of compilation.chunks.values()) {
const modules = compilation.chunkGraph.getChunkModulesIterable(chunk);
for (const module of modules) {
if (modules in module) {
for (const subModule of module.modules) {
visitedModules.add(subModule);
}
} else {
visitedModules.add(module);
}
}
}

console.log('visited modules number', visitedModules.size);
});
});
}
}

module.exports = TraverseChunkModulesPlugin;

0 comments on commit 31b54c0

Please sign in to comment.