From 4a87be85bf4c02fee7895ebf5a6cbf57d2384ca4 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 17 Oct 2024 15:36:57 -0700 Subject: [PATCH] escape + char at regexp to resolve pnpn issue --- CHANGELOG.md | 3 ++- src/esmockLoader.js | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d85dbc4..315893e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # changelog - * 2.6.8 _tbd_ + * 2.6.8 _Oct.17.2024_ + * [resolve issue for pnpm](https://github.com/iambumblehead/esmock/pull/314) by escaping '+' char in regexp * [add log utility function](https://github.com/iambumblehead/esmock/pull/314) for debugging loader * [dropped ava and jest](https://github.com/iambumblehead/esmock/pull/314) from test sequence, node v22 --loader issues * 2.6.7 _Jul.16.2024_ diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 0b16977..84970ca 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -25,9 +25,14 @@ const isnotfoundRe = /isfound=false/ const iscommonjsmoduleRe = /^(commonjs|module)$/ const isstrict3 = /strict=3/ const hashbangRe = /^(#![^\n]*\n)/ + +// escape '+' char, pnpm generates long pathnames serialized with these +const moduleIdEsc = str => + str.indexOf('+') >= 0 ? str.replace(/(?!\\)\+/g, '\\+') : str + // returned regexp will match embedded moduleid w/ treeid const moduleIdReCreate = (moduleid, treeid) => new RegExp( - `.*(${moduleid}(\\?${treeid}(?:(?!#-#).)*)).*`) + `.*(${moduleIdEsc(moduleid)}(\\?${treeid}(?:(?!#-#).)*)).*`) // node v12.0-v18.x, global const mockKeys = global.mockKeys = (global.mockKeys || {})