(opts?: PrecompilePluginOptions)
| 46 | import { createSyncPrecompiler, isPrecompilerWorker } from "./worker.ts" |
| 47 | |
| 48 | export const register = (opts?: PrecompilePluginOptions): void => { |
| 49 | // added in Node 22.15 |
| 50 | if (!module.registerHooks) |
| 51 | throwError(writeNodeVersionMessage("@ark/aot/node", "22.15")) |
| 52 | refuseDeclare(opts, "Node") |
| 53 | // a worker still runs what NODE_OPTIONS preloads, which may register these hooks |
| 54 | if (isPrecompilerWorker) return |
| 55 | const set = withSettingsFile(opts) |
| 56 | refusePrune(set, "Node") |
| 57 | opts = resolvePrecompileOptions(set) |
| 58 | const isStandalone = opts.runtime !== "arktype" |
| 59 | const variant = resolvedVariantOf(opts) |
| 60 | const request = createSyncPrecompiler({ |
| 61 | precompilerOptions: driverOptionsOf( |
| 62 | set, |
| 63 | // `node --test` marks each file's process, or runs the files in its own |
| 64 | process.env.NODE_TEST_CONTEXT !== undefined || |
| 65 | process.execArgv.includes("--test") |
| 66 | ) |
| 67 | }) |
| 68 | const filter = transformedFilterOf( |
| 69 | optedInPackagesOf(opts?.cwd ?? process.cwd(), opts.dependencies) |
| 70 | ) |
| 71 | const isTransformed = (id: string) => |
| 72 | filter.include.test(id) && !filter.exclude.test(id) |
| 73 | // as a development build resolves it, so Node prints errors as their issues |
| 74 | const runtimeConditions = isProductionEnv() ? [] : ["development"] |
| 75 | const resolveRuntime: module.ResolveHookSync = ( |
| 76 | specifier, |
| 77 | context, |
| 78 | nextResolve |
| 79 | ) => |
| 80 | specifier === "arktype/runtime" ? |
| 81 | nextResolve(specifier, { |
| 82 | ...context, |
| 83 | conditions: [...context.conditions, ...runtimeConditions] |
| 84 | }) |
| 85 | : nextResolve(specifier, context) |
| 86 | const modules: Record<string, string> = {} |
| 87 | const importersByUrl: Record<string, string> = {} |
| 88 | const packageNamesByDir: PackageNamesByDir = {} |
| 89 | // modules whose text these hooks were given |
| 90 | const givenUrls = new Set<string>() |
| 91 | // allowUnsafe is offered only where building anyway may be what's wanted |
| 92 | const reportUnsafe = (problem: string, offersAllowUnsafe = true) => { |
| 93 | if (opts?.allowUnsafe) process.emitWarning(problem) |
| 94 | else throwError(offersAllowUnsafe ? withAllowUnsafe(problem) : problem) |
| 95 | } |
| 96 | const reportedPrecompiled = new Set<string>() |
| 97 | const checkPrecompiled = (specifier: string, importer: string) => { |
| 98 | const name = precompiledDependencyOf(specifier, importer) |
| 99 | if (!name || reportedPrecompiled.has(name)) return |
| 100 | reportedPrecompiled.add(name) |
| 101 | reportUnsafe(writePrecompiledDependencyMessage(name)) |
| 102 | } |
| 103 | // e.g. a module another hook loads, code given to node --eval, or a file without an extension |
| 104 | const untransformedProblemOf = (specifier: string, importer: string) => { |
| 105 | if (!importer.startsWith("file:")) |
no test coverage detected