| 77 | |
| 78 | // reads the modules its entries reach a layer at a time, yielding what to load or resolve |
| 79 | function* programSteps( |
| 80 | key: string, |
| 81 | entries: ScanEntry[], |
| 82 | host: Omit<ScanHost<unknown>, "resolve"> |
| 83 | ): Generator< |
| 84 | ScanStep, |
| 85 | ScannedProgram | string, |
| 86 | (string | undefined)[] | ScanResolution[] |
| 87 | > { |
| 88 | const pathOf = (id: string) => |
| 89 | isAbsolute(id) ? printablePathOf(host.cwd, id) : id.replace(/^\0/, "") |
| 90 | const modules: Record<string, ScannedModule> = {} |
| 91 | const targets: Record<string, Record<string, string>> = {} |
| 92 | const arkImports: Record<string, string[]> = {} |
| 93 | // the bundler's id for each module, which it resolves from |
| 94 | const hostIds = new Map<string, string>() |
| 95 | let layer: string[] = [] |
| 96 | const add = (hostId: string) => { |
| 97 | const id = host.idOf(hostId) |
| 98 | if (!hostIds.has(id)) { |
| 99 | hostIds.set(id, hostId) |
| 100 | layer.push(id) |
| 101 | } |
| 102 | return id |
| 103 | } |
| 104 | const given = new Map(entries.map(entry => [add(entry.id), entry.source])) |
| 105 | // a module imported dynamically is a chunk of its own where a bundler splits |
| 106 | const dynamicImports = new Set<string>() |
| 107 | const exposed = new Set<string>() |
| 108 | // a dependency importing a module of the program may write it into a chunk of its own |
| 109 | let isImportedByDependency = false |
| 110 | while (layer.length) { |
| 111 | const read = layer.map(id => { |
| 112 | const hostId = hostIds.get(id)! |
| 113 | const isDependency = isLeftDependency(hostId, host) |
| 114 | return { |
| 115 | id, |
| 116 | hostId, |
| 117 | isDependency, |
| 118 | // a dependency's script is read from its file, for its imports alone |
| 119 | isLoaded: |
| 120 | given.get(id) === undefined && |
| 121 | !!host.load && |
| 122 | !(isDependency && transformedId.test(hostId)) |
| 123 | } |
| 124 | }) |
| 125 | layer = [] |
| 126 | const loading = read.filter(module => module.isLoaded) |
| 127 | const loaded = |
| 128 | loading.length ? |
| 129 | ((yield ["load", loading.map(module => module.hostId)]) as ( |
| 130 | | string |
| 131 | | undefined |
| 132 | )[]) |
| 133 | : [] |
| 134 | let loadedIndex = 0 |
| 135 | const reads = read.map(module => { |
| 136 | const source = |