|
| 1 | +/** quotes strings */ |
| 2 | +const q = JSON.stringify; |
| 3 | + |
| 4 | +const exportsCellRecord = exportMap => |
| 5 | + ''.concat( |
| 6 | + ...Object.keys(exportMap).map( |
| 7 | + exportName => `\ |
| 8 | + ${exportName}: cell(${q(exportName)}), |
| 9 | +`, |
| 10 | + ), |
| 11 | + ); |
| 12 | + |
| 13 | +const importsCellSetter = (exportMap, index) => |
| 14 | + ''.concat( |
| 15 | + ...Object.entries(exportMap).map( |
| 16 | + ([exportName, [importName]]) => `\ |
| 17 | + ${importName}: cells[${index}].${exportName}.set, |
| 18 | +`, |
| 19 | + ), |
| 20 | + ); |
| 21 | + |
| 22 | +const adaptReexport = reexportMap => { |
| 23 | + if (!reexportMap) { |
| 24 | + return {}; |
| 25 | + } |
| 26 | + const ret = Object.fromEntries( |
| 27 | + Object.values(reexportMap) |
| 28 | + .flat() |
| 29 | + .map(([local, exported]) => [exported, [local]]), |
| 30 | + ); |
| 31 | + return ret; |
| 32 | +}; |
| 33 | + |
| 34 | +export const runtime = `\ |
| 35 | +function observeImports(map, importName, importIndex) { |
| 36 | + for (const [name, observers] of map.get(importName)) { |
| 37 | + const cell = cells[importIndex][name]; |
| 38 | + if (cell === undefined) { |
| 39 | + throw new ReferenceError(\`Cannot import name \${name}\`); |
| 40 | + } |
| 41 | + for (const observer of observers) { |
| 42 | + cell.observe(observer); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +`; |
| 47 | + |
| 48 | +export default { |
| 49 | + runtime, |
| 50 | + getBundlerKit({ |
| 51 | + index, |
| 52 | + indexedImports, |
| 53 | + record: { |
| 54 | + __syncModuleProgram__, |
| 55 | + __fixedExportMap__ = {}, |
| 56 | + __liveExportMap__ = {}, |
| 57 | + __reexportMap__ = {}, |
| 58 | + reexports, |
| 59 | + }, |
| 60 | + }) { |
| 61 | + return { |
| 62 | + getFunctor: () => `\ |
| 63 | +// === functors[${index}] === |
| 64 | +${__syncModuleProgram__}, |
| 65 | +`, |
| 66 | + getCells: () => `\ |
| 67 | + { |
| 68 | +${exportsCellRecord(__fixedExportMap__)}${exportsCellRecord( |
| 69 | + __liveExportMap__, |
| 70 | + )}${exportsCellRecord(adaptReexport(__reexportMap__))}\ |
| 71 | + }, |
| 72 | +`, |
| 73 | + getReexportsWiring: () => { |
| 74 | + const mappings = reexports.map( |
| 75 | + importSpecifier => `\ |
| 76 | + Object.defineProperties(cells[${index}], Object.getOwnPropertyDescriptors(cells[${indexedImports[importSpecifier]}])); |
| 77 | +`, |
| 78 | + ); |
| 79 | + // Create references for export name as newname |
| 80 | + const namedReexportsToProcess = Object.entries(__reexportMap__); |
| 81 | + if (namedReexportsToProcess.length > 0) { |
| 82 | + mappings.push(` |
| 83 | + Object.defineProperties(cells[${index}], {${namedReexportsToProcess.map( |
| 84 | + ([specifier, renames]) => { |
| 85 | + return renames.map( |
| 86 | + ([localName, exportedName]) => |
| 87 | + `${q(exportedName)}: { value: cells[${ |
| 88 | + indexedImports[specifier] |
| 89 | + }][${q(localName)}] }`, |
| 90 | + ); |
| 91 | + }, |
| 92 | + )} }); |
| 93 | +`); |
| 94 | + } |
| 95 | + return mappings.join(''); |
| 96 | + }, |
| 97 | + getFunctorCall: () => `\ |
| 98 | + functors[${index}]({ |
| 99 | + imports(entries) { |
| 100 | + const map = new Map(entries); |
| 101 | + ${''.concat( |
| 102 | + ...Object.entries(indexedImports).map( |
| 103 | + ([importName, importIndex]) => `\ |
| 104 | + observeImports(map, ${q(importName)}, ${importIndex}); |
| 105 | + `, |
| 106 | + ), |
| 107 | + )}\ |
| 108 | + }, |
| 109 | + liveVar: { |
| 110 | + ${importsCellSetter(__liveExportMap__, index)}\ |
| 111 | + }, |
| 112 | + onceVar: { |
| 113 | +${importsCellSetter(__fixedExportMap__, index)}\ |
| 114 | + }, |
| 115 | + importMeta: {}, |
| 116 | + }); |
| 117 | +`, |
| 118 | + }; |
| 119 | + }, |
| 120 | +}; |
0 commit comments