UNPKG

@storm-software/esbuild

Version:

A package containing `esbuild` utilities for building Storm Software libraries and applications

95 lines (93 loc) 2.95 kB
import { writeError, writeWarning } from "../chunk-CKFE6AY5.js"; import { __require, init_esm_shims } from "../chunk-5OCVL4NC.js"; // src/plugins/deps-check.ts init_esm_shims(); import { builtinModules } from "node:module"; import path from "node:path"; var unusedIgnore = [ // these are our dev dependencies /@types\/.*?/, /@typescript-eslint.*?/, /eslint.*?/, "esbuild", "husky", "is-ci", "lint-staged", "prettier", "typescript", "ts-node", "ts-jest", "@swc/core", "@swc/jest", "jest", // these are missing 3rd party deps "spdx-exceptions", "spdx-license-ids", // type-only, so it is not detected "ts-toolbelt", // these are indirectly used by build "buffer" ]; var missingIgnore = [".prisma", "@prisma/client", "ts-toolbelt"]; var depsCheckPlugin = (bundle) => ({ name: "storm:deps-check", setup(build) { const pkgJsonPath = path.join(process.cwd(), "package.json"); const pkgContents = __require(pkgJsonPath); 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 = /* @__PURE__ */ new Set(); 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); }); writeWarning( `Unused Dependencies: ${JSON.stringify(filteredUnusedDeps)}` ); writeError( `Missing Dependencies: ${JSON.stringify(filteredMissingDeps)}` ); if (filteredMissingDeps.length > 0) { throw new Error(`Missing dependencies detected - please install them: ${JSON.stringify(filteredMissingDeps)} `); } }); } }); export { depsCheckPlugin };