Skip to content

Commit f499c4c

Browse files
committed
Fix coverage by rewriting non-inlineable require calls to dist paths
During coverage, Vitest transforms imports to load TypeScript sources from registry/src/, but require() bypasses Vite's module system and can't load .ts files. Updated require-transform plugin to rewrite non-inlineable require() calls to use absolute paths pointing to compiled dist/ files.
1 parent e2a49aa commit f499c4c

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

scripts/constants.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const LAZY_LICENSE_CONTENT = () =>
6363
fs.readFileSync(constants.rootLicensePath, UTF8)
6464

6565
const lazyEcosystems = () => {
66-
const registryLibFs = /*@__PURE__*/ require('../registry/dist/lib/fs')
66+
const registryLibFs = /*@__PURE__*/ require('../registry/dist/lib/fs.js')
6767
const readDirNamesSync = registryLibFs.readDirNamesSync
6868
return Object.freeze(readDirNamesSync(constants.rootPackagesPath))
6969
}
@@ -91,7 +91,7 @@ const lazyIgnoreGlobs = () =>
9191
])
9292

9393
const lazyNpmPackageNames = () => {
94-
const registryLibFs = /*@__PURE__*/ require('../registry/dist/lib/fs')
94+
const registryLibFs = /*@__PURE__*/ require('../registry/dist/lib/fs.js')
9595
const readDirNamesSync = registryLibFs.readDirNamesSync
9696
return Object.freeze(readDirNamesSync(constants.npmPackagesPath))
9797
}

vitest-plugins/require-transform.mts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,22 +243,23 @@ export function createRequireTransformPlugin(
243243

244244
const requirePath = node.arguments[0].value
245245

246-
// Step 8: Only handle relative .js requires from our code.
246+
// Step 8: Only handle relative requires from our code.
247247
// We don't inline:
248248
// - Absolute requires (e.g., require('fs'))
249249
// - npm package requires (e.g., require('lodash'))
250-
// - Requires without .js extension
251-
// Not a relative .js require, skip.
252-
if (!requirePath.startsWith('./') || !requirePath.endsWith('.js')) {
250+
// Not a relative require, skip.
251+
if (!requirePath.startsWith('./')) {
253252
return
254253
}
255254

256255
try {
257256
// Step 9: Resolve the require modulePath to the actual TypeScript file.
258257
// Directory of current file.
259258
const currentDir = dirname(id)
260-
// Convert .js to .ts.
261-
const tsPath = requirePath.replace(/\.js$/, '.ts')
259+
// Convert .js to .ts, or add .ts if no extension.
260+
const tsPath = requirePath.endsWith('.js')
261+
? requirePath.replace(/\.js$/, '.ts')
262+
: `${requirePath}.ts`
262263
// Absolute file path.
263264
const resolvedPath = resolve(currentDir, tsPath)
264265

@@ -290,6 +291,31 @@ export function createRequireTransformPlugin(
290291
// This preserves the rest of the source code exactly as-is.
291292
s.overwrite(replaceStart, replaceEnd, value)
292293
modified = true
294+
} else {
295+
// Step 11b: If we can't inline, rewrite to use compiled dist/ version.
296+
// During coverage, require() can't load TypeScript files, so we use
297+
// the compiled JavaScript files from dist/ which Node can handle.
298+
const stringNode = node.arguments[0] as t.StringLiteral
299+
// Get the base project directory.
300+
const projectRoot = id.substring(
301+
0,
302+
id.indexOf('/registry/src/'),
303+
)
304+
// Build absolute path to the compiled dist/ file.
305+
const moduleName = requirePath
306+
.replace(/^\.\//, '')
307+
.replace(/\.js$/, '')
308+
const absoluteDistPath = resolve(
309+
projectRoot,
310+
'registry/dist/lib',
311+
`${moduleName}.js`,
312+
)
313+
s.overwrite(
314+
stringNode.start!,
315+
stringNode.end!,
316+
`'${absoluteDistPath}'`,
317+
)
318+
modified = true
293319
}
294320
} catch {
295321
// Resolution error (file doesn't exist, etc.).

0 commit comments

Comments
 (0)