MCPcopy Create free account
hub / github.com/arktypeio/arktype / programAsBuilt

Function programAsBuilt

ark/aot/scan.ts:257–360  ·  view source on GitHub ↗
(
	key: string,
	entries: readonly BuiltModule[],
	host: Pick<ScanHost, "isTransformed" | "cwd">
)

Source from the content-addressed store, hash-verified

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
257export 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 }

Callers 2

answerBuiltFunction · 0.90
readOfFunction · 0.90

Calls 15

flatMorphFunction · 0.90
isReadFunction · 0.85
positionInFunction · 0.85
sortedByKeyFunction · 0.85
testMethod · 0.80
filterMethod · 0.80
sortMethod · 0.80
pathOfFunction · 0.70
importsOfFunction · 0.70
mapMethod · 0.65
getMethod · 0.65
addMethod · 0.45

Tested by 1

readOfFunction · 0.72