strip-types-loader.mjs 769 B

123456789101112131415161718192021
  1. /**
  2. * Node 类型剥离直跑源码的解析钩子:仓内源码按 tsc/vite 习惯用无扩展名相对导入,
  3. * Node ESM 要求显式扩展名——这里对相对导入补试 `.ts`(benchmark-impact 用)。
  4. */
  5. import path from 'node:path'
  6. export async function resolve(specifier, context, next) {
  7. if ((specifier.startsWith('./') || specifier.startsWith('../')) && path.extname(specifier) === '') {
  8. try {
  9. return await next(`${specifier}.ts`, context)
  10. } catch {
  11. // 目录入口(../provenance → provenance/index.ts)或确实不存在:交给默认解析
  12. try {
  13. return await next(`${specifier}/index.ts`, context)
  14. } catch {
  15. // 交给默认解析报出原始错误
  16. }
  17. }
  18. }
  19. return next(specifier, context)
  20. }