-
Notifications
You must be signed in to change notification settings - Fork 25
/
quibble.mjs
136 lines (112 loc) · 3.55 KB
/
quibble.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import quibble from './quibble.js'
export default quibble
export const reset = quibble.reset
export const ignoreCallsFromThisFile = quibble.ignoreCallsFromThisFile
export const config = quibble.config
export const isLoaderLoaded = quibble.isLoaderLoaded
function loadQuibble () {
global.__quibble = { stubModuleGeneration: 1 }
}
export function globalPreload () {
return `(${loadQuibble.toString()})()`
}
export function getGlobalPreloadCode () {
return `(${loadQuibble.toString()})()`
}
export async function resolve (specifier, context, nextResolve) {
const resolve = () => nextResolve(
specifier.includes('__quibble')
? specifier.replace('?__quibbleresolvepath', '').replace('?__quibbleoriginal', '')
: specifier,
context
)
if (specifier.includes('__quibbleresolvepath')) {
const resolvedPath = new URL((await resolve()).url).pathname
const error = new Error()
error.code = 'QUIBBLE_RESOLVED_PATH'
error.resolvedPath = resolvedPath
throw error
}
if (!global.__quibble.quibbledModules) {
return resolve()
}
if (specifier === 'quibble') {
return resolve()
}
const stubModuleGeneration = global.__quibble.stubModuleGeneration
if (specifier.includes('__quibbleoriginal')) {
return resolve()
}
const { parentURL } = context
try {
const { url } = await resolve()
const quibbledUrl = `${url}?__quibble=${stubModuleGeneration}`
// 'node:' is the prefix for Node >=14.13. 'nodejs:' is for earlier versions
if (/^node(js){0,1}:/.test(url) && !getStubsInfo(new URL(quibbledUrl))) return { url }
return { url: quibbledUrl }
} catch (error) {
if (error.code === 'ERR_MODULE_NOT_FOUND') {
return {
url: parentURL
? `${new URL(specifier, parentURL).href}?__quibble=${stubModuleGeneration}`
: new URL(specifier).href
}
} else { throw error }
}
}
export async function getFormat (url, context, defaultGetFormat) {
if (getStubsInfo(new URL(url))) {
return {
format: 'module'
}
} else {
return defaultGetFormat(url, context, defaultGetFormat)
}
}
export async function getSource (url, context, defaultGetSource) {
const stubsInfo = getStubsInfo(new URL(url))
return stubsInfo
? { source: transformModuleSource(stubsInfo) }
: defaultGetSource(url, context, defaultGetSource)
}
/** @param {URL} moduleUrl */
function getStubsInfo (moduleUrl) {
if (!global.__quibble.quibbledModules) return undefined
if (!moduleUrl.searchParams.get('__quibble')) return undefined
const moduleFilepath = moduleUrl.pathname
const stub = global.__quibble.quibbledModules.get(moduleFilepath)
return stub ? [moduleFilepath, stub] : undefined
}
function transformModuleSource ([moduleKey, stubs]) {
return `
${Object.keys(stubs.namedExportStubs || {})
.map(
(name) =>
`export let ${name} = global.__quibble.quibbledModules.get(${JSON.stringify(
moduleKey
)}).namedExportStubs["${name}"]`
)
.join(';\n')};
${
stubs.defaultExportStub
? `export default global.__quibble.quibbledModules.get(${JSON.stringify(
moduleKey
)}).defaultExportStub;`
: ''
}
`
}
/**
* @param {string} url
* @param {{
* format: string,
* }} context
* @param {Function} nextLoad
* @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array), format: string}>}
*/
export async function load (url, context, nextLoad) {
const stubsInfo = getStubsInfo(new URL(url))
return stubsInfo
? { source: transformModuleSource(stubsInfo), format: 'module', shortCircuit: true }
: nextLoad(url, context)
}