(pkgPath: string)
| 176 | } |
| 177 | |
| 178 | export 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 | |
| 227 | function getResolveCacheKey(key: string, options: InternalResolveOptions) { |
| 228 | // cache key needs to include options which affect |
no test coverage detected