Skip to content

Commit dff0527

Browse files
committed
chore: normalized paths in requireHook for windows
1 parent b3d029e commit dff0527

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

packages/core/src/util/requireHook.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,21 @@ function patchedModuleLoad(moduleName) {
6666
// However, when an ESM library imports a CommonJS package, our requireHook is triggered.
6767
// For native ESM libraries the iitmHook is triggered.
6868
if (path.isAbsolute(moduleName) && ['.node', '.json', '.ts'].indexOf(path.extname(moduleName)) === -1) {
69+
// Normalize path separators for cross-platform compatibility (Windows uses backslashes)
70+
const normalizedPath = moduleName.replace(/\\/g, '/');
71+
6972
// EDGE CASE for ESM: mysql2/promise.js
70-
if (moduleName.indexOf('node_modules/mysql2/promise.js') !== -1) {
73+
if (normalizedPath.indexOf('node_modules/mysql2/promise.js') !== -1) {
7174
moduleName = 'mysql2/promise';
7275
} else {
7376
// e.g. path is node_modules/@elastic/elasicsearch/index.js
74-
let match = moduleName.match(/node_modules\/(@.*?(?=\/)\/.*?(?=\/))/);
77+
let match = normalizedPath.match(/node_modules\/(@.*?(?=\/)\/.*?(?=\/))/);
7578

7679
if (match && match.length > 1) {
7780
moduleName = match[1];
7881
} else {
7982
// e.g. path is node_modules/mysql/lib/index.js
80-
match = moduleName.match(/node_modules\/(.*?(?=\/))/);
83+
match = normalizedPath.match(/node_modules\/(.*?(?=\/))/);
8184

8285
if (match && match.length > 1) {
8386
moduleName = match[1];
@@ -92,6 +95,9 @@ function patchedModuleLoad(moduleName) {
9295

9396
/** @type {string} */
9497
const filename = /** @type {*} */ (Module)._resolveFilename.apply(Module, arguments);
98+
if (logger) {
99+
logger.debug(`[requireHook] Resolved filename: ${filename}`);
100+
}
95101

96102
// We are not directly manipulating the global module cache because there might be other tools fiddling with
97103
// Module._load. We don't want to break any of them.
@@ -145,8 +151,11 @@ function patchedModuleLoad(moduleName) {
145151
}
146152

147153
if (!cacheEntry.byFileNamePatternTransformersApplied) {
154+
// Normalize filename for cross-platform regex matching (Windows uses backslashes)
155+
const normalizedFilename = filename.replace(/\\/g, '/');
156+
148157
for (let i = 0; i < byFileNamePatternTransformers.length; i++) {
149-
if (byFileNamePatternTransformers[i].pattern.test(filename)) {
158+
if (byFileNamePatternTransformers[i].pattern.test(normalizedFilename)) {
150159
cacheEntry.moduleExports =
151160
byFileNamePatternTransformers[i].fn(cacheEntry.moduleExports, filename) || cacheEntry.moduleExports;
152161
}

0 commit comments

Comments
 (0)