Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check require.cache to find preloaded modules #406

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const path = require('node:path')

// runtime cache
const cache = {}

Expand All @@ -15,10 +17,23 @@ function checkProcessArgv (moduleName) {
let preloadModules
function checkPreloadModules (moduleName) {
/* c8 ignore start */
// nullish needed for non Node.js runtime
preloadModules ??= (process._preload_modules ?? [])
// coverage - nullish needed for non Node.js runtime
preloadModules ??= [...(process._preload_modules ?? [])]

// coverage - TS specific
if (preloadModules.includes(moduleName)) {
return true
}

const modulePath = path.join(process.cwd(), 'node_modules', moduleName)
if (Object.keys(require.cache).some((k) => k.startsWith(modulePath))) {
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
preloadModules.push(moduleName)
Copy link

@zetaraku zetaraku Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still discourage mutating preloadModules, as you may mutate the process._preload_modules that may be used by others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return true
}
/* c8 ignore stop */
return preloadModules.includes(moduleName)

return false
}

let preloadModulesString
Expand Down