Skip to content

Instantly share code, notes, and snippets.

@Schniz
Created August 5, 2021 19:13
Show Gist options
  • Save Schniz/aea63bb0459965bd82a1320aa99b9fe9 to your computer and use it in GitHub Desktop.
Save Schniz/aea63bb0459965bd82a1320aa99b9fe9 to your computer and use it in GitHub Desktop.
// @ts-check
require('path');
const esbuild = require('esbuild');
async function main() {
const {plugin, set} = createDependencyPlugin();
await esbuild.build({
bundle: true,
format: 'esm',
platform: "node",
plugins: [plugin],
logLevel: 'info',
entryPoints: [
__filename
]
})
console.log(set);
}
main();
/**
* @typedef {string} Dependency
*/
/**
* @returns {{ set: Map<string, Set<Dependency>>, plugin: import('esbuild').Plugin }}
*/
function createDependencyPlugin() {
/** @type {Map<string, Set<Dependency>>} */
const set = new Map();
return {
set, plugin: {
name: 'dependency-tracker',
setup(b) {
b.onResolve({filter: /.?/}, args => {
const key = args.importer ?? '__entry__'
const deps = set.get(key) ?? new Set();
deps.add(args.path);
set.set(key, deps);
return null;
})
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment