|
| 1 | +import * as recast from "recast"; |
| 2 | +import { ESNode, ParsedFileNode } from "../types"; |
| 3 | +import { ProxifiedModule } from "./types"; |
| 4 | +import { createProxy, literalToAst, proxify } from "./_utils"; |
| 5 | + |
| 6 | +export function proxifyModule<T>(ast: ParsedFileNode): ProxifiedModule<T> { |
| 7 | + const root = ast.program as ESNode; |
| 8 | + if (root.type !== "Program") { |
| 9 | + throw new Error(`Cannot proxify ${ast.type} as module`); |
| 10 | + } |
| 11 | + |
| 12 | + const findExport = (key: string) => { |
| 13 | + const type = |
| 14 | + key === "default" ? "ExportDefaultDeclaration" : "ExportNamedDeclaration"; |
| 15 | + |
| 16 | + for (const n of root.body) { |
| 17 | + if (n.type === type) { |
| 18 | + if (key === "default") { |
| 19 | + return n.declaration; |
| 20 | + } |
| 21 | + if (n.declaration && "declarations" in n.declaration) { |
| 22 | + const dec = n.declaration.declarations[0]; |
| 23 | + if ("name" in dec.id && dec.id.name === key) { |
| 24 | + return dec.init as any; |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + const updateOrAddExport = (key: string, value: any) => { |
| 32 | + const type = |
| 33 | + key === "default" ? "ExportDefaultDeclaration" : "ExportNamedDeclaration"; |
| 34 | + |
| 35 | + const node = literalToAst(value) as any; |
| 36 | + for (const n of root.body) { |
| 37 | + if (n.type === type) { |
| 38 | + if (key === "default") { |
| 39 | + n.declaration = node; |
| 40 | + return; |
| 41 | + } |
| 42 | + if (n.declaration && "declarations" in n.declaration) { |
| 43 | + const dec = n.declaration.declarations[0]; |
| 44 | + if ("name" in dec.id && dec.id.name === key) { |
| 45 | + dec.init = node; |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + root.body.push( |
| 53 | + key === "default" |
| 54 | + ? recast.types.builders.exportDefaultDeclaration(node) |
| 55 | + : (recast.types.builders.exportNamedDeclaration( |
| 56 | + recast.types.builders.variableDeclaration("const", [ |
| 57 | + recast.types.builders.variableDeclarator( |
| 58 | + recast.types.builders.identifier(key), |
| 59 | + node |
| 60 | + ), |
| 61 | + ]) |
| 62 | + ) as any) |
| 63 | + ); |
| 64 | + }; |
| 65 | + |
| 66 | + const exportsProxy = createProxy( |
| 67 | + root, |
| 68 | + {}, |
| 69 | + { |
| 70 | + get(_, prop) { |
| 71 | + const node = findExport(prop as string); |
| 72 | + if (node) { |
| 73 | + return proxify(node); |
| 74 | + } |
| 75 | + }, |
| 76 | + set(_, prop, value) { |
| 77 | + updateOrAddExport(prop as string, value); |
| 78 | + return true; |
| 79 | + }, |
| 80 | + deleteProperty(_, prop) { |
| 81 | + const type = |
| 82 | + prop === "default" |
| 83 | + ? "ExportDefaultDeclaration" |
| 84 | + : "ExportNamedDeclaration"; |
| 85 | + |
| 86 | + for (let i = 0; i < root.body.length; i++) { |
| 87 | + const n = root.body[i]; |
| 88 | + if (n.type === type) { |
| 89 | + if (prop === "default") { |
| 90 | + root.body.splice(i, 1); |
| 91 | + return true; |
| 92 | + } |
| 93 | + if (n.declaration && "declarations" in n.declaration) { |
| 94 | + const dec = n.declaration.declarations[0]; |
| 95 | + if ("name" in dec.id && dec.id.name === prop) { |
| 96 | + root.body.splice(i, 1); |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return false; |
| 103 | + }, |
| 104 | + } |
| 105 | + ); |
| 106 | + |
| 107 | + return { |
| 108 | + $ast: root, |
| 109 | + $type: "module", |
| 110 | + exports: exportsProxy, |
| 111 | + get imports() { |
| 112 | + throw new Error("Not implemented"); |
| 113 | + }, |
| 114 | + } as any; |
| 115 | +} |
0 commit comments