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

use loader mechanism to detect presence of loader #180

Merged
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.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_
Expand Down
7 changes: 4 additions & 3 deletions src/esmock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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) => {
if (!esmockIsLoader())
throw new Error('process must be started with --loader=esmock')

const [moduleId, parent, defs, gdefs, opt] = esmockArgs(args, opts)
if (!(await esmockIsLoader()))
iambumblehead marked this conversation as resolved.
Show resolved Hide resolved
throw esmockErr.errMissingLoader()

const fileURLKey = await esmockModule(moduleId, parent, defs, gdefs, opt)
const importedModule = await import(fileURLKey)

Expand Down
7 changes: 6 additions & 1 deletion src/esmockErr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions src/esmockIsLoader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const isloaderRe = /--(experimental-)?loader[=\s,"']*esmock/
import { loaderIsVerified } from './esmockLoader.js'

export default (pr = process) =>
isloaderRe.test(pr.execArgv) ||
isloaderRe.test(pr.env.NODE_OPTIONS)
export default (c => async () =>
c || (c = await loaderIsVerified(import.meta.url)))()
15 changes: 14 additions & 1 deletion src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ 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(loaderVerificationQuery)) {
return {
format: 'module',
shortCircuit: true,
responseURL: url,
source: 'export default true'
}
}

if (esmkdefsRe.test(url)) // parent of mocked modules
return nextLoad(url, context)

Expand Down Expand Up @@ -113,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}
3 changes: 2 additions & 1 deletion tests/tests-no-loader/esmock.noloader.test.js
Original file line number Diff line number Diff line change
@@ -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
})
})