UNPKG

snowy-designer

Version:

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

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