| 24 | } |
| 25 | |
| 26 | function checkBundle( |
| 27 | bundlePath: string, |
| 28 | declared: Set<string>, |
| 29 | errors: string[] |
| 30 | ): void { |
| 31 | const stat = statSync(bundlePath, {throwIfNoEntry: false}) |
| 32 | if (!stat) return |
| 33 | if (stat.isFile()) { |
| 34 | if (!bundlePath.endsWith('.js')) return |
| 35 | const content = readFileSync(bundlePath, 'utf8') |
| 36 | const result = parseSync(bundlePath, content, {sourceType: 'module'}) |
| 37 | const seen = new Set<string>() |
| 38 | const specs = [ |
| 39 | ...result.module.staticImports.map(i => i.moduleRequest.value), |
| 40 | ...result.module.staticExports.flatMap(e => |
| 41 | e.entries |
| 42 | .map(entry => entry.moduleRequest?.value) |
| 43 | .filter((v): v is string => Boolean(v)) |
| 44 | ), |
| 45 | ...result.module.dynamicImports.flatMap(i => { |
| 46 | const raw = content.slice(i.moduleRequest.start, i.moduleRequest.end) |
| 47 | const m = /^(['"])([^'"]+)\1$/.exec(raw) |
| 48 | return m ? [m[2]] : [] |
| 49 | }), |
| 50 | ] |
| 51 | for (const spec of specs) { |
| 52 | const bare = bareSpecifier(spec) |
| 53 | if (!bare || seen.has(bare)) continue |
| 54 | seen.add(bare) |
| 55 | if (!declared.has(bare)) { |
| 56 | errors.push( |
| 57 | `'${bare}' imported by ${bundlePath} but not declared in package.json (dependencies/peerDependencies)` |
| 58 | ) |
| 59 | } |
| 60 | } |
| 61 | return |
| 62 | } |
| 63 | if (stat.isDirectory()) { |
| 64 | for (const entry of readdirSync(bundlePath)) { |
| 65 | checkBundle(join(bundlePath, entry), declared, errors) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function main(args: Args): void { |
| 71 | if (!args.pkg) { |