* Generate the source code for a mocked module. * @param {string} encodedTargetURL the module being mocked * @returns {string}
(encodedTargetURL)
| 107 | * @returns {string} |
| 108 | */ |
| 109 | function 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 | } |