@adep/web-container 0.2.3 → 0.2.4

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.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * IDE 预览「控制台中继」脚本:内联**经典**脚本,注入预览文档 `<head>` 首位,先于任何 module
3
+ * 脚本执行(module script 天然 defer,故必然先就位)。
4
+ *
5
+ * 职责:劫持 `console.*`(log / info / warn / error / debug)与 window 的 `error` /
6
+ * `unhandledrejection`,以 `{ source: 'adep-ide-preview', kind: 'console', level, text }` 信封
7
+ * `postMessage` 给父窗口,由 IDE 的 `FrontendPreview.onMessage` 原样消费(注入「控制台」页签)。
8
+ *
9
+ * **同步纪律**:本字符串与 `packages/vite-plugin/src/ide-proxy.ts` 的 `CONSOLE_RELAY_SCRIPT`
10
+ * (单一真相源,Nodebox 路径经 `@adep/vite-plugin` 消费)必须**逐字一致**——两张副本、无共享
11
+ * import(预览 iframe 与浏览器内 vite-dev 是两个独立加载上下文,共享源码会引入打包耦合),
12
+ * 与 `FN_FETCH_INTERCEPTOR_SCRIPT` 同款处理。改这里务必同步另一侧;
13
+ * 机检见 `app/composables/__tests__/preview-inject-scripts.test.ts`(两份副本逐字比对 + 经典脚本语法门)。
14
+ *
15
+ * 边界:
16
+ * - `window.parent === window`(预览被直接打开 / 分享链接场景)直接不发——没有父窗口可回传;
17
+ * - 序列化**不走裸 `JSON.stringify`**:`Error` 取 `stack`(含嵌在对象里的 `cause`——vue-router 的
18
+ * `VUE_ROUTER_R0120({ cause })` 正是这形态,裸 stringify 会把它压成 `{"cause":{}}` 而丢掉根因)、
19
+ * 函数 / `bigint` / 循环引用各有可读形态;整体失败才退化为 `String(value)`,绝不因「记日志本身」
20
+ * 把页面搞崩;postMessage 失败同样静默(父窗口可能已关闭或拒绝该来源);
21
+ * - 必须是**纯经典脚本语法**:本包把它内联进 `<script>`,出现 `import` / `export` 会让浏览器
22
+ * 报 `SyntaxError`(见 AGENTS.md §9 的 `Unexpected token 'export'` 一例)。
23
+ */
24
+ export declare const CONSOLE_RELAY_SCRIPT: string;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * CSS `@import` 内联(浏览器内 vite 的编译期 CSS 管线)。
3
+ *
4
+ * 为什么必须有这一层:真 Vite 的 CSS 管线(postcss-import)在**编译期**按文件解析 `@import`
5
+ * 并把内容合进来;浏览器内 vite 原先只把 CSS 文本逐字注入 `<style>`,`@import` 于是留给浏览器
6
+ * 按**文档 base** 去解析——预览面上那个 URL 是「未命中 → SPA 回退入口文档」,浏览器拿到
7
+ * `text/html` 只能整条丢弃;而 SFC 的 `<style>` 块里常常**只有**这条 `@import`(全局样式全在它指向
8
+ * 的文件里),于是整份样式表失效,且**纯 CSS 层的失败不产生任何 JS 异常**(页面照常渲染、
9
+ * 控制台干净)。IDE-038 之前的现场即如此。
10
+ *
11
+ * 语义对齐 postcss-import 的核心部分(本包零第三方依赖,故自实现):
12
+ * - 支持 `@import "x"` / `@import 'x'` / `@import url(x)` / `@import url("x")` 四种形态;
13
+ * - **注释与字符串感知**:注释块内的 `@import` 与字符串内的 `@import`(如 `content: "@import"`)一律不动;
14
+ * - **条件保留**:`layer` / `layer(name)` / `supports(…)` / 媒体查询按等价 at-rule 包裹;
15
+ * - **重复与环**:同一次展开内同一文件只内联一次(postcss-import 同口径),环据此自然终止;
16
+ * - 被内联文件的 `@charset` / BOM 剥掉(内联后不在首行,留着是非法 CSS)。
17
+ *
18
+ * 解析失败**分级**处置(避免把静默失败换成误红屏):
19
+ * - **外部说明符**(`http(s)://` / `//` / `data:` / `blob:`)⇒ 原样保留,浏览器自己会取;
20
+ * - **本地路径**(`./` `../` `/`)解析不到 ⇒ 项目里确实没有这个文件,抛 `CssImportError`
21
+ * (调用方转成红屏,消息含说明符与导入方路径)——静默丢整份样式正是本模块要根除的问题;
22
+ * - **裸说明符**(`@import 'tailwindcss'`)解析不到 ⇒ 原样保留:第三方包的 CSS 入口布局超出
23
+ * 本包 resolver 的能力,不得据此判死整个预览。
24
+ *
25
+ * 边界:不做 CSS 内的相对 `url()` 资源改写(需资产管线),不支持预处理器(非 `.css` 目标一律
26
+ * 原样保留:`compileStyle` 本就不预处理,本模块不替它做半套)。
27
+ */
28
+ /** 导入解析契约(VFS 侧适配由调用方注入:`vite-dev` 与 `preview-server` 语义不同,各给各的)。 */
29
+ export interface CssImportResolver {
30
+ /** 读文件内容;不存在 / 是目录返回 null。 */
31
+ read(abs: string): string | null;
32
+ /** 说明符 → VFS 绝对路径;解析不到返回 null(**不抛**——分级处置归本模块)。 */
33
+ resolve(spec: string, importerAbs: string): string | null;
34
+ }
35
+ /** 本地路径的 `@import` 解析不到时抛出(裸说明符不抛,见文件头)。 */
36
+ export declare class CssImportError extends Error {
37
+ constructor(message: string);
38
+ }
39
+ /**
40
+ * 把 `css` 里可解析的 `@import` 全部内联(编译期),其余原样保留。
41
+ *
42
+ * `importerAbs` 是 `css` 所属文件的 VFS 绝对路径——相对说明符按它所在目录解析(对齐
43
+ * postcss-import 以文件为基准的语义;SFC 的 `<style>` 块传该 `.vue` 文件路径)。
44
+ */
45
+ export declare function inlineCssImports(css: string, importerAbs: string, resolver: CssImportResolver): string;
package/dist/index.d.ts CHANGED
@@ -46,4 +46,5 @@ export * from './offline';
46
46
  export * from './sim';
47
47
  export { FN_FETCH_INTERCEPTOR_SCRIPT, createFunctionFetchHandler } from './function-fetch-proxy';
48
48
  export type { FunctionFetchHandler, FunctionFetchHandlerOptions, OfflineRunner, } from './function-fetch-proxy';
49
+ export { CONSOLE_RELAY_SCRIPT } from './console-relay';
49
50
  export type { WebContainer, WebContainerDirectoryTree, WebContainerEventMap, WebContainerFsEntry, WebContainerProcess, } from '@adep/types';