MCPcopy
hub / github.com/vitejs/vite / loadPackageData

Function loadPackageData

packages/vite/src/node/packages.ts:178–225  ·  view source on GitHub ↗
(pkgPath: string)

Source from the content-addressed store, hash-verified

176}
177
178export function loadPackageData(pkgPath: string): PackageData {
179 const data = JSON.parse(stripBomTag(fs.readFileSync(pkgPath, 'utf-8')))
180 const pkgDir = normalizePath(path.dirname(pkgPath))
181 const { sideEffects } = data
182 let hasSideEffects: (id: string) => boolean | null
183 if (typeof sideEffects === 'boolean') {
184 hasSideEffects = () => sideEffects
185 } else if (Array.isArray(sideEffects)) {
186 if (sideEffects.length <= 0) {
187 // createFilter always returns true if `includes` is an empty array
188 // but here we want it to always return false
189 hasSideEffects = () => false
190 } else {
191 const finalPackageSideEffects = sideEffects.map((sideEffect) => {
192 /*
193 * The array accepts simple glob patterns to the relevant files... Patterns like *.css, which do not include a /, will be treated like **\/*.css.
194 * https://webpack.js.org/guides/tree-shaking/
195 * https://github.com/vitejs/vite/pull/11807
196 */
197 if (sideEffect.includes('/')) {
198 return sideEffect
199 }
200 return `**/${sideEffect}`
201 })
202
203 hasSideEffects = createFilter(finalPackageSideEffects, null, {
204 resolve: pkgDir,
205 })
206 }
207 } else {
208 hasSideEffects = () => null
209 }
210
211 const resolvedCache: Record<string, string | undefined> = {}
212 const pkg: PackageData = {
213 dir: pkgDir,
214 data,
215 hasSideEffects,
216 setResolvedCache(key, entry, options) {
217 resolvedCache[getResolveCacheKey(key, options)] = entry
218 },
219 getResolvedCache(key, options) {
220 return resolvedCache[getResolveCacheKey(key, options)]
221 },
222 }
223
224 return pkg
225}
226
227function getResolveCacheKey(key: string, options: InternalResolveOptions) {
228 // cache key needs to include options which affect

Callers 3

tryCleanFsResolveFunction · 0.90
resolvePackageDataFunction · 0.85
findNearestPackageDataFunction · 0.85

Calls 4

stripBomTagFunction · 0.90
normalizePathFunction · 0.90
createFilterFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected