-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from iambumblehead/add-support-for-initialize…
…-hook add support for initialize hook
- Loading branch information
Showing
19 changed files
with
137 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import module from 'node:module' | ||
import threads from 'node:worker_threads' | ||
|
||
const channel = threads.MessageChannel | ||
&& new threads.MessageChannel() | ||
|
||
const register = (res => () => { | ||
if (typeof res === 'boolean') | ||
return res | ||
|
||
if ((res = Boolean(module.register))) { | ||
module.register('./esmockLoader.js', { | ||
parentURL: import.meta.url, | ||
data: { port: channel.port2 }, | ||
transferList: [channel.port2] | ||
}) | ||
} | ||
|
||
return res | ||
})() | ||
|
||
export default Object.assign(msg => { | ||
if (register()) { | ||
channel.port1.postMessage(msg) | ||
} else if (typeof global.postMessageEsmk === 'function') { | ||
global.postMessageEsmk(msg) | ||
} | ||
}, { register }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import test from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import module from 'node:module' | ||
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: esmockErr.errMissingLoader().message | ||
if (!module.register) { | ||
test('should throw error if !esmockloader', async () => { | ||
await assert.rejects(() => esmock('./to/module.js'), { | ||
message: esmockErr.errMissingLoader().message | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
// node version 20.6+ do not need --loader | ||
if (module.register) { | ||
test('should mock a module', async () => { | ||
const main = await esmock('../local/mainUtil.js', { | ||
'form-urlencoded': () => 'mock encode' | ||
}) | ||
|
||
assert.strictEqual(typeof main, 'function') | ||
assert.strictEqual(main.createString(), 'mock encode') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters