@deot/docs-playground 1.1.9 → 1.2.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.
@@ -1,322 +0,0 @@
1
- const require_css = require("./css-DGNVJsLz.cjs");
2
- let sass = require("sass");
3
- //#region packages/playground/src/core/runtime/local/compile/scss.ts
4
- /**
5
- * 直接渲染使用已安装的 sass,importer 与 iframe 预览共用,不注入额外 BEM。
6
- */
7
- var compiler = { compileString(source, options) {
8
- return { css: (0, sass.compileString)(source, options).css };
9
- } };
10
- var toScssFiles = (files) => {
11
- const map = {};
12
- Object.entries(files || {}).forEach(([filename, code]) => {
13
- const normalized = filename.replace(/\\/gu, "/");
14
- map[normalized] = code;
15
- map[normalized.replace(/^\/+/u, "")] = code;
16
- });
17
- return map;
18
- };
19
- var compileLocalScss = (source, filename, files, lang) => {
20
- try {
21
- return require_css.compileScssSource(source, filename.replace(/^\/+/u, ""), compiler, toScssFiles(files), lang);
22
- } catch (error) {
23
- /* istanbul ignore next -- Sass 失败时 message 总是字符串 */
24
- const message = error instanceof Error ? error.message : String(error);
25
- throw new Error(`[Playground] SCSS 编译失败 ${filename}: ${message}`, { cause: error });
26
- }
27
- };
28
- //#endregion
29
- //#region packages/playground/src/core/runtime/local/compile/sfc.ts
30
- var compilerPromise = null;
31
- /**
32
- * 只加载浏览器构建,避免 `@vue/compiler-sfc` 的 Node 主入口碰文件系统。
33
- * @returns 浏览器版 compiler-sfc。
34
- */
35
- var loadCompiler = () => {
36
- compilerPromise ??= import("@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js").then((mod) => {
37
- return mod.default || mod;
38
- });
39
- return compilerPromise;
40
- };
41
- var hashId = (filename) => {
42
- let hash = 0;
43
- for (let i = 0; i < filename.length; i += 1) {
44
- hash = (hash << 5) - hash + filename.charCodeAt(i);
45
- hash |= 0;
46
- }
47
- return Math.abs(hash).toString(36).slice(0, 8);
48
- };
49
- var messageOf$1 = (error) => error instanceof Error ? error.message : String(error);
50
- var isScssLang = (lang) => lang === "scss" || lang === "sass";
51
- var isSupportedStyleLang = (lang) => !lang || lang === "css" || isScssLang(lang);
52
- var compileSfc = async (code, filename, files = {}) => {
53
- const compiler = await loadCompiler();
54
- const errors = [];
55
- const id = hashId(filename);
56
- const { descriptor, errors: parseErrors } = compiler.parse(code, {
57
- filename,
58
- sourceMap: false
59
- });
60
- parseErrors.forEach((error) => {
61
- errors.push(messageOf$1(error));
62
- });
63
- if (errors.length) return {
64
- js: "export default {}",
65
- css: "",
66
- errors
67
- };
68
- const srcBlock = [
69
- descriptor.script,
70
- descriptor.scriptSetup,
71
- descriptor.template,
72
- ...descriptor.styles
73
- ].find((block) => block?.src);
74
- if (srcBlock) {
75
- errors.push(`[Playground] 不支持 <${srcBlock.type} src>,请把源码放进 files。文件: ${filename}`);
76
- return {
77
- js: "export default {}",
78
- css: "",
79
- errors
80
- };
81
- }
82
- const hasScript = !!(descriptor.script || descriptor.scriptSetup);
83
- const isTS = !!(descriptor.script?.lang?.startsWith("ts") || descriptor.scriptSetup?.lang?.startsWith("ts"));
84
- let clientCode;
85
- let bindings;
86
- if (hasScript) try {
87
- const scriptResult = compiler.compileScript(descriptor, {
88
- id,
89
- inlineTemplate: true,
90
- genDefaultAs: "__sfc__",
91
- templateOptions: {
92
- filename,
93
- id,
94
- scoped: descriptor.styles.some((style) => style.scoped)
95
- }
96
- });
97
- clientCode = scriptResult.content;
98
- bindings = scriptResult.bindings;
99
- } catch (error) {
100
- errors.push(`[Playground] compileScript ${filename}: ${messageOf$1(error)}`);
101
- return {
102
- js: "export default {}",
103
- css: "",
104
- errors
105
- };
106
- }
107
- else clientCode = "const __sfc__ = {};";
108
- const templateInlined = !!(descriptor.scriptSetup && hasScript);
109
- if (descriptor.template && !templateInlined) try {
110
- const templateResult = compiler.compileTemplate({
111
- id,
112
- filename,
113
- source: descriptor.template.content,
114
- scoped: descriptor.styles.some((style) => style.scoped),
115
- compilerOptions: {
116
- bindingMetadata: bindings,
117
- isTS
118
- }
119
- });
120
- if (templateResult.errors?.length) templateResult.errors.forEach((error) => {
121
- errors.push(messageOf$1(error));
122
- });
123
- else {
124
- clientCode += `\n${templateResult.code}`;
125
- clientCode += "\n__sfc__.render = typeof render === \"function\" ? render : exports.render;";
126
- }
127
- } catch (error) {
128
- errors.push(`[Playground] compileTemplate ${filename}: ${messageOf$1(error)}`);
129
- }
130
- if (descriptor.styles.some((style) => style.scoped)) clientCode += `\n__sfc__.__scopeId = ${JSON.stringify(`data-v-${id}`)};`;
131
- clientCode += `\n__sfc__.__file = ${JSON.stringify(filename)};`;
132
- clientCode += "\nexport default __sfc__;";
133
- let css = "";
134
- for (const style of descriptor.styles) {
135
- if (!isSupportedStyleLang(style.lang)) {
136
- errors.push(`[Playground] <style lang="${style.lang}"> 暂不支持(仅 CSS / SCSS / Sass)。文件: ${filename}`);
137
- continue;
138
- }
139
- let source = style.content;
140
- if (isScssLang(style.lang)) try {
141
- source = compileLocalScss(source, filename, files, style.lang);
142
- } catch (error) {
143
- errors.push(messageOf$1(error));
144
- continue;
145
- }
146
- try {
147
- const styleResult = await compiler.compileStyleAsync({
148
- id: style.scoped ? `data-v-${id}` : id,
149
- filename,
150
- source,
151
- scoped: !!style.scoped
152
- });
153
- if (styleResult.errors?.length) styleResult.errors.forEach((error) => {
154
- errors.push(messageOf$1(error));
155
- });
156
- else css += `${styleResult.code}\n`;
157
- } catch (error) {
158
- /* istanbul ignore next -- 样式编译失败会走 errors 数组,这里只兜底抛错 */
159
- errors.push(`[Playground] compileStyle ${filename}: ${messageOf$1(error)}`);
160
- }
161
- }
162
- return {
163
- js: clientCode,
164
- css,
165
- errors
166
- };
167
- };
168
- //#endregion
169
- //#region packages/playground/src/core/runtime/local/compile/index.ts
170
- var JSON_IDENT = /^[$A-Z_][0-9A-Z_$]*$/iu;
171
- var messageOf = (error) => error instanceof Error ? error.message : String(error);
172
- /**
173
- * Vite 语义:default 是整份对象,合法标识符键再挂成具名导出。
174
- * @param parsed JSON.parse 的结果。
175
- * @param filename 当前虚拟文件名,写入 sourceURL。
176
- * @returns 可执行的 CJS 文本。
177
- */
178
- var transformJson = (parsed, filename) => {
179
- const lines = [`exports.default = ${JSON.stringify(parsed)};`];
180
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) Object.keys(parsed).forEach((key) => {
181
- if (!JSON_IDENT.test(key) || key === "__esModule" || key === "default") return;
182
- lines.push(`exports[${JSON.stringify(key)}] = exports.default[${JSON.stringify(key)}];`);
183
- });
184
- lines.push(`//# sourceURL=playground://${filename}`);
185
- return lines.join("\n");
186
- };
187
- var compileOne = async (filename, code, files) => {
188
- const ext = require_css.getExtension(filename);
189
- const errors = [];
190
- if (ext === ".vue") {
191
- if (/lang\s*=\s*["']jsx["']/iu.test(code) || /lang\s*=\s*["']tsx["']/iu.test(code)) {
192
- errors.push(`[Playground] JSX/TSX 暂不支持: ${filename}`);
193
- return {
194
- filename,
195
- js: "exports.default = {}",
196
- css: "",
197
- errors
198
- };
199
- }
200
- const result = await compileSfc(code, filename, files);
201
- errors.push(...result.errors);
202
- return {
203
- filename,
204
- js: require_css.transformScript(result.js, filename, { typescript: true }),
205
- css: result.css,
206
- errors
207
- };
208
- }
209
- if (ext === ".css") return {
210
- filename,
211
- js: `exports.default = {};\n//# sourceURL=playground://${filename}`,
212
- css: code,
213
- errors
214
- };
215
- if (ext === ".scss" || ext === ".sass") try {
216
- const { css, exports: styleExports } = require_css.extractScssExports(compileLocalScss(code, filename, files), filename);
217
- return {
218
- filename,
219
- js: `exports.__esModule = true; exports.default = ${JSON.stringify(styleExports)};\n//# sourceURL=playground://${filename}`,
220
- css,
221
- errors
222
- };
223
- } catch (error) {
224
- errors.push(messageOf(error));
225
- return {
226
- filename,
227
- js: "exports.default = {};",
228
- css: "",
229
- errors
230
- };
231
- }
232
- if (ext === ".json") try {
233
- return {
234
- filename,
235
- js: transformJson(JSON.parse(code), filename),
236
- css: "",
237
- errors
238
- };
239
- } catch (error) {
240
- errors.push(`[Playground] JSON 解析失败 ${filename}: ${messageOf(error)}`);
241
- return {
242
- filename,
243
- js: "exports.default = {};",
244
- css: "",
245
- errors
246
- };
247
- }
248
- if (ext === ".js" || ext === ".ts" || ext === ".jsx" || ext === ".tsx") try {
249
- return {
250
- filename,
251
- js: require_css.transformScript(code, filename, {
252
- typescript: ext === ".ts" || ext === ".tsx",
253
- jsx: ext === ".jsx" || ext === ".tsx"
254
- }),
255
- css: "",
256
- errors
257
- };
258
- } catch (error) {
259
- errors.push(messageOf(error));
260
- return {
261
- filename,
262
- js: "exports.default = {};",
263
- css: "",
264
- errors
265
- };
266
- }
267
- errors.push(`[Playground] 不支持的文件类型: ${filename}`);
268
- return {
269
- filename,
270
- js: "exports.default = {};",
271
- css: "",
272
- errors
273
- };
274
- };
275
- /**
276
- * 浏览器内编译整份 files。入口和相对路径与 Playground 的 files 键一致。
277
- * @param files 当前 Playground 文件表。
278
- * @param entryOrOptions 入口文件名,或带入口的编译选项。
279
- * @returns 编译出的模块、样式和错误。
280
- */
281
- var compilePlayground = async (files, entryOrOptions) => {
282
- const options = typeof entryOrOptions === "string" ? { entry: entryOrOptions } : entryOrOptions || {};
283
- const errors = [];
284
- let entry;
285
- try {
286
- entry = require_css.resolveEntry(files, options.entry);
287
- } catch (error) {
288
- return {
289
- entry: "",
290
- modules: {},
291
- css: [],
292
- errors: [messageOf(error)]
293
- };
294
- }
295
- const fileIndex = require_css.createFileIndex(files);
296
- const reachable = require_css.collectReachableFilenames(files, entry);
297
- const modules = {};
298
- const cssChunks = [];
299
- for (const filename of reachable) {
300
- const code = fileIndex.get(filename);
301
- if (typeof code !== "string") continue;
302
- const compiled = await compileOne(filename, code, files);
303
- errors.push(...compiled.errors);
304
- modules[filename] = {
305
- filename,
306
- js: compiled.js,
307
- css: compiled.css
308
- };
309
- cssChunks.push(...require_css.collectCss([compiled.css]));
310
- }
311
- const normalizedEntry = require_css.normalizeVirtualPath(entry);
312
- /* istanbul ignore if -- 可达编译总会留下入口模块 */
313
- if (!modules[normalizedEntry]) errors.push(`[Playground] 入口编译结果缺失: ${normalizedEntry}`);
314
- return {
315
- entry: normalizedEntry,
316
- modules,
317
- css: cssChunks,
318
- errors
319
- };
320
- };
321
- //#endregion
322
- exports.compilePlayground = compilePlayground;