|
1 | 1 | import { fork, type ChildProcess } from 'node:child_process' |
2 | 2 | import { existsSync } from 'node:fs' |
3 | | -import { readFile } from 'node:fs/promises' |
| 3 | +import { access, readFile } from 'node:fs/promises' |
4 | 4 | import path from 'node:path' |
5 | 5 | import { parse } from '@babel/parser' |
6 | 6 | import { createDebug } from 'obug' |
@@ -42,6 +42,7 @@ export interface TsModule { |
42 | 42 | /** `.ts` file name */ |
43 | 43 | id: string |
44 | 44 | isEntry: boolean |
| 45 | + jsFile: boolean |
45 | 46 | } |
46 | 47 | /** dts filename -> ts module */ |
47 | 48 | export type DtsMap = Map<string, TsModule> |
@@ -190,15 +191,15 @@ export function createGeneratePlugin({ |
190 | 191 | }, |
191 | 192 | }, |
192 | 193 | handler(code, id) { |
193 | | - const shouldEmit = !RE_JS.test(id) || emitJs |
| 194 | + const jsFile = RE_JS.test(id) |
194 | 195 |
|
195 | | - if (shouldEmit) { |
| 196 | + if (!jsFile || emitJs) { |
196 | 197 | const mod = this.getModuleInfo(id) |
197 | 198 | const isEntry = entryMatcher |
198 | 199 | ? entryMatcher(path.relative(cwd, id)) |
199 | 200 | : !!mod?.isEntry |
200 | 201 | const dtsId = filename_to_dts(id) |
201 | | - dtsMap.set(dtsId, { code, id, isEntry }) |
| 202 | + dtsMap.set(dtsId, { code, id, isEntry, jsFile }) |
202 | 203 | debug('register dts source: %s', id) |
203 | 204 |
|
204 | 205 | if (isEntry) { |
@@ -226,9 +227,20 @@ export function createGeneratePlugin({ |
226 | 227 | }, |
227 | 228 | }, |
228 | 229 | async handler(dtsId) { |
229 | | - if (!dtsMap.has(dtsId)) return |
| 230 | + const module = dtsMap.get(dtsId) |
| 231 | + if (!module) return |
| 232 | + |
| 233 | + const { code, id, jsFile } = module |
| 234 | + if ( |
| 235 | + jsFile && |
| 236 | + (await access(dtsId) |
| 237 | + .then(() => true) |
| 238 | + .catch(() => false)) |
| 239 | + ) { |
| 240 | + debug('dts file already exists for %s, skipping generation', id) |
| 241 | + return |
| 242 | + } |
230 | 243 |
|
231 | | - const { code, id } = dtsMap.get(dtsId)! |
232 | 244 | let dtsCode: string | undefined |
233 | 245 | let map: SourceMapInput | undefined |
234 | 246 | debug('generate dts %s from %s', dtsId, id) |
|
0 commit comments