UNPKG

snowy-designer

Version:

基于Epic-Designer-Pro版本的设计器,可视化开发页面表单

201 lines (199 loc) 5.94 kB
import { MagicString as b, babelParse as F, extractIdentifiers as M, walkIdentifiers as E, isStaticProperty as N, isInDestructureAssignment as S, walk as C } from "vue/compiler-sfc"; function A(n, r = !1) { const l = /* @__PURE__ */ new Set(), s = []; if (v(n, n.files[n.mainFile], s, l, r), !r) { for (const i in n.files) if (i.endsWith(".css")) { const a = n.files[i]; l.has(a) || s.push( ` window.__css__.push(${JSON.stringify(a.compiled.css)})` ); } } return s; } const w = "__modules__", h = "__export__", O = "__dynamic_import__", _ = "__module__"; function v(n, r, l, s, i) { if (s.has(r)) return []; if (s.add(r), !i && r.filename.endsWith(".html")) return k(n, r.code, r.filename, l, s); let { code: a, hasDynamicImport: d, importedFiles: f } = x( n, i ? r.compiled.ssr : r.compiled.js, r.filename ); D( n, f, d, l, s, i ), r.compiled.css && !i && (a += ` __module__.__css__=${JSON.stringify(r.compiled.css)} return __module__`), l.push(a); } function D(n, r, l, s, i, a) { if (l) for (const d of Object.values(n.files)) i.has(d) || v(n, d, s, i, a); else if (r.size > 0) for (const d of r) v(n, n.files[d], s, i, a); } function x(n, r, l) { const s = new b(r), i = F(r, { sourceFilename: l, sourceType: "module" }).program.body, a = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Set(), f = /* @__PURE__ */ new Set(), y = /* @__PURE__ */ new Map(); function m(e) { const o = n.files; let t = e; return o[t] || o[t = `${e}.ts`] || o[t = `${e}.js`] ? t : void 0; } function u(e, o) { const t = m(o.replace(/^\.\/+/, "src/")); if (!t) throw new Error(`File "${o}" does not exist.`); if (f.has(t)) return y.get(t); f.add(t); const c = `__import_${f.size}__`; return y.set(t, c), s.appendLeft( e.start ?? 0, `const ${c} = ${w}[${JSON.stringify(t)}] ` ), c; } function p(e, o = e) { s.append(` ${h}(${_}, "${e}", () => ${o})`); } s.prepend( `const ${_} = ${w}[${JSON.stringify( l )}] = { [Symbol.toStringTag]: "Module" } ` ); for (const e of i) if (e.type === "ImportDeclaration" && e.source.value.startsWith("./")) { const t = u(e, e.source.value); for (const c of e.specifiers) c.type === "ImportSpecifier" ? a.set( c.local.name, `${t}.${c.imported.name}` ) : c.type === "ImportDefaultSpecifier" ? a.set(c.local.name, `${t}.default`) : t && a.set(c.local.name, t); e.start && e.end && s.remove(e.start, e.end); } for (const e of i) { if (e.type === "ExportNamedDeclaration") if (e.declaration) { if (e.declaration.type === "FunctionDeclaration" || e.declaration.type === "ClassDeclaration") e.declaration.id && p(e.declaration.id.name); else if (e.declaration.type === "VariableDeclaration") for (const o of e.declaration.declarations) for (const t of M(o.id)) p(t.name); e.start && e.declaration.start && s.remove(e.start, e.declaration.start); } else if (e.source) { const o = u(e, e.source.value); for (const t of e.specifiers) p( t.exported.name, `${o}.${t.local.name}` ); e.start && e.end && s.remove(e.start, e.end); } else { for (const o of e.specifiers) { const t = o.local.name, c = a.get(t); p(o.exported.name, c || t); } e.start && e.end && s.remove(e.start, e.end); } if (e.type === "ExportDefaultDeclaration") if ("id" in e.declaration && e.declaration.id) { const { name: o } = e.declaration.id; e.start && s.remove(e.start, e.start + 15), s.append(` ${h}(${_}, "default", () => ${o})`); } else e.start && s.overwrite(e.start, e.start + 14, `${_}.default =`); if (e.type === "ExportAllDeclaration") { const o = u(e, e.source.value); e.start && e.end && s.remove(e.start, e.end), s.append(` for (const key in ${o}) { if (key !== 'default') { ${h}(${_}, key, () => ${o}[key]) } }`); } } for (const e of i) e.type !== "ImportDeclaration" && E(e, (o, t, c) => { const g = a.get(o.name); if (g) if (t && N(t) && t.shorthand) (!t.inPattern || S(t, c)) && o.end && s.appendLeft(o.end, `: ${g}`); else if (t && t.type === "ClassDeclaration" && o === t.superClass) { if (!d.has(o.name)) { d.add(o.name); const I = c[1]; I.start && s.prependRight(I.start, `const ${o.name} = ${g}; `); } } else o.start && o.end && s.overwrite(o.start, o.end, g); }); let $ = !1; return C(i, { enter(e, o) { if (e.type === "Import" && o.type === "CallExpression") { const t = o.arguments[0]; t.type === "StringLiteral" && t.value.startsWith("./") && ($ = !0, e.start && s.overwrite(e.start, e.start + 6, O), t.start && t.end && s.overwrite( t.start, t.end, JSON.stringify(t.value.replace(/^\.\/+/, "src/")) )); } } }), { code: s.toString(), hasDynamicImport: $, importedFiles: f }; } const J = /<script\b(?:\s[^>]*>|>)([\s\S]*?)<\/script>/gi, j = /<script\b[^>]*type\s*=\s*(?:"module"|'module')[^>]*>([\s\S]*?)<\/script>/gi; function k(n, r, l, s, i) { const a = []; let d = ""; const f = r.replaceAll(j, (y, m) => { const { code: u, hasDynamicImport: p, importedFiles: $ } = x( n, m, l ); return D( n, $, p, a, i, !1 ), d += ` ${u}`, ""; }).replaceAll(J, (y, m) => (d += ` ${m}`, "")); s.push( `document.body.innerHTML = ${JSON.stringify(f)}`, ...a, d ); } export { A as compileModulesForPreview };