import type * as esbuild from 'esbuild' ; import path from 'path' ; import { builtinModules } from 'module' ; const unusedIgnore = [ /@types\/.*?/ , /@typescript-eslint.*?/ , /eslint.*?/ , 'esbuild' , 'husky' , 'is-ci' , 'lint-staged' , 'prettier' , 'typescript' , 'ts-node' , 'ts-jest' , 'jest' , 'spdx-exceptions' , 'spdx-license-ids' , 'ts-toolbelt' , 'buffer' , 'eventemitter3' , 'path-browserify' , 'tty-browserify' , 'util' , ] ; const missingIgnore = [ '.prisma' , '@prisma/client' ] ; /**
 * Checks for unused and missing dependencies.
 */ export const depCheckPlugin = ( bundle ? ) : esbuild . Plugin => ( { name : 'depCheckPlugin' , setup ( build ) { const pkgJsonPath = path . join ( process . cwd ( ) , 'package.json' ) ; const pkgContents = require ( pkgJsonPath ) as Record < string , object > ; const regDependencies = Object . keys ( pkgContents [ 'dependencies' ] ?? { } ) ; const devDependencies = Object . keys ( pkgContents [ 'devDependencies' ] ?? { } ) ; const peerDependencies = Object . keys ( pkgContents [ 'peerDependencies' ] ?? { } ) ; const dependencies = [ ... regDependencies , ... ( bundle ? devDependencies : [ ] ) ] ; const collectedDependencies = new Set < string > ( ) ; const onlyPackages = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ ; build . onResolve ( { filter : onlyPackages } , ( args ) => { if ( args . importer . includes ( process . cwd ( ) ) ) { if ( args . path [ 0 ] == '@' ) { const [ org , pkg ] = args . path . split ( '/' ) ; collectedDependencies . add ( `${ org }/${ pkg }` ) ; } else { const [ pkg ] = args . path . split ( '/' ) ; collectedDependencies . add ( pkg ) ; } } return { external : true } ; } ) ; build . onEnd ( ( ) => { const unusedDependencies = [ ... dependencies ] . filter ( ( dep ) => { return ! collectedDependencies . has ( dep ) || builtinModules . includes ( dep ) ; } ) ; const missingDependencies = [ ... collectedDependencies ] . filter ( ( dep ) => { return ! dependencies . includes ( dep ) && ! builtinModules . includes ( dep ) ; } ) ; const filteredUnusedDeps = unusedDependencies . filter ( ( dep ) => { return ! unusedIgnore . some ( ( pattern ) => dep . match ( pattern ) ) ; } ) ; const filteredMissingDeps = missingDependencies . filter ( ( dep ) => { return ! missingIgnore . some ( ( pattern ) => dep . match ( pattern ) ) && ! peerDependencies . includes ( dep ) ; } ) ; console . warn ( 'unusedDependencies' , JSON . stringify ( filteredUnusedDeps ) ) ; console . warn ( 'missingDependencies' , JSON . stringify ( filteredMissingDeps ) ) ; if ( filteredMissingDeps . length > 0 ) process . exit ( 1 ) ; } ) ; } , } ) ;