/
validate.ts
61 lines (56 loc) 路 2.11 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { existsSync } from 'fs'
import chalk from 'chalk'
import { resolve } from 'pathe'
import { PackageJson } from 'pkg-types'
import { extractExportFilenames, getpkg, warn } from './utils'
import { BuildContext } from './types'
export function validateDependencies (ctx: BuildContext) {
const usedDependencies = new Set<string>()
const unusedDependencies = new Set<string>(Object.keys(ctx.pkg.dependencies || {}))
const implicitDependencies = new Set<string>()
for (const id of ctx.usedImports) {
unusedDependencies.delete(id)
usedDependencies.add(id)
}
if (Array.isArray(ctx.options.dependencies)) {
for (const id of ctx.options.dependencies) {
unusedDependencies.delete(id)
}
}
for (const id of usedDependencies) {
if (
!ctx.options.externals.includes(id) &&
!id.startsWith('chunks/') &&
!ctx.options.dependencies.includes(getpkg(id)) &&
!ctx.options.peerDependencies.includes(getpkg(id))
) {
implicitDependencies.add(id)
}
}
if (unusedDependencies.size) {
warn(ctx, 'Potential unused dependencies found: ' + Array.from(unusedDependencies).map(id => chalk.cyan(id)).join(', '))
}
if (implicitDependencies.size && !ctx.options.rollup.inlineDependencies) {
warn(ctx, 'Potential implicit dependencies found: ' + Array.from(implicitDependencies).map(id => chalk.cyan(id)).join(', '))
}
}
export function validatePackage (pkg: PackageJson, rootDir: string, ctx: BuildContext) {
if (!pkg) { return }
const filenames = new Set([
...typeof pkg.bin === 'string' ? [pkg.bin] : Object.values(pkg.bin || {}),
pkg.main,
pkg.module,
pkg.types,
pkg.typings,
...extractExportFilenames(pkg.exports).map(i => i.file)
].map(i => i && resolve(rootDir, i.replace(/\/[^/]*\*.*$/, ''))))
const missingOutputs = []
for (const filename of filenames) {
if (filename && !filename.includes('*') && !existsSync(filename)) {
missingOutputs.push(filename.replace(rootDir + '/', ''))
}
}
if (missingOutputs.length) {
warn(ctx, `Potential missing package.json files: ${missingOutputs.map(o => chalk.cyan(o)).join(', ')}`)
}
}