@uibot/web-preview 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/document.d.ts +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +13 -13
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usability checks for a generated, self-contained HTML document (moved up from InfoOS App Build).
|
|
3
|
+
* String-based like the rest of this package: it reads source tags, not a parsed DOM.
|
|
4
|
+
*/
|
|
5
|
+
export type HtmlDocumentIssueCode = "not-a-document" | "missing-doctype" | "missing-title" | "empty-body" | "external-script";
|
|
6
|
+
export interface HtmlDocumentIssue {
|
|
7
|
+
code: HtmlDocumentIssueCode;
|
|
8
|
+
detail: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Problems that keep a generated document from working on its own in a sandboxed preview:
|
|
12
|
+
* not a complete `<html>`/`<body>` document (e.g. a truncated reply), no doctype, no title, nothing
|
|
13
|
+
* visible in the body, or a script loaded from another origin. Callers choose which codes block.
|
|
14
|
+
*/
|
|
15
|
+
export declare function assessHtmlDocument(html: string): HtmlDocumentIssue[];
|
package/dist/index.d.ts
CHANGED
|
@@ -6,5 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export { type AssembleInput, assembleErrorPage, assemblePreviewHtml, } from "./assemble.js";
|
|
8
8
|
export { detectProject, entryFromIndexHtml, findPreviewEntry, findProjectRoot, isBuildableFile, isProjectEntry, type PreviewKind, type ProjectInfo, } from "./detect.js";
|
|
9
|
+
export { assessHtmlDocument, type HtmlDocumentIssue, type HtmlDocumentIssueCode, } from "./document.js";
|
|
9
10
|
export { stripHtmlEnvelope } from "./html-envelope.js";
|
|
10
11
|
export { APP_SRCDOC_CSP, buildErrorBridgeScript, cspMetaTag, escapeAttr, escapeHtml, injectBeforeBodyEnd, injectBeforeHeadEnd, NETWORK_PREVIEW_CSP, PREVIEW_ERROR_MESSAGE, SANDBOX_LINK_SHIM_SCRIPT, SANDBOX_SHIM_SCRIPT, SANDBOX_STORAGE_SHIM_SCRIPT, STATIC_SRCDOC_CSP, STRICT_PREVIEW_CSP, } from "./srcdoc.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function
|
|
1
|
+
function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function y(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function H(t,e){let n=t.toLowerCase(),r=n.indexOf("<body"),s=r>=0?r:n.length,i=n.lastIndexOf("</head>",s-1);if(i>=0)return t.slice(0,i)+e+t.slice(i);if(/<head[^>]*>/i.test(t))return t.replace(/<head[^>]*>/i,(c)=>`${c}${e}`);if(/<html[^>]*>/i.test(t))return t.replace(/<html[^>]*>/i,(c)=>`${c}<head>${e}</head>`);return e+t}function N(t,e){let n=t.toLowerCase(),r=n.lastIndexOf("</html>"),s=r>=0?r:n.length,i=n.lastIndexOf("</body>",s-1);if(i>=0)return t.slice(0,i)+e+t.slice(i);if(/<body[^>]*>/i.test(t))return t.replace(/<body[^>]*>/i,(c)=>`${c}${e}`);return t+e}var x=`<script data-uibot-storage-shim>(function(){
|
|
2
2
|
function makeStore(){
|
|
3
3
|
var data = {};
|
|
4
4
|
var api = {
|
|
@@ -21,7 +21,7 @@ function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,"
|
|
|
21
21
|
}
|
|
22
22
|
tryShim('localStorage');
|
|
23
23
|
tryShim('sessionStorage');
|
|
24
|
-
})();</script>`,
|
|
24
|
+
})();</script>`,_=`<script data-uibot-link-shim>(function(){
|
|
25
25
|
document.addEventListener('click', function(e){
|
|
26
26
|
if (!e.target || !(e.target instanceof Element)) return;
|
|
27
27
|
var link = e.target.closest('a[href]');
|
|
@@ -43,7 +43,7 @@ function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,"
|
|
|
43
43
|
} catch (_) {}
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
|
-
})();</script>`,g=
|
|
46
|
+
})();</script>`,g=x+_,S="uibot:preview-error";function h(){return`<script data-uibot-error-bridge>(function(){
|
|
47
47
|
function show(msg){
|
|
48
48
|
try {
|
|
49
49
|
var bar = document.getElementById('__uibot_err__');
|
|
@@ -57,7 +57,7 @@ function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,"
|
|
|
57
57
|
}
|
|
58
58
|
bar.textContent = '预览运行时错误:\\n' + msg;
|
|
59
59
|
} catch (_) {}
|
|
60
|
-
try { if (window.parent && window.parent !== window) window.parent.postMessage({ type: ${JSON.stringify(
|
|
60
|
+
try { if (window.parent && window.parent !== window) window.parent.postMessage({ type: ${JSON.stringify(S)}, message: String(msg) }, '*'); } catch (_) {}
|
|
61
61
|
}
|
|
62
62
|
window.addEventListener('error', function(ev){
|
|
63
63
|
var m = ev && ev.error && ev.error.stack ? ev.error.stack : (ev && ev.message) || 'unknown error';
|
|
@@ -67,31 +67,31 @@ function a(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,"
|
|
|
67
67
|
var r = ev && ev.reason;
|
|
68
68
|
show(r && r.stack ? r.stack : String(r));
|
|
69
69
|
});
|
|
70
|
-
})();</script>`}var B="default-src 'none'; script-src 'none'; style-src 'unsafe-inline'; img-src data:; font-src data:; connect-src 'none'; frame-src 'none'",
|
|
70
|
+
})();</script>`}var B="default-src 'none'; script-src 'none'; style-src 'unsafe-inline'; img-src data:; font-src data:; connect-src 'none'; frame-src 'none'",M="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline' https:; img-src data: blob: https:; font-src data: https:; connect-src 'none'; frame-src 'none'",u="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; connect-src 'none'; frame-src 'none'",b="default-src 'none'; script-src 'unsafe-inline' https://esm.sh https://cdn.jsdelivr.net; style-src 'unsafe-inline' https:; img-src data: blob: https:; font-src data: https:; connect-src 'none'; frame-src 'none'";function d(t){return`<meta http-equiv="Content-Security-Policy" content="${y(t)}">`}var W="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4";function L(t){let{js:e,css:n="",importMap:r={},tailwind:s=!1,title:i="Preview",rootId:c="root",notices:l=[]}=t,o=Object.keys(r).length>0,T=o||s?b:u,j=o?`<script type="importmap">${JSON.stringify({imports:r})}</script>`:"",O=s?`<script src="${W}"></script>`:"",k=n?`<style>${n}</style>`:"",A=l.length?K(l):"",D=e.replace(/<\/(script)/gi,"<\\/$1").replace(/<!--/g,"<\\!--");return`<!doctype html>
|
|
71
71
|
<html lang="zh-CN">
|
|
72
72
|
<head>
|
|
73
73
|
<meta charset="utf-8" />
|
|
74
74
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
75
|
-
${
|
|
75
|
+
${d(T)}
|
|
76
76
|
<title>${a(i)}</title>
|
|
77
77
|
${g}
|
|
78
78
|
${h()}
|
|
79
|
+
${O}
|
|
79
80
|
${j}
|
|
80
|
-
${
|
|
81
|
-
${T}
|
|
81
|
+
${k}
|
|
82
82
|
</head>
|
|
83
83
|
<body>
|
|
84
|
-
${
|
|
84
|
+
${A}
|
|
85
85
|
<div id="${a(c)}"></div>
|
|
86
|
-
<script type="module">${
|
|
86
|
+
<script type="module">${D}</script>
|
|
87
87
|
</body>
|
|
88
|
-
</html>`}function
|
|
88
|
+
</html>`}function K(t){return`<div id="__uibot_notice__" style="font:12px/1.5 ui-sans-serif,system-ui;background:#fef3c7;color:#92400e;padding:6px 12px;border-bottom:1px solid #fde68a" onclick="this.remove()" title="点击关闭">⚠ ${t.map((n)=>a(n)).join(" · ")}</div>`}function V(t,e){let n=e?`${t}
|
|
89
89
|
|
|
90
90
|
${e}`:t;return`<!doctype html>
|
|
91
91
|
<html lang="zh-CN">
|
|
92
92
|
<head>
|
|
93
93
|
<meta charset="utf-8" />
|
|
94
|
-
${
|
|
94
|
+
${d(u)}
|
|
95
95
|
<title>预览构建失败</title>
|
|
96
96
|
</head>
|
|
97
97
|
<body style="margin:0;font:13px/1.6 ui-monospace,Menlo,monospace;background:#1f2937;color:#fca5a5">
|
|
@@ -100,4 +100,4 @@ ${p(d)}
|
|
|
100
100
|
<pre style="white-space:pre-wrap;margin:0;color:#fda4af">${a(n)}</pre>
|
|
101
101
|
</div>
|
|
102
102
|
</body>
|
|
103
|
-
</html>`}var
|
|
103
|
+
</html>`}var F=["src/main.tsx","src/main.jsx","src/main.ts","src/main.js","src/index.tsx","src/index.jsx","src/index.ts","src/index.js","src/routes/__root.tsx","src/routes/__root.jsx","main.tsx","main.jsx","index.tsx","index.jsx"],G=/(^|\/)tailwind\.config\.(?:js|ts|cjs|mjs)$/i,X=/(^|\/)vite\.config\.(?:js|ts|mjs|cjs|mts)$/i,z=/(^|\/)src\/routes\/__root\.(?:tsx|jsx|ts|js)$/i,J=/(^|\/)app\.config\.(?:js|ts|mjs)$/i;function w(t){let e=t.lastIndexOf("/");return e<0?"":t.slice(0,e)}function p(t,e){return t?`${t}/${e}`:e}function v(t,e){return e===""?!0:t===e||t.startsWith(`${e}/`)}function I(t,e){let n=new Set(e),r=w(t);for(;;){if(n.has(p(r,"package.json")))return r;if(r==="")return null;r=w(r)}}function m(t,e){let n=I(t,e);if(n===null)return null;let r=new Set(e),s=r.has(p(n,"index.html"))?p(n,"index.html"):null,i=null;for(let o of F){let f=p(n,o);if(r.has(f)){i=f;break}}if(!s&&!i)return null;let c=e.some((o)=>v(o,n)&&G.test(o)),l=e.some((o)=>v(o,n)&&(X.test(o)||z.test(o)||J.test(o)))?"dev-server":"html";return{root:n,entry:s??i,htmlEntry:s,codeEntry:i,needsTailwind:c,previewKind:l}}function q(t,e){return m(t,e)!==null}function P(t,e){let n=m(t,e);if(!n)return!1;return t===n.htmlEntry||t===n.codeEntry}function U(t){let e=t.find((n)=>P(n,t));if(e)return e;for(let n of t){let r=m(n,t);if(r)return r.entry}return t.find((n)=>/(^|\/)index\.html?$/i.test(n))??t.find((n)=>/\.html?$/i.test(n))??null}function Y(t){let n=/<script\b[^>]*\btype\s*=\s*["']module["'][^>]*\bsrc\s*=\s*["']([^"']+)["']/i.exec(t);if(!n){let s=/<script\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*\btype\s*=\s*["']module["']/i.exec(t);if(!s)return null;return E(s[1])}return E(n[1])}function E(t){if(/^(?:https?:)?\/\//i.test(t))return null;return t.replace(/^\.?\//,"")}var Z=/<(img|svg|canvas|video|input|textarea|button|select|iframe|picture)\b/i;function Q(t){return t.replace(/<!--[\s\S]*?-->/g,"")}function C(t){return/<body\b[^>]*>([\s\S]*?)<\/body\s*>/i.exec(t)?.[1]??null}function R(t){return t.replace(/<(script|style|template|noscript)\b[\s\S]*?<\/\1\s*>/gi," ").replace(/<[^>]+>/g," ").replace(/ /gi," ").replace(/\s+/g," ").trim()}function tt(t){let e=[];for(let n of t.matchAll(/<script\b([^>]*)>/gi)){let r=/\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(n[1]??""),s=(r?.[1]??r?.[2]??r?.[3]??"").trim();if(/^(?:https?:)?\/\//i.test(s))e.push(s)}return e}function et(t){let e=Q(t),n=[];if(!/<html\b[^>]*>[\s\S]*<\/html\s*>\s*$/i.test(e.trim())||C(e)===null)n.push({code:"not-a-document",detail:"expected a complete <html> document with <body>…</body>"});if(!/^\s*<!doctype\s+html\b/i.test(e))n.push({code:"missing-doctype",detail:"missing <!doctype html>"});let r=/<title\b[^>]*>([\s\S]*?)<\/title\s*>/i.exec(e)?.[1];if(!r||!R(r))n.push({code:"missing-title",detail:"missing <title>"});let s=C(e);if(s!==null&&!R(s)&&!Z.test(s))n.push({code:"empty-body",detail:"the body has no visible content"});for(let i of tt(e))n.push({code:"external-script",detail:`script loaded from ${i}`});return n}function nt(t){let e=t.trim(),r=/^```[a-zA-Z]*\n([\s\S]*?)\n?```$/.exec(e);if(r?.[1])e=r[1].trim();if(e.startsWith("<![CDATA[")&&e.endsWith("]]>"))e=e.slice(9,-3).trim();return e}export{M as APP_SRCDOC_CSP,b as NETWORK_PREVIEW_CSP,S as PREVIEW_ERROR_MESSAGE,_ as SANDBOX_LINK_SHIM_SCRIPT,g as SANDBOX_SHIM_SCRIPT,x as SANDBOX_STORAGE_SHIM_SCRIPT,B as STATIC_SRCDOC_CSP,u as STRICT_PREVIEW_CSP,V as assembleErrorPage,L as assemblePreviewHtml,et as assessHtmlDocument,h as buildErrorBridgeScript,d as cspMetaTag,m as detectProject,Y as entryFromIndexHtml,y as escapeAttr,a as escapeHtml,U as findPreviewEntry,I as findProjectRoot,N as injectBeforeBodyEnd,H as injectBeforeHeadEnd,q as isBuildableFile,P as isProjectEntry,nt as stripHtmlEnvelope};
|