From cd9a6c04bcabcf775b41753c1acbff4ea37d9895 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 31 Jul 2024 17:49:22 +0200 Subject: [PATCH 1/5] fix: check require.cache to find preloaded modules --- runtime.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/runtime.js b/runtime.js index ed3623f2..e0a32338 100644 --- a/runtime.js +++ b/runtime.js @@ -1,5 +1,7 @@ 'use strict' +const path = require('path') + // runtime cache const cache = {} @@ -14,11 +16,16 @@ function checkProcessArgv (moduleName) { let preloadModules function checkPreloadModules (moduleName) { + const modulePath = path.join(process.cwd(), 'node_modules/', moduleName) + /* c8 ignore start */ - // nullish needed for non Node.js runtime + // coverage - nullish needed for non Node.js runtime preloadModules ??= (process._preload_modules ?? []) + + // coverage - TS specific + return preloadModules.includes(moduleName) || + Object.keys(require.cache).some(k => k.startsWith(modulePath) && preloadModules.push(modulePath)) /* c8 ignore stop */ - return preloadModules.includes(moduleName) } let preloadModulesString From 28dc49fff5761e506608e8f0ea21522db04e9823 Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 31 Jul 2024 22:39:28 +0200 Subject: [PATCH 2/5] refactor: dont mutate preloadModules in some --- runtime.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/runtime.js b/runtime.js index e0a32338..d1943608 100644 --- a/runtime.js +++ b/runtime.js @@ -23,9 +23,18 @@ function checkPreloadModules (moduleName) { preloadModules ??= (process._preload_modules ?? []) // coverage - TS specific - return preloadModules.includes(moduleName) || - Object.keys(require.cache).some(k => k.startsWith(modulePath) && preloadModules.push(modulePath)) + if (preloadModules.includes(moduleName)) { + return true + } + + if (Object.keys(require.cache).some(k => k.startsWith(modulePath))) { + preloadModules.push(moduleName) + + return true + } /* c8 ignore stop */ + + return false } let preloadModulesString From d5a650258712898b22f8e4199f277a2354cb0b7b Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 31 Jul 2024 23:07:27 +0200 Subject: [PATCH 3/5] refactor: declare modulePath only if necessary --- runtime.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/runtime.js b/runtime.js index d1943608..6055b8bf 100644 --- a/runtime.js +++ b/runtime.js @@ -16,8 +16,6 @@ function checkProcessArgv (moduleName) { let preloadModules function checkPreloadModules (moduleName) { - const modulePath = path.join(process.cwd(), 'node_modules/', moduleName) - /* c8 ignore start */ // coverage - nullish needed for non Node.js runtime preloadModules ??= (process._preload_modules ?? []) @@ -27,6 +25,7 @@ function checkPreloadModules (moduleName) { return true } + const modulePath = path.join(process.cwd(), 'node_modules/', moduleName) if (Object.keys(require.cache).some(k => k.startsWith(modulePath))) { preloadModules.push(moduleName) From 77a701df215751d15a98c8b72869ba306c1de20e Mon Sep 17 00:00:00 2001 From: jean Date: Wed, 31 Jul 2024 23:09:23 +0200 Subject: [PATCH 4/5] refactor: no need for trailing / --- runtime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime.js b/runtime.js index 6055b8bf..b022e9d8 100644 --- a/runtime.js +++ b/runtime.js @@ -25,7 +25,7 @@ function checkPreloadModules (moduleName) { return true } - const modulePath = path.join(process.cwd(), 'node_modules/', moduleName) + const modulePath = path.join(process.cwd(), 'node_modules', moduleName) if (Object.keys(require.cache).some(k => k.startsWith(modulePath))) { preloadModules.push(moduleName) From cef560e7ca97101360e5a60859f66f150130943b Mon Sep 17 00:00:00 2001 From: jean Date: Thu, 1 Aug 2024 07:35:30 +0200 Subject: [PATCH 5/5] fix: should not reference process._preload_modules --- runtime.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime.js b/runtime.js index b022e9d8..d27bbe40 100644 --- a/runtime.js +++ b/runtime.js @@ -1,6 +1,6 @@ 'use strict' -const path = require('path') +const path = require('node:path') // runtime cache const cache = {} @@ -18,7 +18,7 @@ let preloadModules function checkPreloadModules (moduleName) { /* c8 ignore start */ // coverage - nullish needed for non Node.js runtime - preloadModules ??= (process._preload_modules ?? []) + preloadModules ??= [...(process._preload_modules ?? [])] // coverage - TS specific if (preloadModules.includes(moduleName)) { @@ -26,7 +26,7 @@ function checkPreloadModules (moduleName) { } const modulePath = path.join(process.cwd(), 'node_modules', moduleName) - if (Object.keys(require.cache).some(k => k.startsWith(modulePath))) { + if (Object.keys(require.cache).some((k) => k.startsWith(modulePath))) { preloadModules.push(moduleName) return true