diff --git a/CHANGELOG.md b/CHANGELOG.md index b8601bc7..dc44d07d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # changelog + * 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 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", diff --git a/src/esmock.js b/src/esmock.js index cb9ccbcd..5a3a1bd3 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -1,11 +1,11 @@ -import esmockIsLoader from './esmockIsLoader.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) 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 d28f7aa0..6f60c391 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') @@ -118,11 +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 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, @@ -187,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, loaderIsVerified, globalPreload} +export {load, resolve, getSource, globalPreload, loaderIsVerified as default}