( context: RollupContext, program: string, inputs: string[], isTransformed: (id: string) => boolean, // a module the plugin transforms, as it reaches the plugin arrivalOf: (id: string) => string | Promise<string>, mappingsOf: (id: string) => string | undefined, scan: (program: ScannedProgram) => Promise<ScannedUsage>, cwd: string )
| 2517 | } |
| 2518 | |
| 2519 | const rollupScanOf = async ( |
| 2520 | context: RollupContext, |
| 2521 | program: string, |
| 2522 | inputs: string[], |
| 2523 | isTransformed: (id: string) => boolean, |
| 2524 | // a module the plugin transforms, as it reaches the plugin |
| 2525 | arrivalOf: (id: string) => string | Promise<string>, |
| 2526 | mappingsOf: (id: string) => string | undefined, |
| 2527 | scan: (program: ScannedProgram) => Promise<ScannedUsage>, |
| 2528 | cwd: string |
| 2529 | ): Promise<ProgramScan<BundleScan>> => { |
| 2530 | // each module's resolution, as a plugin may resolve it with options to load it by |
| 2531 | const res = new Map<string, RollupResolution>() |
| 2532 | const entries = await rollupEntriesOf(context, inputs, res, cwd) |
| 2533 | if (typeof entries === "string") |
| 2534 | return { warnings: [writeUnprunedMessage(entries)] } |
| 2535 | // modules read as they reached the plugin, having no file |
| 2536 | const arrived = new Set<string>() |
| 2537 | const scanned = await scanProgram(program, entries, { |
| 2538 | resolve: async (specifier, importer) => { |
| 2539 | const resolved = await bundledResolutionOf( |
| 2540 | context, |
| 2541 | specifier, |
| 2542 | importer, |
| 2543 | isTransformed |
| 2544 | ) |
| 2545 | if (resolved) res.set(resolved.id, resolved) |
| 2546 | return resolved && resolved.id |
| 2547 | }, |
| 2548 | load: async id => { |
| 2549 | const loaded = () => |
| 2550 | context.load({ ...res.get(id), id }).then( |
| 2551 | info => info?.code ?? undefined, |
| 2552 | () => undefined |
| 2553 | ) |
| 2554 | if (!isTransformed(id)) return loaded() |
| 2555 | // read from its file whether or not it has reached the plugin, so |
| 2556 | // transform order can't change the scan or the bundle |
| 2557 | const file = fileOf(id) |
| 2558 | const source = readFile(file) |
| 2559 | if (source !== undefined) return source |
| 2560 | // a virtual module written as a script, like Nuxt's, has no file, |
| 2561 | // so its plugin loads it, raced against its arrival |
| 2562 | arrived.add(file) |
| 2563 | return Promise.race([arrivalOf(file), loaded()]) |
| 2564 | }, |
| 2565 | readFile, |
| 2566 | isTransformed, |
| 2567 | idOf: fileOf, |
| 2568 | cwd |
| 2569 | }) |
| 2570 | if (typeof scanned === "string") |
| 2571 | return { warnings: [writeUnprunedMessage(scanned)] } |
| 2572 | for (const id of arrived) |
| 2573 | if (scanned.modules[id]) scanned.modules[id].mappings = mappingsOf(id) |
| 2574 | |
| 2575 | const usage = await scan(scanned) |
| 2576 | return { |
no test coverage detected