()
| 35 | * @since 4.0.0 |
| 36 | */ |
| 37 | export const plugin = (): Plugin => { |
| 38 | const store = new Map<string, ReadonlyArray<Source.Snippet>>() |
| 39 | const cache = new Map<string, { |
| 40 | readonly version: string | undefined |
| 41 | readonly value: Promise<ReadonlyArray<Source.Snippet>> |
| 42 | }>() |
| 43 | |
| 44 | const loadExamples = ( |
| 45 | file: string, |
| 46 | version?: string | undefined |
| 47 | ): Promise<ReadonlyArray<Source.Snippet>> => { |
| 48 | const cached = cache.get(file) |
| 49 | if (cached !== undefined && cached.version === version) { |
| 50 | return cached.value |
| 51 | } |
| 52 | |
| 53 | const loaded = Source.extractFile(file).then((snippets) => { |
| 54 | store.set(file, snippets) |
| 55 | return snippets |
| 56 | }) |
| 57 | |
| 58 | cache.set(file, { version, value: loaded }) |
| 59 | return loaded |
| 60 | } |
| 61 | |
| 62 | return { |
| 63 | name: "effect-doctest", |
| 64 | enforce: "pre", |
| 65 | perEnvironmentWatchChangeDuringDev: true, |
| 66 | config(config) { |
| 67 | if (config.test?.runner !== undefined) { |
| 68 | return undefined |
| 69 | } |
| 70 | |
| 71 | return { test: { runner } satisfies TestUserConfig } |
| 72 | }, |
| 73 | resolveId(source, importer, options) { |
| 74 | const collector = Protocol.request(Protocol.collectorPrefix, source) |
| 75 | if (collector !== undefined) { |
| 76 | return Protocol.resolvedId("collector", collector) |
| 77 | } |
| 78 | |
| 79 | const snippet = Protocol.request(Protocol.snippetPrefix, source) |
| 80 | if (snippet !== undefined && store.has(snippet.file)) { |
| 81 | return Protocol.resolvedId("snippet", snippet) |
| 82 | } |
| 83 | |
| 84 | const parent = importer === undefined ? undefined : Protocol.resolvedRequest(importer) |
| 85 | if (parent?.kind !== "snippet") { |
| 86 | return null |
| 87 | } |
| 88 | |
| 89 | return this.resolve(source, parent.file, { ...options, skipSelf: true }) |
| 90 | }, |
| 91 | load(id) { |
| 92 | const loaded = Protocol.resolvedRequest(id) |
| 93 | if (loaded === undefined) { |
| 94 | return null |
nothing calls this directly
no outgoing calls
no test coverage detected