Skip to content

Commit

Permalink
start renaming esmockkey to more descriptive treeid, step 4
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Oct 1, 2022
1 parent 95043c8 commit 774c327
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
63 changes: 30 additions & 33 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const isLT1612 = major < 16 || (major === 16 && minor < 12)
const esmkgdefsAndAfterRe = /\?esmkgdefs=.*/
const esmkgdefsAndBeforeRe = /.*\?esmkgdefs=/
const esmkdefsRe = /#-#esmkdefs/
const exportNamesRe = /.*exportNames=(.*)/
const esmkTreeIdRe = /esmkTreeId=\d*/
const esmkModuleIdRe = /esmkModuleId=([^&]*)/
const esmkIdRe = /\?esmk=\d*/
const exportNamesRe = /.*exportNames=(.*)/
const withHashRe = /.*#-#/
const isesmRe = /isesm=true/
const notfoundRe = /notfound=([^&]*)/
const isnotfoundRe = /isfound=false/

// new versions of node: when multiple loaders are used and context
// is passed to nextResolve, the process crashes in a recursive call
Expand All @@ -30,49 +32,44 @@ const nextResolveCall = async (nextResolve, specifier, context) => (

const resolve = async (specifier, context, nextResolve) => {
const { parentURL } = context
const [esmockKeyParamSmall] =
(parentURL && parentURL.match(/\?esmk=\d*/)) || []
const esmockKeyLong = esmockKeyParamSmall
? global.esmockTreeIdGet(esmockKeyParamSmall.split('=')[1])
const treeidspec = esmkIdRe.test(parentURL)
? global.esmockTreeIdGet(parentURL.match(esmkIdRe)[0].split('=')[1])
: parentURL

if (!esmkTreeIdRe.test(esmockKeyLong))
if (!esmkTreeIdRe.test(treeidspec))
return nextResolveCall(nextResolve, specifier, context)

const [esmockKeyParam] = String(esmockKeyLong).match(esmkTreeIdRe)
const [keyUrl, keys] = esmockKeyLong.split(esmkdefsRe)
const moduleGlobals = keyUrl && keyUrl.replace(esmkgdefsAndBeforeRe, '')
const [treeid] = String(treeidspec).match(esmkTreeIdRe)
const [url, defs] = treeidspec.split(esmkdefsRe)
const gdefs = url && url.replace(esmkgdefsAndBeforeRe, '')
// do not call 'nextResolve' for notfound modules
if (esmockKeyLong.includes(`notfound=${specifier}`)) {
const moduleKeyRe = new RegExp(
'.*file:///' + specifier + '(\\?' + esmockKeyParam + '(?:(?!#-#).)*).*')
const moduleKey = (
moduleGlobals.match(moduleKeyRe) || keys.match(moduleKeyRe) || [])[1]
if (moduleKey) {
if (treeidspec.includes(`esmkModuleId=${specifier}&isfound=false`)) {
const moduleIdRe = new RegExp(
'.*file:///' + specifier + '(\\?' + treeid + '(?:(?!#-#).)*).*')
const moduleId = (
gdefs.match(moduleIdRe) || defs.match(moduleIdRe) || [])[1]
if (moduleId) {
return {
shortCircuit: true,
url: urlDummy + moduleKey
url: urlDummy + moduleId
}
}
}

const resolved = await nextResolveCall(nextResolve, specifier, context)
const resolvedurl = decodeURI(resolved.url)
const moduleKeyRe = new RegExp(
'.*(' + resolvedurl + '\\?' + esmockKeyParam + '(?:(?!#-#).)*).*')
const moduleKeyChild = moduleKeyRe.test(keys)
&& keys.replace(moduleKeyRe, '$1')
const moduleKeyGlobal = moduleKeyRe.test(moduleGlobals)
&& moduleGlobals.replace(moduleKeyRe, '$1')

const moduleKey = moduleKeyChild || moduleKeyGlobal
if (moduleKey) {
resolved.url = isesmRe.test(moduleKey)
? moduleKey
: urlDummy + '#-#' + moduleKey
} else if (moduleGlobals && moduleGlobals !== '0') {
const moduleIdRe = new RegExp(
'.*(' + resolvedurl + '\\?' + treeid + '(?:(?!#-#).)*).*')
const moduleId =
moduleIdRe.test(defs) && defs.replace(moduleIdRe, '$1') ||
moduleIdRe.test(gdefs) && gdefs.replace(moduleIdRe, '$1')
if (moduleId) {
resolved.url = isesmRe.test(moduleId)
? moduleId
: urlDummy + '#-#' + moduleId
} else if (gdefs && gdefs !== '0') {
if (!resolved.url.startsWith('node:')) {
resolved.url += '?esmkgdefs=' + moduleGlobals
resolved.url += '?esmkgdefs=' + gdefs
}
}

Expand All @@ -86,8 +83,8 @@ const load = async (url, context, nextLoad) => {
url = url.replace(esmkgdefsAndAfterRe, '')
if (url.startsWith(urlDummy)) {
url = url.replace(withHashRe, '')
if (notfoundRe.test(url))
url = url.replace(urlDummy, `file:///${url.match(notfoundRe)[1]}`)
if (isnotfoundRe.test(url))
url = url.replace(urlDummy, `file:///${url.match(esmkModuleIdRe)[1]}`)
}

const exportedNames = exportNamesRe.test(url) &&
Expand Down
9 changes: 4 additions & 5 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const esmockModuleImportedSanitize = (imported, esmkTreeId) => {
: Object.assign({}, importedDefault, imported, { esmkTreeId })
}

const esmockModuleImportedPurge = modulePathKey => {
const esmockModuleImportedPurge = treeid => {
const purgeKey = key => key === 'null' || esmockCacheSet(key, null)
const longKey = esmockTreeIdGet(modulePathKey.split('esmk=')[1])
const longKey = esmockTreeIdGet(treeid.split('esmk=')[1])
const [url, keys] = longKey.split('#-#esmkdefs=')

String(keys).split('#-#').forEach(purgeKey)
Expand All @@ -83,13 +83,12 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => {
def = esmockModuleApply(
opt.strict || !fileURL || await import(fileURL), def, fileURL)

const mockExportNames = Object.keys(def).sort().join()
const mockModuleKey = (fileURL || 'file:///' + id) + '?' + [
'esmkTreeId=' + treeid,
'esmkModuleId=' + id,
'isfound=' + Boolean(fileURL),
'isesm=' + esmockModuleIsESM(fileURL),
fileURL ? 'found' : 'notfound=' + id,
mockExportNames ? 'exportNames=' + mockExportNames : 'exportNone'
'exportNames=' + Object.keys(def).sort().join()
].join('&')

esmockCacheSet(mockModuleKey, def)
Expand Down

0 comments on commit 774c327

Please sign in to comment.