/
packages.ts
166 lines (153 loc) · 4.29 KB
/
packages.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import fs from 'fs'
import path from 'path'
import { createFilter } from '@rollup/pluginutils'
import { createDebugger, resolveFrom } from './utils'
import type { ResolvedConfig } from './config'
import type { Plugin } from './plugin'
const isDebug = process.env.DEBUG
const debug = createDebugger('vite:resolve-details', {
onlyWhenFocused: true
})
/** Cache for package.json resolution and package.json contents */
export type PackageCache = Map<string, PackageData>
export interface PackageData {
dir: string
hasSideEffects: (id: string) => boolean | 'no-treeshake'
webResolvedImports: Record<string, string | undefined>
nodeResolvedImports: Record<string, string | undefined>
setResolvedCache: (key: string, entry: string, targetWeb: boolean) => void
getResolvedCache: (key: string, targetWeb: boolean) => string | undefined
data: {
[field: string]: any
name: string
type: string
version: string
main: string
module: string
browser: string | Record<string, string | false>
exports: string | Record<string, any> | string[]
dependencies: Record<string, string>
}
}
export function invalidatePackageData(
packageCache: PackageCache,
pkgPath: string
): void {
packageCache.delete(pkgPath)
const pkgDir = path.dirname(pkgPath)
packageCache.forEach((pkg, cacheKey) => {
if (pkg.dir === pkgDir) {
packageCache.delete(cacheKey)
}
})
}
export function resolvePackageData(
id: string,
basedir: string,
preserveSymlinks = false,
packageCache?: PackageCache
): PackageData | null {
let pkg: PackageData | undefined
let cacheKey: string | undefined
if (packageCache) {
cacheKey = `${id}&${basedir}&${preserveSymlinks}`
if ((pkg = packageCache.get(cacheKey))) {
return pkg
}
}
let pkgPath: string | undefined
try {
pkgPath = resolveFrom(`${id}/package.json`, basedir, preserveSymlinks)
pkg = loadPackageData(pkgPath, true, packageCache)
if (packageCache) {
packageCache.set(cacheKey!, pkg)
}
return pkg
} catch (e) {
if (e instanceof SyntaxError) {
isDebug && debug(`Parsing failed: ${pkgPath}`)
}
// Ignore error for missing package.json
else if (e.code !== 'MODULE_NOT_FOUND') {
throw e
}
}
return null
}
export function loadPackageData(
pkgPath: string,
preserveSymlinks?: boolean,
packageCache?: PackageCache
): PackageData {
if (!preserveSymlinks) {
pkgPath = fs.realpathSync.native(pkgPath)
}
let cached: PackageData | undefined
if ((cached = packageCache?.get(pkgPath))) {
return cached
}
const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
const pkgDir = path.dirname(pkgPath)
const { sideEffects } = data
let hasSideEffects: (id: string) => boolean
if (typeof sideEffects === 'boolean') {
hasSideEffects = () => sideEffects
} else if (Array.isArray(sideEffects)) {
hasSideEffects = createFilter(sideEffects, null, { resolve: pkgDir })
} else {
hasSideEffects = () => true
}
const pkg: PackageData = {
dir: pkgDir,
data,
hasSideEffects,
webResolvedImports: {},
nodeResolvedImports: {},
setResolvedCache(key: string, entry: string, targetWeb: boolean) {
if (targetWeb) {
pkg.webResolvedImports[key] = entry
} else {
pkg.nodeResolvedImports[key] = entry
}
},
getResolvedCache(key: string, targetWeb: boolean) {
if (targetWeb) {
return pkg.webResolvedImports[key]
} else {
return pkg.nodeResolvedImports[key]
}
}
}
packageCache?.set(pkgPath, pkg)
return pkg
}
export function watchPackageDataPlugin(config: ResolvedConfig): Plugin {
const watchQueue = new Set<string>()
let watchFile = (id: string) => {
watchQueue.add(id)
}
const { packageCache } = config
const setPackageData = packageCache.set.bind(packageCache)
packageCache.set = (id, pkg) => {
if (id.endsWith('.json')) {
watchFile(id)
}
return setPackageData(id, pkg)
}
return {
name: 'vite:watch-package-data',
buildStart() {
watchFile = this.addWatchFile
watchQueue.forEach(watchFile)
watchQueue.clear()
},
buildEnd() {
watchFile = (id) => watchQueue.add(id)
},
watchChange(id) {
if (id.endsWith('/package.json')) {
invalidatePackageData(packageCache, id)
}
}
}
}