Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse moduleidre #231

Merged
merged 8 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/esmock.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 0 additions & 3 deletions src/esmockIsLoader.js

This file was deleted.

21 changes: 11 additions & 10 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (
Expand Down Expand Up @@ -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,
Expand All @@ -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')
Expand All @@ -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,
Expand Down Expand Up @@ -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}