| 255 | // Nothing about it is a guess at what the bundle holds, so what it can't read |
| 256 | // is what no module of the program reads through a name it knows |
| 257 | export const programAsBuilt = ( |
| 258 | key: string, |
| 259 | entries: readonly BuiltModule[], |
| 260 | host: Pick<ScanHost, "isTransformed" | "cwd"> |
| 261 | ): ScannedProgram | string => { |
| 262 | const pathOf = (id: string) => printablePathOf(host.cwd, id) |
| 263 | const modules: Record<string, ScannedModule> = {} |
| 264 | const targets: Record<string, Record<string, string>> = {} |
| 265 | const arkImports: Record<string, string[]> = {} |
| 266 | const dynamicImports = new Set<string>() |
| 267 | const exposed = new Set<string>() |
| 268 | let isImportedByDependency = false |
| 269 | const isRead = (module: BuiltModule) => isReadBuilt(module, host) |
| 270 | // what each module's text imports, which a loader after the plugin may add to |
| 271 | const namedBy = new Map<string, Set<string>>() |
| 272 | const reached = new Set(entries) |
| 273 | const builtApart = new Set<string>() |
| 274 | for (const module of reached) { |
| 275 | if (isRead(module)) { |
| 276 | const { id, source, transformed, mappings } = module |
| 277 | if (source === undefined) return `${pathOf(id)} can't be read` |
| 278 | if (id in modules) builtApart.add(id) |
| 279 | if ( |
| 280 | source === null || |
| 281 | (id in modules && |
| 282 | (modules[id].source !== source || |
| 283 | modules[id].transformed !== transformed)) |
| 284 | ) |
| 285 | return `it built ${pathOf(id)} from two codes` |
| 286 | if (!(id in modules)) { |
| 287 | const { imports, unnamed } = importsOf(id, source) |
| 288 | if (unnamed?.reason === unparsed) |
| 289 | return `${positionIn(pathOf(id), source, unnamed.offset)} ${unparsed}` |
| 290 | namedBy.set(id, new Set(imports.map(([specifier]) => specifier))) |
| 291 | modules[id] = { |
| 292 | source, |
| 293 | transformed, |
| 294 | mappings, |
| 295 | // read again once the plugin wrote the program, for what the |
| 296 | // loaders after it add |
| 297 | ...(module.built !== undefined && |
| 298 | module.built !== source && { built: module.built }) |
| 299 | } |
| 300 | targets[id] = {} |
| 301 | } |
| 302 | } |
| 303 | for (const { |
| 304 | specifier, |
| 305 | named = specifier, |
| 306 | isDynamic, |
| 307 | target |
| 308 | } of module.imports) { |
| 309 | if (specifier !== undefined && runtimeSpecifier.test(specifier)) { |
| 310 | // a generated module's reads are as unknown as a dependency's |
| 311 | const imported = (arkImports[module.id ?? ""] ??= []) |
| 312 | if (!imported.includes(specifier)) imported.push(specifier) |
| 313 | continue |
| 314 | } |