Просмотр исходного кода

feat: add plugin to log eager vs lazy module counts for client-side dev builds

Yuki Takei 1 месяц назад
Родитель
Сommit
0e313822cc
2 измененных файлов с 42 добавлено и 1 удалено
  1. 9 1
      apps/app/next.config.js
  2. 33 0
      apps/app/src/utils/next.config.utils.js

+ 9 - 1
apps/app/next.config.js

@@ -94,7 +94,7 @@ const optimizePackageImports = [
   '@growi/ui',
 ];
 
-module.exports = async (phase) => {
+module.exports = (phase) => {
   const { i18n, localePath } = require('./config/next-i18next.config');
 
   /** @type {import('next').NextConfig} */
@@ -152,6 +152,14 @@ module.exports = async (phase) => {
         config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
       }
 
+      // Log eager vs lazy module counts for dev compilation analysis
+      if (!options.isServer && options.dev) {
+        const {
+          createChunkModuleStatsPlugin,
+        } = require('./src/utils/next.config.utils');
+        config.plugins.push(createChunkModuleStatsPlugin());
+      }
+
       return config;
     },
   };

+ 33 - 0
apps/app/src/utils/next.config.utils.js

@@ -51,6 +51,39 @@ exports.listScopedPackages = (scopes, opts = defaultOpts) => {
 /**
  * @param prefixes {string[]}
  */
+/**
+ * Webpack plugin that logs eager (initial) vs lazy (async-only) module counts.
+ * Attach to client-side dev builds only.
+ */
+exports.createChunkModuleStatsPlugin = () => ({
+  apply(compiler) {
+    compiler.hooks.done.tap('ChunkModuleStatsPlugin', (stats) => {
+      const { compilation } = stats;
+      const initialModuleIds = new Set();
+      const asyncModuleIds = new Set();
+
+      for (const chunk of compilation.chunks) {
+        const target = chunk.canBeInitial() ? initialModuleIds : asyncModuleIds;
+        for (const module of compilation.chunkGraph.getChunkModulesIterable(
+          chunk,
+        )) {
+          target.add(module.identifier());
+        }
+      }
+
+      // Modules that appear ONLY in async chunks
+      const asyncOnlyCount = [...asyncModuleIds].filter(
+        (id) => !initialModuleIds.has(id),
+      ).length;
+
+      // biome-ignore lint/suspicious/noConsole: Dev-only module stats for compilation analysis
+      console.log(
+        `[ChunkModuleStats] initial: ${initialModuleIds.size}, async-only: ${asyncOnlyCount}, total: ${compilation.modules.size}`,
+      );
+    });
+  },
+});
+
 exports.listPrefixedPackages = (prefixes, opts = defaultOpts) => {
   /** @type {string[]} */
   const prefixedPackages = [];