From 8458b630bdd42fd108367c0063a4be5067997c02 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 10:39:50 -0700 Subject: [PATCH 1/6] use loader mechanism to detect presence of loader --- src/esmock.js | 4 ++-- src/esmockIsLoader.js | 7 ++----- src/esmockLoader.js | 9 +++++++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/esmock.js b/src/esmock.js index 525ec99d..017434d3 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -3,10 +3,10 @@ import esmockModule from './esmockModule.js' import esmockArgs from './esmockArgs.js' const esmockGo = opts => async (...args) => { - if (!esmockIsLoader()) + const [moduleId, parent, defs, gdefs, opt] = esmockArgs(args, opts) + if (!(await esmockIsLoader())) throw new Error('process must be started with --loader=esmock') - const [moduleId, parent, defs, gdefs, opt] = esmockArgs(args, opts) const fileURLKey = await esmockModule(moduleId, parent, defs, gdefs, opt) const importedModule = await import(fileURLKey) diff --git a/src/esmockIsLoader.js b/src/esmockIsLoader.js index aeca4008..106f05e9 100644 --- a/src/esmockIsLoader.js +++ b/src/esmockIsLoader.js @@ -1,5 +1,2 @@ -const isloaderRe = /--(experimental-)?loader[=\s,"']*esmock/ - -export default (pr = process) => - isloaderRe.test(pr.execArgv) || - isloaderRe.test(pr.env.NODE_OPTIONS) +export default (cache => async () => typeof cache === 'boolean' ? cache + : cache = ((await import(`${import.meta.url}?test`)).default === true))() diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 6be61602..cb780de3 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -81,6 +81,15 @@ const resolve = async (specifier, context, nextResolve) => { } const load = async (url, context, nextLoad) => { + if (url.endsWith('esmockIsLoader.js?test')) { + return { + format: 'module', + shortCircuit: true, + responseURL: encodeURI(url), + source: 'export default true' + } + } + if (esmkdefsRe.test(url)) // parent of mocked modules return nextLoad(url, context) From 650f8fbdb31ce1264e988b58d424d49d60f0b524 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 11:26:58 -0700 Subject: [PATCH 2/6] update error message --- src/esmock.js | 3 ++- src/esmockErr.js | 7 ++++++- tests/tests-no-loader/esmock.noloader.test.js | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/esmock.js b/src/esmock.js index 017434d3..fa2e8f4b 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -1,11 +1,12 @@ import esmockIsLoader from './esmockIsLoader.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())) - throw new Error('process must be started with --loader=esmock') + throw esmockErr.errMissingLoader() const fileURLKey = await esmockModule(moduleId, parent, defs, gdefs, opt) const importedModule = await import(fileURLKey) diff --git a/src/esmockErr.js b/src/esmockErr.js index 35905f9b..ef016ef6 100644 --- a/src/esmockErr.js +++ b/src/esmockErr.js @@ -4,7 +4,12 @@ const errModuleIdNotFound = (moduleId, parent) => const errModuleIdNotMocked = (moduleId, parent) => new Error(`un-mocked moduleId: "${moduleId}" (used by ${parent})`) +const errMissingLoader = () => + new Error('the loader chain process must include esmock. ' + + 'start the process using --loader=esmock.') + export default { errModuleIdNotFound, - errModuleIdNotMocked + errModuleIdNotMocked, + errMissingLoader } diff --git a/tests/tests-no-loader/esmock.noloader.test.js b/tests/tests-no-loader/esmock.noloader.test.js index bc6a2c11..f6903926 100644 --- a/tests/tests-no-loader/esmock.noloader.test.js +++ b/tests/tests-no-loader/esmock.noloader.test.js @@ -1,9 +1,10 @@ import test from 'node:test' import assert from 'node:assert/strict' import esmock from 'esmock' +import esmockErr from '../../src/esmockErr.js' test('should throw error if !esmockloader', async () => { await assert.rejects(() => esmock('./to/module.js'), { - message: 'process must be started with --loader=esmock' + message: esmockErr.errMissingLoader().message }) }) From 0229e6039646adb03858019f992a053bb91cac83 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 11:30:56 -0700 Subject: [PATCH 3/6] do not encode test url --- src/esmockLoader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockLoader.js b/src/esmockLoader.js index cb780de3..7a0bdbf7 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -85,7 +85,7 @@ const load = async (url, context, nextLoad) => { return { format: 'module', shortCircuit: true, - responseURL: encodeURI(url), + responseURL: url, source: 'export default true' } } From 61b1aa5cc0d585f84b8dd0d5b30a37c9e83b1cf9 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 11:45:48 -0700 Subject: [PATCH 4/6] use fixed query string to verify loader --- src/esmockIsLoader.js | 4 +++- src/esmockLoader.js | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/esmockIsLoader.js b/src/esmockIsLoader.js index 106f05e9..37ef2d49 100644 --- a/src/esmockIsLoader.js +++ b/src/esmockIsLoader.js @@ -1,2 +1,4 @@ +import { loaderIsVerified } from './esmockLoader.js' + export default (cache => async () => typeof cache === 'boolean' ? cache - : cache = ((await import(`${import.meta.url}?test`)).default === true))() + : cache = await loaderIsVerified(import.meta.url))() diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 7a0bdbf7..e1aa55b7 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -80,8 +80,12 @@ const resolve = async (specifier, context, nextResolve) => { return resolved } +const loaderVerificationQuery = 'esmock-loader=true' +const loaderVerificationURLCreate = url => `${url}?${loaderVerificationQuery}` +const loaderIsVerified = async url => + (await import(loaderVerificationURLCreate(url))).default === true const load = async (url, context, nextLoad) => { - if (url.endsWith('esmockIsLoader.js?test')) { + if (url.endsWith(`esmockIsLoader.js?${loaderVerificationQuery}`)) { return { format: 'module', shortCircuit: true, @@ -122,4 +126,4 @@ const getSource = isLT1612 && load export * from './esmock.js' export {default} from './esmock.js' -export {load, resolve, getSource} +export {load, resolve, getSource, loaderIsVerified} From da7f1ae968a1e64998e74388e1d7756fe7b9d6b4 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 11:59:55 -0700 Subject: [PATCH 5/6] update verification detection --- src/esmockLoader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockLoader.js b/src/esmockLoader.js index e1aa55b7..74ca3ce1 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -85,7 +85,7 @@ const loaderVerificationURLCreate = url => `${url}?${loaderVerificationQuery}` const loaderIsVerified = async url => (await import(loaderVerificationURLCreate(url))).default === true const load = async (url, context, nextLoad) => { - if (url.endsWith(`esmockIsLoader.js?${loaderVerificationQuery}`)) { + if (url.endsWith(loaderVerificationQuery)) { return { format: 'module', shortCircuit: true, From 49340c39975669bbfa898a36551b7dbb8093d628 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 20:30:48 -0700 Subject: [PATCH 6/6] update changelog and isloader function --- CHANGELOG.md | 3 +++ src/esmockIsLoader.js | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5738a11f..da0f5e05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # changelog + * 2.0.7 _Oct.??.2022_ + * [use loader mechanism to detect](https://github.com/iambumblehead/esmock/pull/180) presence of esmock loader + * [detect and use import.meta.resolve,](https://github.com/iambumblehead/esmock/pull/179) when defined by host environment * 2.0.6 _Oct.14.2022_ * [show full path at error message,](https://github.com/iambumblehead/esmock/pull/170) making it easier to identify an invalid path * 2.0.5 _Oct.05.2022_ diff --git a/src/esmockIsLoader.js b/src/esmockIsLoader.js index 37ef2d49..2714ab45 100644 --- a/src/esmockIsLoader.js +++ b/src/esmockIsLoader.js @@ -1,4 +1,4 @@ import { loaderIsVerified } from './esmockLoader.js' -export default (cache => async () => typeof cache === 'boolean' ? cache - : cache = await loaderIsVerified(import.meta.url))() +export default (c => async () => + c || (c = await loaderIsVerified(import.meta.url)))()