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 1 commit
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
11 changes: 9 additions & 2 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const path = require('path')
jean-michelet marked this conversation as resolved.
Show resolved Hide resolved

// runtime cache
const cache = {}

Expand All @@ -14,11 +16,16 @@ function checkProcessArgv (moduleName) {

let preloadModules
function checkPreloadModules (moduleName) {
const modulePath = path.join(process.cwd(), 'node_modules/', moduleName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about this path join.
I just want to do a startsWith instead of includes.

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.

Can process.cwd() guarantee to be the project root if the program is executed from other path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you do?


/* c8 ignore start */
// nullish needed for non Node.js runtime
// coverage - nullish needed for non Node.js runtime
preloadModules ??= (process._preload_modules ?? [])
jean-michelet marked this conversation as resolved.
Show resolved Hide resolved

// coverage - TS specific
return preloadModules.includes(moduleName) ||
Object.keys(require.cache).some(k => k.startsWith(modulePath) && preloadModules.push(modulePath))
jean-michelet marked this conversation as resolved.
Show resolved Hide resolved
/* c8 ignore stop */
return preloadModules.includes(moduleName)
}

let preloadModulesString
Expand Down