Skip to content

Instantly share code, notes, and snippets.

@lsdsjy
Last active May 9, 2023 08:31
Show Gist options
  • Save lsdsjy/cd0953e7cc997d93f5741e22475f7918 to your computer and use it in GitHub Desktop.
Save lsdsjy/cd0953e7cc997d93f5741e22475f7918 to your computer and use it in GitHub Desktop.
Print the dependency path of some module in Vite/Rollup
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