From 626831fba013b890daffb2cdb5fc1322459bf9ad Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:08:12 -0700 Subject: [PATCH 1/8] reuse moduleidre --- CHANGELOG.md | 2 ++ src/esmockLoader.js | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8601bc7..387a7e8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 2.3.8 _unreleased_ + * [reuse moduleid regexp](https://github.com/iambumblehead/esmock/pull/230) replacing separately created regexps * 2.3.7 _Aug.15.2023_ * [normalize package.json url](https://github.com/iambumblehead/esmock/pull/225) and [reduce loc for loader verification](https://github.com/iambumblehead/esmock/pull/226) * [small adjustments](https://github.com/iambumblehead/esmock/pull/228) reducing lines of code diff --git a/src/esmockLoader.js b/src/esmockLoader.js index d28f7aa0..fe7f61ac 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -19,6 +19,9 @@ const withHashRe = /.*#-#/ const isesmRe = /isesm=true/ const isnotfoundRe = /isfound=false/ const hashbangRe = /^(#![^\n]*\n)/ +// returned regexp will match embedded moduleid w/ treeid +const moduleIdReCreate = (moduleid, treeid) => new RegExp( + `.*(${moduleid}(\\?${treeid}(?:(?!#-#).)*)).*`) const globalPreload = (({ port }) => ( port.addEventListener('message', ev => ( @@ -77,10 +80,9 @@ const resolve = async (specifier, context, nextResolve) => { const gdefs = url && url.replace(esmkgdefsAndBeforeRe, '') // do not call 'nextResolve' for notfound modules if (treeidspec.includes(`esmkModuleId=${specifier}&isfound=false`)) { - const moduleIdRe = new RegExp( - '.*file:///' + specifier + '(\\?' + treeid + '(?:(?!#-#).)*).*') + const moduleIdRe = moduleIdReCreate(`file:///${specifier}`, treeid) const moduleId = ( - gdefs.match(moduleIdRe) || defs.match(moduleIdRe) || [])[1] + gdefs.match(moduleIdRe) || defs.match(moduleIdRe) || [])[2] if (moduleId) { return { shortCircuit: true, @@ -97,8 +99,7 @@ const resolve = async (specifier, context, nextResolve) => { } const resolved = await nextResolveCall(nextResolve, specifier, context) - const moduleIdRe = new RegExp( - '.*(' + resolved.url + '\\?' + treeid + '(?:(?!#-#).)*).*') + const moduleIdRe = moduleIdReCreate(resolved.url, treeid) const moduleId = moduleIdRe.test(defs) && defs.replace(moduleIdRe, '$1') || moduleIdRe.test(gdefs) && gdefs.replace(moduleIdRe, '$1') From 7e8e08f59999ec2e22dea8efb659ad5df4deb444 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:13:18 -0700 Subject: [PATCH 2/8] correct link at changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 387a7e8e..dcc4d9cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # changelog * 2.3.8 _unreleased_ - * [reuse moduleid regexp](https://github.com/iambumblehead/esmock/pull/230) replacing separately created regexps + * [reuse moduleid regexp](https://github.com/iambumblehead/esmock/pull/231) replacing separately created regexps * 2.3.7 _Aug.15.2023_ * [normalize package.json url](https://github.com/iambumblehead/esmock/pull/225) and [reduce loc for loader verification](https://github.com/iambumblehead/esmock/pull/226) * [small adjustments](https://github.com/iambumblehead/esmock/pull/228) reducing lines of code From 12b442ab50c2226d7c5e59da350f4cb1fd84baae Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:22:18 -0700 Subject: [PATCH 3/8] git rm esmockIsLoader.js; export from esmockLoader.js instead --- src/esmock.js | 3 ++- src/esmockIsLoader.js | 3 --- src/esmockLoader.js | 3 ++- 3 files changed, 4 insertions(+), 5 deletions(-) delete mode 100644 src/esmockIsLoader.js diff --git a/src/esmock.js b/src/esmock.js index cb9ccbcd..ee06f1ff 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -1,4 +1,5 @@ -import esmockIsLoader from './esmockIsLoader.js' +//import esmockIsLoader from './esmockIsLoader.js' +import esmockIsLoader from './esmockLoader.js' import esmockModule from './esmockModule.js' import esmockArgs from './esmockArgs.js' import esmockErr from './esmockErr.js' diff --git a/src/esmockIsLoader.js b/src/esmockIsLoader.js deleted file mode 100644 index c4175b03..00000000 --- a/src/esmockIsLoader.js +++ /dev/null @@ -1,3 +0,0 @@ -import { loaderIsVerified } from './esmockLoader.js' - -export default (c => () => (c = c || loaderIsVerified(import.meta.url)))() diff --git a/src/esmockLoader.js b/src/esmockLoader.js index fe7f61ac..833d9871 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -122,6 +122,7 @@ const resolve = async (specifier, context, nextResolve) => { const loaderVerificationQuery = '?esmock-loader=true' const loaderIsVerified = url => import( url + loaderVerificationQuery).then(m => m.default === true) +const loaderIs = (c => () => (c = c || loaderIsVerified(import.meta.url)))() const load = async (url, context, nextLoad) => { if (url.endsWith(loaderVerificationQuery)) { return { @@ -188,4 +189,4 @@ const load = async (url, context, nextLoad) => { // node lt 16.12 require getSource, node gte 16.12 warn remove getSource const getSource = isLT1612 && load -export {load, resolve, getSource, loaderIsVerified, globalPreload} +export { load, resolve, getSource, globalPreload, loaderIs as default } From af6d70088cdd64abe87c0667c3dadb7e1ed8a167 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:27:18 -0700 Subject: [PATCH 4/8] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcc4d9cd..dc44d07d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * 2.3.8 _unreleased_ * [reuse moduleid regexp](https://github.com/iambumblehead/esmock/pull/231) replacing separately created regexps + * [remove esmockIsLoader.js](https://github.com/iambumblehead/esmock/pull/231) export from esmockLoader.js instead * 2.3.7 _Aug.15.2023_ * [normalize package.json url](https://github.com/iambumblehead/esmock/pull/225) and [reduce loc for loader verification](https://github.com/iambumblehead/esmock/pull/226) * [small adjustments](https://github.com/iambumblehead/esmock/pull/228) reducing lines of code From f92c272eecf5604a103a64ea2b0efc58ff0f96b8 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:27:54 -0700 Subject: [PATCH 5/8] increment version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 43c92ee0..681aa532 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.3.7", + "version": "2.3.8", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import and globals mocking for unit tests", From 151472902ea51bf6f93f7fe64ad6582cc2d51e57 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:32:15 -0700 Subject: [PATCH 6/8] remove commented-out import --- src/esmock.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/esmock.js b/src/esmock.js index ee06f1ff..5a3a1bd3 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -1,12 +1,11 @@ -//import esmockIsLoader from './esmockIsLoader.js' -import esmockIsLoader from './esmockLoader.js' +import esmockLoader from './esmockLoader.js' import esmockModule from './esmockModule.js' import esmockArgs from './esmockArgs.js' import esmockErr from './esmockErr.js' const esmockGo = opts => async (...args) => { const [moduleId, parent, defs, gdefs, opt] = esmockArgs(args, opts) - if (!await esmockIsLoader()) + if (!await esmockLoader()) throw esmockErr.errMissingLoader() const fileURLKey = await esmockModule(moduleId, parent, defs, gdefs, opt) From 01518095194a85148e520f86f6ffac755a8f42eb Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 00:34:22 -0700 Subject: [PATCH 7/8] remove added whitespaces --- src/esmockLoader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 833d9871..bb139314 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -189,4 +189,4 @@ const load = async (url, context, nextLoad) => { // node lt 16.12 require getSource, node gte 16.12 warn remove getSource const getSource = isLT1612 && load -export { load, resolve, getSource, globalPreload, loaderIs as default } +export {load, resolve, getSource, globalPreload, loaderIs as default} From 07bb778e7cec2741834ce711a949a55e8ba6a50a Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 15 Aug 2023 08:21:18 -0700 Subject: [PATCH 8/8] merge and simplify; loaderIs and loaderIsVerified, per review --- src/esmockLoader.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/esmockLoader.js b/src/esmockLoader.js index bb139314..6f60c391 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -119,12 +119,11 @@ const resolve = async (specifier, context, nextResolve) => { return resolved } -const loaderVerificationQuery = '?esmock-loader=true' -const loaderIsVerified = url => import( - url + loaderVerificationQuery).then(m => m.default === true) -const loaderIs = (c => () => (c = c || loaderIsVerified(import.meta.url)))() +const loaderVerificationUrl = urlDummy + '?esmock-loader=true' +const loaderIsVerified = (memo => () => memo = memo || ( + import(loaderVerificationUrl).then(m => m.default === true)))() const load = async (url, context, nextLoad) => { - if (url.endsWith(loaderVerificationQuery)) { + if (url === loaderVerificationUrl) { return { format: 'module', shortCircuit: true, @@ -189,4 +188,4 @@ const load = async (url, context, nextLoad) => { // node lt 16.12 require getSource, node gte 16.12 warn remove getSource const getSource = isLT1612 && load -export {load, resolve, getSource, globalPreload, loaderIs as default} +export {load, resolve, getSource, globalPreload, loaderIsVerified as default}