MCPcopy Create free account
hub / github.com/nodejs/node / generateModule

Function generateModule

test/fixtures/es-module-loaders/mock-loader.mjs:109–133  ·  view source on GitHub ↗

* Generate the source code for a mocked module. * @param {string} encodedTargetURL the module being mocked * @returns {string}

(encodedTargetURL)

Source from the content-addressed store, hash-verified

107 * @returns {string}
108 */
109function generateModule(encodedTargetURL) {
110 const exports = mockedModuleExports.get(
111 decodeURIComponent(encodedTargetURL)
112 );
113 let body = [
114 `import { mockedModules } from ${JSON.stringify(mainImportURL)};`,
115 'export {};',
116 'let mapping = {__proto__: null};',
117 `const mock = mockedModules.get(${JSON.stringify(encodedTargetURL)});`,
118 ];
119 for (const [i, name] of Object.entries(exports)) {
120 let key = JSON.stringify(name);
121 body.push(`var _${i} = mock.namespace[${key}];`);
122 body.push(`Object.defineProperty(mapping, ${key}, { enumerable: true, set(v) {_${i} = v;}, get() {return _${i};} });`);
123 body.push(`export {_${i} as ${name}};`);
124 }
125 body.push(`mock.listeners.push(${
126 () => {
127 for (var k in mapping) {
128 mapping[k] = mock.namespace[k];
129 }
130 }
131 });`);
132 return body.join('\n');
133}

Callers 1

loadFunction · 0.85

Calls 4

getMethod · 0.65
entriesMethod · 0.45
pushMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected