This commit is contained in:
Alex
2023-12-30 16:24:54 -05:00
parent 4563047e01
commit 690b46fd2b
5 changed files with 626 additions and 522 deletions

View File

@ -8,27 +8,39 @@ import { dependencies, peerDependencies } from '../dist/package.json';
const deps = Object.keys({ ...dependencies, ...peerDependencies }).concat('eslint');
const builtIn = new Set(builtinModules.flatMap(module => [module, `node:${module}`]));
function findRequires(text: string) {
const list = Array.from(text.matchAll(/require\(["']([^"']+)["']\)/g))
.map(m => m[1])
.filter(
module =>
!(
builtIn.has(module) ||
module.startsWith('eslint/') ||
module.startsWith('typescript/')
),
);
return uniq(list);
}
const moduleMap = glob
.sync('dist/**/*.js')
.map(path => ({ key: path, value: findRequires(fs.readFileSync(path, 'utf8')) }));
const files = Object.fromEntries(
glob
.sync('dist/**/*.js')
.map(path => [
path,
uniq(
Array.from(fs.readFileSync(path, 'utf8').matchAll(/require\(["']([^"']+)["']\)/g))
.map(m => m[1])
.filter(
module =>
!(
builtIn.has(module) ||
deps.includes(module) ||
deps.some(dep => module.startsWith(`${dep}/`)) ||
module.startsWith('eslint/') ||
module.startsWith('typescript/')
),
),
moduleMap
.map(({ key, value }) => ({
key,
value: value.filter(
module =>
!(deps.includes(module) || deps.some(dep => module.startsWith(`${dep}/`))),
),
])
.filter(([, modules]) => modules.length > 0),
}))
.filter(({ value }) => value.length > 0)
.map(({ key, value }) => [key, value]),
);
const uselessDeps = Object.keys(dependencies).filter(
dep => !moduleMap.some(({ value }) => value.includes(dep)),
);
console.log(files);
console.log('Missing imports:', files);
console.log('Unused dependencies:', uselessDeps);