Last active
January 14, 2022 11:39
-
-
Save OliverJAsh/fb974e5e45714e917ebc9dbf04395269 to your computer and use it in GitHub Desktop.
webpack: list reasons why a given module was included (dependents)
This file contains hidden or 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
// @ts-check | |
const treeify = require('treeify'); | |
/** | |
* @param {import('webpack').StatsCompilation} stats | |
* @param {string | number} id | |
*/ | |
const getModuleById = (stats, id) => stats.modules.find((module) => module.id === id); | |
/** | |
* @param {import('webpack').StatsCompilation} stats | |
* @param {string | number} id | |
*/ | |
const createTree = (stats, id) => { | |
const module = getModuleById(stats, id); | |
if (module === undefined) { | |
throw new Error('Module not found') | |
} else { | |
return { | |
[id]: module.reasons | |
.map((reason) => (reason.moduleId !== null ? createTree(stats, reason.moduleId) : null)) | |
.reduce((acc, object) => ({ ...acc, ...object }), {}), | |
}; | |
} | |
}; | |
// | |
// Example | |
// | |
const testId = './client/uploader/state/FormFile/epic.ts'; | |
const stats = /** @type {import('webpack').StatsCompilation} */ (require('./stats.json')); | |
console.log(treeify.asTree(createTree(stats, testId))); | |
/* | |
└─ ./client/uploader/state/FormFile/epic.ts | |
└─ ./client/uploader/state/FormStage/epic.ts | |
└─ ./client/uploader/state/UploaderReducerState/epic.ts | |
└─ ./client/uploader/components/Uploader/Uploader.tsx | |
└─ ./client/uploader/components/Uploader/index.ts | |
└─ ./client/uploader/index.ts | |
└─ ./app/components/MainRouteContainer/components/UploaderContainer/UploaderContainer.tsx | |
└─ ./app/components/MainRouteContainer/components/UploaderContainer/index.ts | |
└─ ./app/components/MainRouteContainer/MainRouteContainer.tsx | |
└─ ./app/components/MainRouteContainer/index.ts | |
└─ ./app/components/App/App.tsx | |
└─ ./app/components/App/index.ts | |
└─ ./server/components/App/App.tsx | |
└─ ./server/components/App/index.ts | |
└─ ./server/middlewares/handle-route-middleware.tsx | |
└─ ./server/index.ts | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment