@deot/docs-playground 1.1.4 → 1.1.5

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.
@@ -1,432 +0,0 @@
1
- const require_rolldown_runtime = require("./rolldown-runtime-C0BPl7ul.cjs");
2
- const require_store = require("./store-DKawhFZu.cjs");
3
- let vue = require("vue");
4
- vue = require_rolldown_runtime.__toESM(vue);
5
- let _deot_vc = require("@deot/vc");
6
- let vue_router = require("vue-router");
7
- vue_router = require_rolldown_runtime.__toESM(vue_router);
8
- //#region packages/playground/src/core/runtime/local/compile/css.ts
9
- var collectCss = (chunks) => {
10
- const result = [];
11
- chunks.forEach((chunk) => {
12
- const css = String(chunk || "").trim();
13
- if (css) result.push(css);
14
- });
15
- return result;
16
- };
17
- var injectStyle = (id, cssList) => {
18
- /* istanbul ignore if -- 仅浏览器注入样式 */
19
- if (typeof document === "undefined") return null;
20
- document.querySelector(`style[data-playground-id="${id}"]`)?.remove();
21
- const css = cssList.filter(Boolean).join("\n");
22
- if (!css) return null;
23
- const style = document.createElement("style");
24
- style.setAttribute("data-playground-id", id);
25
- style.textContent = css;
26
- document.head.appendChild(style);
27
- return style;
28
- };
29
- var removeStyle = (id) => {
30
- /* istanbul ignore if -- 仅浏览器注入样式 */
31
- if (typeof document === "undefined") return;
32
- document.querySelector(`style[data-playground-id="${id}"]`)?.remove();
33
- };
34
- //#endregion
35
- //#region packages/playground/src/core/runtime/local/playground.ts
36
- /**
37
- * 直接渲染与页面同文档,弹层不需要临时撑高。
38
- * `run` 仍返回可调用函数,但不会申请高度,也不会改动预览尺寸。
39
- * @param vue 宿主 Vue,只用其中的 `isRef`。
40
- * @returns 注入给预览的 `docs:playground`。
41
- */
42
- var createLocalPlayground = (vue) => {
43
- const run = (_height, optionsOrHandler, lastHandler) => {
44
- const visible = (typeof optionsOrHandler === "function" || optionsOrHandler == null ? {} : optionsOrHandler).visible;
45
- const handler = typeof optionsOrHandler === "function" ? optionsOrHandler : typeof lastHandler === "function" ? lastHandler : vue.isRef(visible) ? () => {
46
- visible.value = true;
47
- } : void 0;
48
- return (...args) => {
49
- if (typeof handler !== "function") return void 0;
50
- return handler(...args);
51
- };
52
- };
53
- return {
54
- enable: () => Promise.resolve(true),
55
- disable() {},
56
- dispose() {},
57
- run
58
- };
59
- };
60
- //#endregion
61
- //#region packages/playground/src/core/runtime/local/port.ts
62
- /** 只在桥接 iframe 内部转发,保证父页面收到的 source 是该 iframe。 */
63
- var LOCAL_BRIDGE_CHANNEL = "docs:local-out";
64
- var BRIDGE_INSTALLER = `
65
- if (!window.__docsLocalBridge) {
66
- window.__docsLocalBridge = true;
67
- window.addEventListener('message', function (event) {
68
- var data = event.data;
69
- if (!data || data.channel !== ${JSON.stringify(LOCAL_BRIDGE_CHANNEL)}) return;
70
- window.parent.postMessage(data.payload, '*');
71
- });
72
- }
73
- `;
74
- var LOCAL_BRIDGE_SRCDOC = `<!doctype html><html><head></head><body><script>${BRIDGE_INSTALLER}<\/script></body></html>`;
75
- /**
76
- * 在同源空白 iframe 里安装转发脚本。调用发生在 iframe realm,父页面看到的 source 才是它。
77
- * @param iframe 用作消息身份的空白 iframe。
78
- * @returns 脚本是否已经装上。
79
- */
80
- var installLocalBridge = (iframe) => {
81
- const win = iframe.contentWindow;
82
- if (!win || win.__docsLocalBridge) return Boolean(win?.__docsLocalBridge);
83
- win.eval(BRIDGE_INSTALLER);
84
- return Boolean(win.__docsLocalBridge);
85
- };
86
- /**
87
- * 满足 PlaygroundService 现有 Window 调用的预览窗口。
88
- * 出站消息经 iframe 转发,入站回复再交回服务,协议仍是 docs:height / docs:navigate。
89
- */
90
- var LocalPreviewPort = class {
91
- iframe;
92
- parent;
93
- listeners = /* @__PURE__ */ new Map();
94
- constructor(iframe) {
95
- this.iframe = iframe;
96
- this.parent = { postMessage: (data) => {
97
- this.iframe.contentWindow?.postMessage({
98
- channel: LOCAL_BRIDGE_CHANNEL,
99
- payload: data
100
- }, "*");
101
- } };
102
- }
103
- get contentWindow() {
104
- return this.iframe.contentWindow;
105
- }
106
- get innerHeight() {
107
- const fromWindow = this.iframe.contentWindow?.innerHeight ?? 0;
108
- if (fromWindow > 0) return fromWindow;
109
- return this.iframe.clientHeight || this.iframe.parentElement?.clientHeight || 0;
110
- }
111
- addEventListener(type, listener) {
112
- const win = this.iframe.contentWindow;
113
- if (!win) return;
114
- if (type !== "message") {
115
- win.addEventListener(type, listener);
116
- return;
117
- }
118
- const wrapped = (event) => {
119
- const message = event;
120
- const data = message.data;
121
- if (data && data.channel === "docs:local-out") return;
122
- listener({
123
- data: message.data,
124
- source: this.parent
125
- });
126
- };
127
- this.listeners.set(listener, wrapped);
128
- win.addEventListener("message", wrapped);
129
- }
130
- removeEventListener(type, listener) {
131
- const win = this.iframe.contentWindow;
132
- if (!win) return;
133
- const wrapped = this.listeners.get(listener);
134
- if (wrapped) {
135
- this.listeners.delete(listener);
136
- win.removeEventListener(type, wrapped);
137
- return;
138
- }
139
- win.removeEventListener(type, listener);
140
- }
141
- requestAnimationFrame(callback) {
142
- return window.requestAnimationFrame(callback);
143
- }
144
- cancelAnimationFrame(handle) {
145
- window.cancelAnimationFrame(handle);
146
- }
147
- setTimeout(handler, timeout, ...args) {
148
- return window.setTimeout(handler, timeout, ...args);
149
- }
150
- clearTimeout(handle) {
151
- window.clearTimeout(handle);
152
- }
153
- };
154
- //#endregion
155
- //#region packages/playground/src/core/runtime/local/format-error.ts
156
- /**
157
- * 空 reject(例如弹层取消)不要渲染成红条。
158
- * @param error 运行时抛出的值。
159
- * @returns 可展示的错误文本;空取消返回空字符串。
160
- */
161
- var formatPlaygroundRuntimeError = (error) => {
162
- if (error == null) return "";
163
- const raw = typeof error === "object" && "message" in error ? error.message : error;
164
- if (raw == null || raw === "") return "";
165
- const text = String(raw);
166
- if (text === "undefined" || text === "null") return "";
167
- return text;
168
- };
169
- //#endregion
170
- //#region packages/playground/src/core/runtime/local/local-sandbox.vue?vue&type=script&setup=true&lang.ts
171
- var _hoisted_1 = ["srcdoc"];
172
- var local_sandbox_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ (0, vue.defineComponent)({
173
- __name: "local-sandbox",
174
- props: {
175
- files: {},
176
- entry: {},
177
- options: {},
178
- previewScroller: {
179
- type: Boolean,
180
- default: false
181
- },
182
- clearConsole: {
183
- type: Boolean,
184
- default: false
185
- },
186
- autoHeight: {
187
- type: Boolean,
188
- default: false
189
- }
190
- },
191
- emits: ["error"],
192
- setup(__props, { expose: __expose, emit: __emit }) {
193
- const props = __props;
194
- const emit = __emit;
195
- const bridgeSrcdoc = LOCAL_BRIDGE_SRCDOC;
196
- const containerEl = (0, vue.ref)(null);
197
- const bridgeEl = (0, vue.ref)(null);
198
- const mountEl = (0, vue.ref)(null);
199
- const compileError = (0, vue.ref)("");
200
- const runtimeError = (0, vue.ref)("");
201
- const messageSource = (0, vue.shallowRef)(null);
202
- let port = null;
203
- let previewApp = null;
204
- let version = 0;
205
- let pendingMount = null;
206
- const styleId = `docs-local-${Math.random().toString(36).slice(2, 10)}`;
207
- const publishError = () => {
208
- emit("error", {
209
- compile: compileError.value,
210
- runtime: runtimeError.value
211
- });
212
- };
213
- (0, vue.watch)([compileError, runtimeError], publishError);
214
- const createDocsLink = (preview) => (0, vue.defineComponent)({
215
- name: "DocsLink",
216
- props: { to: {
217
- type: String,
218
- default: ""
219
- } },
220
- setup(linkProps, { slots }) {
221
- return () => (0, vue.h)("a", {
222
- href: linkProps.to,
223
- style: {
224
- color: "inherit",
225
- textDecoration: "none"
226
- },
227
- onClick(event) {
228
- event.preventDefault();
229
- preview.parent.postMessage({
230
- action: "docs:navigate",
231
- to: linkProps.to
232
- });
233
- }
234
- }, slots.default?.());
235
- }
236
- });
237
- const destroyPreview = () => {
238
- previewApp?.unmount();
239
- previewApp = null;
240
- removeStyle(styleId);
241
- };
242
- const mountPreview = (component, preview, css) => {
243
- if (!mountEl.value) return;
244
- destroyPreview();
245
- injectStyle(styleId, css);
246
- if (props.clearConsole) console.clear();
247
- const useScroller = props.previewScroller === true;
248
- const app = vue.createApp({
249
- name: "LocalSandboxRoot",
250
- setup() {
251
- const service = createLocalPlayground(vue);
252
- (0, vue.provide)("docs:playground", service);
253
- (0, vue.onErrorCaptured)((error) => {
254
- const message = formatPlaygroundRuntimeError(error);
255
- if (message) runtimeError.value = message;
256
- return false;
257
- });
258
- (0, vue.onBeforeUnmount)(() => service.dispose());
259
- return () => {
260
- const content = (0, vue.h)(component);
261
- if (!useScroller) return content;
262
- return (0, vue.h)(_deot_vc.Scroller, {
263
- height: "100%",
264
- native: false,
265
- autoResize: true,
266
- showBar: true,
267
- contentClass: require_store.PREVIEW_SCROLL_CONTENT_CLASS
268
- }, { default: () => content });
269
- };
270
- }
271
- });
272
- app.component("DocsLink", createDocsLink(preview));
273
- app.config.errorHandler = (error) => {
274
- const message = formatPlaygroundRuntimeError(error);
275
- if (message) runtimeError.value = message;
276
- };
277
- app.mount(mountEl.value);
278
- previewApp = app;
279
- };
280
- const ensurePort = () => {
281
- const iframe = bridgeEl.value;
282
- if (!iframe?.contentWindow) return null;
283
- try {
284
- if (!installLocalBridge(iframe)) return null;
285
- } catch {
286
- return null;
287
- }
288
- port ??= new LocalPreviewPort(iframe);
289
- messageSource.value = iframe.contentWindow;
290
- return port;
291
- };
292
- const tryMount = async () => {
293
- const job = pendingMount;
294
- /* istanbul ignore if -- 桥接 iframe 尚未就绪或编译已被取代 */
295
- if (!job || job.version !== version) return;
296
- const preview = ensurePort();
297
- /* istanbul ignore if -- 挂载点在编译完成时已经存在 */
298
- if (!preview || !mountEl.value) return;
299
- await (0, vue.nextTick)();
300
- /* istanbul ignore if -- 桥接 iframe 尚未就绪或编译已被取代 */
301
- if (pendingMount !== job || !mountEl.value) return;
302
- pendingMount = null;
303
- mountPreview(job.component, preview, job.css);
304
- };
305
- const run = async () => {
306
- const current = ++version;
307
- pendingMount = null;
308
- compileError.value = "";
309
- runtimeError.value = "";
310
- publishError();
311
- destroyPreview();
312
- try {
313
- const [{ compilePlayground }, { linkPlayground }, modulesApi, fileApi] = await Promise.all([
314
- Promise.resolve().then(() => require("./compile-Da095AyM.cjs")),
315
- Promise.resolve().then(() => require("./linker-DIpEiean.cjs")),
316
- Promise.resolve().then(() => require("./modules-E6kK6ZJH.cjs")),
317
- Promise.resolve().then(() => require("./files-BsAOZiPN.cjs")).then((n) => n.files_exports)
318
- ]);
319
- /* istanbul ignore if -- 并发编译时只保留最后一次 */
320
- if (current !== version) return;
321
- const compiled = await compilePlayground(props.files, { entry: props.entry });
322
- /* istanbul ignore if -- 并发编译时只保留最后一次 */
323
- if (current !== version) return;
324
- if (compiled.errors.length || !compiled.entry) {
325
- compileError.value = compiled.errors.join("\n") || "[Playground] 未能确定入口";
326
- publishError();
327
- return;
328
- }
329
- const imports = require_store.createRuntimeImports(props.options.cdnURL, props.options.builtinImportMap);
330
- const specifiers = fileApi.collectBareSpecifiers(props.files, fileApi.collectReachableFilenames(props.files, compiled.entry));
331
- const { createRemoteLoader } = await Promise.resolve().then(() => require("./remote-BG0E1Ugo.cjs"));
332
- const remote = createRemoteLoader(imports, {
333
- vue,
334
- vueRouter: vue_router
335
- }, props.options.cdnURL);
336
- const loaded = await modulesApi.loadLocalModules(specifiers, imports, {
337
- vue,
338
- vueRouter: vue_router
339
- }, (url) => remote.load(url), props.options.cdnURL);
340
- /* istanbul ignore if -- 并发编译时只保留最后一次 */
341
- if (current !== version) return;
342
- if (loaded.errors.length) {
343
- compileError.value = loaded.errors.join("\n");
344
- publishError();
345
- return;
346
- }
347
- const linked = linkPlayground(compiled, props.files, { modules: loaded.modules });
348
- /* istanbul ignore if -- 并发编译时只保留最后一次 */
349
- if (current !== version) return;
350
- if (linked.errors.length || !linked.component) {
351
- compileError.value = (linked.errors.length ? linked.errors : ["[Playground] 未能解析入口组件"]).join("\n");
352
- publishError();
353
- return;
354
- }
355
- pendingMount = {
356
- component: linked.component,
357
- css: linked.css,
358
- version: current
359
- };
360
- await tryMount();
361
- } catch (error) {
362
- /* istanbul ignore next -- 编译器自身异常才进入,普通错误走结果里的 errors */
363
- if (current !== version) return;
364
- /* istanbul ignore next -- 同上,只兜底编译器抛错 */
365
- compileError.value = error instanceof Error ? error.message : String(error);
366
- /* istanbul ignore next -- 同上,只兜底编译器抛错 */
367
- publishError();
368
- }
369
- };
370
- const handleBridgeLoad = () => {
371
- tryMount();
372
- };
373
- (0, vue.onMounted)(() => {
374
- run();
375
- });
376
- (0, vue.watch)(() => [
377
- props.files,
378
- props.entry,
379
- props.options,
380
- props.previewScroller
381
- ], () => {
382
- if (!containerEl.value) return;
383
- run();
384
- }, { deep: true });
385
- (0, vue.onBeforeUnmount)(() => {
386
- version += 1;
387
- pendingMount = null;
388
- destroyPreview();
389
- });
390
- __expose({
391
- container: containerEl,
392
- messageSource,
393
- local: true,
394
- refresh: () => {
395
- run();
396
- }
397
- });
398
- return (_ctx, _cache) => {
399
- return (0, vue.openBlock)(), (0, vue.createElementBlock)("div", {
400
- ref_key: "containerEl",
401
- ref: containerEl,
402
- class: "docs-playground-local"
403
- }, [(0, vue.createElementVNode)("iframe", {
404
- ref_key: "bridgeEl",
405
- ref: bridgeEl,
406
- class: "docs-playground-local__bridge",
407
- tabindex: "-1",
408
- "aria-hidden": "true",
409
- title: "",
410
- srcdoc: (0, vue.unref)(bridgeSrcdoc),
411
- onLoad: handleBridgeLoad
412
- }, null, 40, _hoisted_1), (0, vue.createElementVNode)("div", {
413
- ref_key: "mountEl",
414
- ref: mountEl,
415
- class: (0, vue.normalizeClass)(["docs-playground-local__mount", { "docs-playground-local__mount--auto": __props.autoHeight }])
416
- }, null, 2)], 512);
417
- };
418
- }
419
- });
420
- //#endregion
421
- //#region packages/playground/src/core/runtime/local/local-sandbox.vue
422
- var local_sandbox_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({ default: () => local_sandbox_default });
423
- var local_sandbox_default = local_sandbox_vue_vue_type_script_setup_true_lang_default;
424
- //#endregion
425
- exports.collectCss = collectCss;
426
- exports.default = local_sandbox_default;
427
- Object.defineProperty(exports, "local_sandbox_exports", {
428
- enumerable: true,
429
- get: function() {
430
- return local_sandbox_exports;
431
- }
432
- });