@@ -9,6 +9,8 @@ import MagicString from 'magic-string'
99import type { NodePath } from '@babel/traverse'
1010import type { Plugin } from 'vite'
1111
12+ import { getDistDir , srcToDistPath } from './transform-utils.mts'
13+
1214// Handle both ESM and CJS exports from @babel /traverse
1315const traverse = ( traverseModule as any ) . default || traverseModule
1416
@@ -250,34 +252,15 @@ export function createRequireTransformPlugin(
250252 return
251253 }
252254
253- // Get the directory of the current file
254- const currentDir = dirname ( id )
255-
256255 // Build a template literal that resolves to dist/
257256 // For constants/index.ts with require(`./\${k}`):
258257 // Transform to require(`/abs/path/dist/lib/constants/\${k}`)
258+ const distDir = getDistDir ( id )
259259
260- // Find the path relative to /registry/src/lib/
261- const libMarker = '/registry/src/lib/'
262- const libIndex = id . indexOf ( libMarker )
263-
264- if ( libIndex === - 1 ) {
260+ if ( ! distDir ) {
265261 return
266262 }
267263
268- // Get the directory path relative to lib/
269- const relativeDir = dirname (
270- id . substring ( libIndex + libMarker . length ) ,
271- )
272-
273- // Build absolute dist path
274- const projectRoot = id . substring ( 0 , libIndex )
275- const distDir = resolve (
276- projectRoot ,
277- 'registry/dist/lib' ,
278- relativeDir ,
279- )
280-
281264 // Reconstruct the template literal with absolute dist path
282265 // Replace the leading ./ or ../ with the absolute dist path
283266 const newQuasis = arg . quasis . map ( ( quasi , i ) => {
@@ -368,44 +351,13 @@ export function createRequireTransformPlugin(
368351 // During coverage, require() can't load TypeScript files, so we use
369352 // the compiled JavaScript files from dist/ which Node can handle.
370353 const stringNode = arg as t . StringLiteral
354+ const absoluteDistPath = srcToDistPath ( resolvedPath )
371355
372- // Strategy: Use the resolvedPath (absolute path to the TypeScript source)
373- // to determine the correct dist path with preserved directory structure.
374-
375- // Extract the path relative to /registry/src/lib/ from the resolved source file.
376- // Example: resolvedPath = '/abs/path/registry/src/lib/packages/normalize.ts'
377- // relativeToLib = 'packages/normalize.ts'
378- // Example: resolvedPath = '/abs/path/registry/src/lib/constants/WIN32.ts'
379- // relativeToLib = 'constants/WIN32.ts'
380- const libMarker = '/registry/src/lib/'
381- const libIndex = resolvedPath . indexOf ( libMarker )
382-
383- if ( libIndex === - 1 ) {
356+ if ( ! absoluteDistPath ) {
384357 // Not in lib directory, skip transformation.
385358 return
386359 }
387360
388- // Get everything after '/registry/src/lib/' from the resolved path.
389- // This gives us the full relative path including subdirectories.
390- const relativeToLib = resolvedPath . substring (
391- libIndex + libMarker . length ,
392- )
393-
394- // Convert TypeScript extension to JavaScript.
395- // Example: 'packages/normalize.ts' -> 'packages/normalize.js'
396- // Example: 'constants/WIN32.ts' -> 'constants/WIN32.js'
397- const relativeJsPath = relativeToLib . replace ( / \. t s $ / , '.js' )
398-
399- // Build the absolute dist path by combining project root + dist/lib + relative path.
400- // Example: '/abs/path' + 'registry/dist/lib/' + 'packages/normalize.js'
401- // = '/abs/path/registry/dist/lib/packages/normalize.js'
402- const projectRoot = resolvedPath . substring ( 0 , libIndex )
403- const absoluteDistPath = resolve (
404- projectRoot ,
405- 'registry/dist/lib' ,
406- relativeJsPath ,
407- )
408-
409361 // Replace the require string with the absolute dist path.
410362 s . overwrite (
411363 stringNode . start ! ,
0 commit comments