Last active
May 9, 2023 08:31
-
-
Save lsdsjy/cd0953e7cc997d93f5741e22475f7918 to your computer and use it in GitHub Desktop.
Print the dependency path of some module in Vite/Rollup
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
module.exports = function plugin(options = {}) { | |
let exclude = (str) => options.exclude && str.match(options.exclude); | |
return { | |
name: 'print-dep-path', | |
generateBundle() { | |
/** | |
* @type {Map<string, string[]>} | |
*/ | |
const importersMap = new Map(); | |
const ids = []; | |
for (const moduleId of this.getModuleIds()) { | |
if (!exclude(moduleId)) { | |
ids.push(moduleId); | |
const { importers } = this.getModuleInfo(moduleId); | |
if (importers.length > 0) importersMap.set(moduleId, importers); | |
} | |
} | |
let importer = '/path/to/some/module'; | |
let result = [importer]; | |
while (importersMap.has(importer)) { | |
importer = importersMap.get(importer)[0]; | |
result.push(importer); | |
} | |
console.log(result.join(' <- ')); | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment