Created
August 5, 2021 19:13
-
-
Save Schniz/aea63bb0459965bd82a1320aa99b9fe9 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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