@sybz-components/utils 1.0.22 → 1.0.23
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/base.cjs +1 -1
- package/dist/base.mjs +1 -1
- package/dist/confirmSemantic.cjs +6 -5
- package/dist/confirmSemantic.mjs +6 -5
- package/dist/format.cjs +1 -1
- package/dist/format.mjs +1 -1
- package/dist/gitCommitLog.cjs +6 -4
- package/dist/gitCommitLog.mjs +6 -4
- package/dist/http.cjs +1 -1
- package/dist/http.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/postcss.cjs +99 -0
- package/dist/postcss.d.cts +16 -0
- package/dist/postcss.d.mts +16 -0
- package/dist/postcss.d.ts +16 -0
- package/dist/postcss.mjs +95 -0
- package/dist/shared/{utils.veOn4f_Q.mjs → utils.BNdsPonV.mjs} +73 -58
- package/dist/shared/{utils.BCKcQULi.cjs → utils.CshLEFSw.cjs} +73 -58
- package/dist/sybz-code-standard.cjs +4 -3
- package/dist/sybz-code-standard.mjs +4 -3
- package/dist/vite.cjs +686 -6
- package/dist/vite.d.cts +96 -3
- package/dist/vite.d.mts +96 -3
- package/dist/vite.d.ts +96 -3
- package/dist/vite.mjs +686 -6
- package/package.json +10 -2
package/dist/vite.mjs
CHANGED
|
@@ -1,24 +1,627 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { isAbsolute, relative, sep, resolve } from 'node:path';
|
|
2
4
|
import { codeInspectorPlugin } from 'code-inspector-plugin';
|
|
3
5
|
import tailwindcss from '@tailwindcss/vite';
|
|
4
6
|
import vueJsx from '@vitejs/plugin-vue-jsx';
|
|
5
7
|
import { gitCommitLog } from './gitCommitLog.mjs';
|
|
6
|
-
import '
|
|
7
|
-
import 'node:
|
|
8
|
-
|
|
8
|
+
import { SYBZ_COMPAT_BROWSERS, createSybzCompatPostcssPlugins } from './postcss.mjs';
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
|
|
11
|
+
const DEFAULT_METHODS$1 = ["log", "info", "warn", "error", "debug"];
|
|
12
|
+
const GO_TO_EDITORS = /* @__PURE__ */ new Set(["code", "code-insiders", "cursor", "vscodium", "windsurf", "trae"]);
|
|
13
|
+
const SOURCE_RE$1 = /\.(?:[cm]?[jt]sx?|vue)$/;
|
|
14
|
+
const SCRIPT_RE$1 = /<script\b[^>]*>([\s\S]*?)<\/script>/g;
|
|
15
|
+
const IDENTIFIER_RE = /[\w$]/;
|
|
16
|
+
const METHOD_RE = /[A-Za-z]/;
|
|
17
|
+
const normalizePath = (value) => value.replace(/\\/g, "/");
|
|
18
|
+
const countNewlines = (value) => {
|
|
19
|
+
let count = 0;
|
|
20
|
+
for (let index = 0; index < value.length; index++) {
|
|
21
|
+
if (value[index] === "\n") count++;
|
|
22
|
+
}
|
|
23
|
+
return count;
|
|
24
|
+
};
|
|
25
|
+
const createMatcher = (matcher) => {
|
|
26
|
+
if (!matcher) return void 0;
|
|
27
|
+
const matchers = Array.isArray(matcher) ? matcher : [matcher];
|
|
28
|
+
return (value) => matchers.some((item) => {
|
|
29
|
+
if (item instanceof RegExp) {
|
|
30
|
+
item.lastIndex = 0;
|
|
31
|
+
return item.test(value);
|
|
32
|
+
}
|
|
33
|
+
return value.includes(item);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const findConsoleCalls = (code, methods) => {
|
|
37
|
+
const calls = [];
|
|
38
|
+
let state = "code";
|
|
39
|
+
for (let index = 0; index < code.length; index++) {
|
|
40
|
+
const char = code[index];
|
|
41
|
+
const next = code[index + 1];
|
|
42
|
+
if (state === "line-comment") {
|
|
43
|
+
if (char === "\n") state = "code";
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (state === "block-comment") {
|
|
47
|
+
if (char === "*" && next === "/") {
|
|
48
|
+
state = "code";
|
|
49
|
+
index++;
|
|
50
|
+
}
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (state === "single" || state === "double") {
|
|
54
|
+
if (char === "\\") {
|
|
55
|
+
index++;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (state === "single" && char === "'" || state === "double" && char === '"') state = "code";
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (state === "template") {
|
|
62
|
+
if (char === "\\") {
|
|
63
|
+
index++;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (char === "`") state = "code";
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (char === "/" && next === "/") {
|
|
70
|
+
state = "line-comment";
|
|
71
|
+
index++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (char === "/" && next === "*") {
|
|
75
|
+
state = "block-comment";
|
|
76
|
+
index++;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (char === "'") {
|
|
80
|
+
state = "single";
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (char === '"') {
|
|
84
|
+
state = "double";
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (char === "`") {
|
|
88
|
+
state = "template";
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (!code.startsWith("console", index)) continue;
|
|
92
|
+
if (index > 0 && (IDENTIFIER_RE.test(code[index - 1]) || code[index - 1] === ".")) continue;
|
|
93
|
+
let cursor = index + 7;
|
|
94
|
+
while (/\s/.test(code[cursor] || "")) cursor++;
|
|
95
|
+
if (code[cursor] === "?" && code[cursor + 1] === ".") cursor += 2;
|
|
96
|
+
else if (code[cursor] === ".") cursor++;
|
|
97
|
+
else continue;
|
|
98
|
+
while (/\s/.test(code[cursor] || "")) cursor++;
|
|
99
|
+
const methodStart = cursor;
|
|
100
|
+
while (METHOD_RE.test(code[cursor] || "")) cursor++;
|
|
101
|
+
const method = code.slice(methodStart, cursor);
|
|
102
|
+
if (!methods.has(method)) continue;
|
|
103
|
+
while (/\s/.test(code[cursor] || "")) cursor++;
|
|
104
|
+
if (code[cursor] !== "(") continue;
|
|
105
|
+
calls.push({ start: index, paren: cursor, method });
|
|
106
|
+
index = cursor;
|
|
107
|
+
}
|
|
108
|
+
return calls;
|
|
109
|
+
};
|
|
110
|
+
const findClosingParen = (code, openIndex) => {
|
|
111
|
+
let state = "code";
|
|
112
|
+
let depth = 0;
|
|
113
|
+
for (let index = openIndex; index < code.length; index++) {
|
|
114
|
+
const char = code[index];
|
|
115
|
+
const next = code[index + 1];
|
|
116
|
+
if (state === "line-comment") {
|
|
117
|
+
if (char === "\n") state = "code";
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (state === "block-comment") {
|
|
121
|
+
if (char === "*" && next === "/") {
|
|
122
|
+
state = "code";
|
|
123
|
+
index++;
|
|
124
|
+
}
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (state === "single") {
|
|
128
|
+
if (char === "\\") index++;
|
|
129
|
+
else if (char === "'") state = "code";
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (state === "double") {
|
|
133
|
+
if (char === "\\") index++;
|
|
134
|
+
else if (char === '"') state = "code";
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (state === "template") {
|
|
138
|
+
if (char === "\\") {
|
|
139
|
+
index++;
|
|
140
|
+
} else if (char === "`") {
|
|
141
|
+
state = "code";
|
|
142
|
+
}
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (char === "'") {
|
|
146
|
+
state = "single";
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (char === '"') {
|
|
150
|
+
state = "double";
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (char === "`") {
|
|
154
|
+
state = "template";
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (char === "/" && next === "/") {
|
|
158
|
+
state = "line-comment";
|
|
159
|
+
index++;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (char === "/" && next === "*") {
|
|
163
|
+
state = "block-comment";
|
|
164
|
+
index++;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (char === "(") depth++;
|
|
168
|
+
else if (char === ")") {
|
|
169
|
+
depth--;
|
|
170
|
+
if (depth === 0) return index;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return -1;
|
|
174
|
+
};
|
|
175
|
+
const createStaticPrefix = (context, line) => {
|
|
176
|
+
var _a;
|
|
177
|
+
const format = (_a = context.options.format) != null ? _a : (source, lineNumber) => `[${source}:${lineNumber}]`;
|
|
178
|
+
try {
|
|
179
|
+
const prefix = format(context.file, line);
|
|
180
|
+
return prefix ? JSON.stringify(prefix) : "";
|
|
181
|
+
} catch (e) {
|
|
182
|
+
return JSON.stringify(`[${context.file}:${line}]`);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const createJumpPrefix = (context, line) => {
|
|
186
|
+
var _a;
|
|
187
|
+
return `(typeof window === 'undefined' ? '' : window.location.origin) + '/s/${(_a = context.fileId) == null ? void 0 : _a.toString(36)}/${line.toString(36)}'`;
|
|
188
|
+
};
|
|
189
|
+
const injectConsoleSource = (code, context) => {
|
|
190
|
+
const calls = findConsoleCalls(code, context.methods);
|
|
191
|
+
if (!calls.length) return code;
|
|
192
|
+
let result = code;
|
|
193
|
+
for (let index = calls.length - 1; index >= 0; index--) {
|
|
194
|
+
const call = calls[index];
|
|
195
|
+
const line = context.lineOffset + countNewlines(code.slice(0, call.start));
|
|
196
|
+
const prefix = context.jump ? createJumpPrefix(context, line) : createStaticPrefix(context, line);
|
|
197
|
+
if (!prefix) continue;
|
|
198
|
+
const closeParen = findClosingParen(result, call.paren);
|
|
199
|
+
if (closeParen === -1) continue;
|
|
200
|
+
let insertAt = closeParen;
|
|
201
|
+
while (insertAt > call.paren + 1 && /\s/.test(result[insertAt - 1])) insertAt--;
|
|
202
|
+
const hasArguments = result.slice(call.paren + 1, insertAt).trim().length > 0;
|
|
203
|
+
const before = result.slice(0, insertAt);
|
|
204
|
+
const separator = hasArguments ? before.endsWith(",") ? " " : ", " : "";
|
|
205
|
+
result = result.slice(0, insertAt) + separator + prefix + result.slice(insertAt);
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
};
|
|
209
|
+
const injectVueSource = (code, context) => {
|
|
210
|
+
let output = "";
|
|
211
|
+
let cursor = 0;
|
|
212
|
+
let changed = false;
|
|
213
|
+
SCRIPT_RE$1.lastIndex = 0;
|
|
214
|
+
let match;
|
|
215
|
+
while (match = SCRIPT_RE$1.exec(code)) {
|
|
216
|
+
const scriptTag = match[0];
|
|
217
|
+
const content = match[1];
|
|
218
|
+
const contentStart = match.index + scriptTag.indexOf(">") + 1;
|
|
219
|
+
const contentEnd = contentStart + content.length;
|
|
220
|
+
const lineOffset = countNewlines(code.slice(0, contentStart)) + 1;
|
|
221
|
+
const transformed = injectConsoleSource(content, { ...context, lineOffset });
|
|
222
|
+
output += code.slice(cursor, contentStart) + transformed;
|
|
223
|
+
cursor = contentEnd;
|
|
224
|
+
if (transformed !== content) changed = true;
|
|
225
|
+
}
|
|
226
|
+
if (!changed) return code;
|
|
227
|
+
return output + code.slice(cursor);
|
|
228
|
+
};
|
|
229
|
+
const launchEditor = (editor, file, line) => new Promise((resolvePromise, reject) => {
|
|
230
|
+
const position = `${file}:${line}:1`;
|
|
231
|
+
const args = GO_TO_EDITORS.has(editor) ? ["-g", position] : [position];
|
|
232
|
+
try {
|
|
233
|
+
const child = spawn(editor, args, {
|
|
234
|
+
detached: true,
|
|
235
|
+
stdio: "ignore",
|
|
236
|
+
windowsHide: true,
|
|
237
|
+
shell: process.platform === "win32"
|
|
238
|
+
});
|
|
239
|
+
child.once("spawn", () => {
|
|
240
|
+
child.unref();
|
|
241
|
+
resolvePromise();
|
|
242
|
+
});
|
|
243
|
+
child.once("error", reject);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
reject(error);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
const createOpenMiddleware = (root, editor, fileIds) => {
|
|
249
|
+
const rootPrefix = root.endsWith(sep) ? root : `${root}${sep}`;
|
|
250
|
+
return (request, response) => {
|
|
251
|
+
const [idPart, linePart] = (request.url || "/").replace(/^\/+/, "").split("/");
|
|
252
|
+
const file = fileIds.get(parseInt(idPart, 36)) || "";
|
|
253
|
+
const line = Math.max(1, parseInt(linePart, 36) || 1);
|
|
254
|
+
const absolutePath = resolve(root, file);
|
|
255
|
+
if (!file || !absolutePath.startsWith(rootPrefix) || !existsSync(absolutePath)) {
|
|
256
|
+
response.statusCode = 404;
|
|
257
|
+
response.end("Source file not found");
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
launchEditor(editor, absolutePath, line).then(() => {
|
|
261
|
+
response.statusCode = 200;
|
|
262
|
+
response.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
263
|
+
response.end(
|
|
264
|
+
`<!doctype html><meta charset="utf-8"><title>Sybz</title><body><script>try { window.close(); } catch (error) {} document.body.textContent = ${JSON.stringify(
|
|
265
|
+
`\u5DF2\u6253\u5F00 ${file}:${line}\uFF08${editor}\uFF09`
|
|
266
|
+
)};<\/script></body>`
|
|
267
|
+
);
|
|
268
|
+
}).catch((error) => {
|
|
269
|
+
response.statusCode = 500;
|
|
270
|
+
response.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
271
|
+
response.end(`\u65E0\u6CD5\u6253\u5F00\u7F16\u8F91\u5668 ${editor}\uFF1A${String(error)}`);
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
const createConsoleSource = (options = {}) => {
|
|
276
|
+
var _a, _b;
|
|
277
|
+
let root = process.cwd();
|
|
278
|
+
let isServe = false;
|
|
279
|
+
const editor = (_a = options.editor) != null ? _a : "code";
|
|
280
|
+
const methods = new Set((_b = options.methods) != null ? _b : DEFAULT_METHODS$1);
|
|
281
|
+
const include = createMatcher(options.include);
|
|
282
|
+
const exclude = createMatcher(options.exclude);
|
|
283
|
+
const fileIds = /* @__PURE__ */ new Map();
|
|
284
|
+
const fileIdByName = /* @__PURE__ */ new Map();
|
|
285
|
+
let nextFileId = 1;
|
|
286
|
+
const getFileId = (file) => {
|
|
287
|
+
const cached = fileIdByName.get(file);
|
|
288
|
+
if (cached) return cached;
|
|
289
|
+
const id = nextFileId++;
|
|
290
|
+
fileIdByName.set(file, id);
|
|
291
|
+
fileIds.set(id, file);
|
|
292
|
+
return id;
|
|
293
|
+
};
|
|
294
|
+
return {
|
|
295
|
+
name: "sybz:console-source",
|
|
296
|
+
apply: options.build ? void 0 : "serve",
|
|
297
|
+
enforce: "pre",
|
|
298
|
+
configResolved(config) {
|
|
299
|
+
root = config.root;
|
|
300
|
+
isServe = config.command === "serve";
|
|
301
|
+
},
|
|
302
|
+
configureServer(server) {
|
|
303
|
+
if (options.jump === false) return;
|
|
304
|
+
server.middlewares.use("/s", createOpenMiddleware(root, editor, fileIds));
|
|
305
|
+
},
|
|
306
|
+
transform(code, id) {
|
|
307
|
+
if (!code) return;
|
|
308
|
+
if (!id || id.startsWith("\0") || id.startsWith("virtual:")) return;
|
|
309
|
+
const normalizedId = normalizePath(id);
|
|
310
|
+
if (normalizedId.includes("/node_modules/") || normalizedId.includes("/.vitepress/cache/") || normalizedId.includes("/.vitepress/dist/")) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const cleanId = id.split("?")[0];
|
|
314
|
+
if (!cleanId || !isAbsolute(cleanId) || !SOURCE_RE$1.test(cleanId)) return;
|
|
315
|
+
if (cleanId.includes(".d.ts")) return;
|
|
316
|
+
const file = normalizePath(relative(root, cleanId));
|
|
317
|
+
if (!file || file.startsWith("..") || file.startsWith("/")) return;
|
|
318
|
+
if (include && !include(file) && !include(id)) return;
|
|
319
|
+
if ((exclude == null ? void 0 : exclude(file)) || (exclude == null ? void 0 : exclude(id))) return;
|
|
320
|
+
const jump = isServe && options.jump !== false;
|
|
321
|
+
const context = {
|
|
322
|
+
file,
|
|
323
|
+
fileId: jump ? getFileId(file) : void 0,
|
|
324
|
+
lineOffset: 1,
|
|
325
|
+
methods,
|
|
326
|
+
options,
|
|
327
|
+
jump
|
|
328
|
+
};
|
|
329
|
+
const transformed = cleanId.endsWith(".vue") ? injectVueSource(code, context) : injectConsoleSource(code, context);
|
|
330
|
+
if (transformed === code) return;
|
|
331
|
+
return { code: transformed, map: null };
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const DEFAULT_METHODS = [
|
|
337
|
+
"log",
|
|
338
|
+
"info",
|
|
339
|
+
"warn",
|
|
340
|
+
"error",
|
|
341
|
+
"debug",
|
|
342
|
+
"trace",
|
|
343
|
+
"dir",
|
|
344
|
+
"dirxml",
|
|
345
|
+
"table",
|
|
346
|
+
"group",
|
|
347
|
+
"groupCollapsed",
|
|
348
|
+
"groupEnd",
|
|
349
|
+
"time",
|
|
350
|
+
"timeEnd",
|
|
351
|
+
"timeLog",
|
|
352
|
+
"count",
|
|
353
|
+
"countReset",
|
|
354
|
+
"assert",
|
|
355
|
+
"clear",
|
|
356
|
+
"profile",
|
|
357
|
+
"profileEnd"
|
|
358
|
+
];
|
|
359
|
+
const DEFAULT_PRESERVE = ["[build git info]"];
|
|
360
|
+
const DEFAULT_PRESERVE_COMMENT = "@sybz-keep-console";
|
|
361
|
+
const SOURCE_RE = /\.(?:[cm]?[jt]sx?|vue)$/;
|
|
362
|
+
const SCRIPT_RE = /<script\b[^>]*>([\s\S]*?)<\/script>/g;
|
|
363
|
+
const matchesPattern = (source, pattern) => {
|
|
364
|
+
if (typeof pattern === "string") return source.includes(pattern);
|
|
365
|
+
pattern.lastIndex = 0;
|
|
366
|
+
return pattern.test(source);
|
|
367
|
+
};
|
|
368
|
+
const hasPreserveComment = (code, start, end, marker) => {
|
|
369
|
+
const lineStart = code.lastIndexOf("\n", start) + 1;
|
|
370
|
+
const lineEnd = code.indexOf("\n", end);
|
|
371
|
+
const currentLine = code.slice(lineStart, lineEnd === -1 ? code.length : lineEnd);
|
|
372
|
+
if (currentLine.includes(marker)) return true;
|
|
373
|
+
if (lineStart <= 1) return false;
|
|
374
|
+
const previousLineEnd = lineStart - 1;
|
|
375
|
+
const previousLineStart = code.lastIndexOf("\n", previousLineEnd - 1) + 1;
|
|
376
|
+
return code.slice(previousLineStart, previousLineEnd).includes(marker);
|
|
377
|
+
};
|
|
378
|
+
const removeConsoleCalls = (code, context) => {
|
|
379
|
+
const calls = findConsoleCalls(code, context.methods);
|
|
380
|
+
if (!calls.length) return code;
|
|
381
|
+
let result = code;
|
|
382
|
+
for (let index = calls.length - 1; index >= 0; index--) {
|
|
383
|
+
const call = calls[index];
|
|
384
|
+
const closeParen = findClosingParen(result, call.paren);
|
|
385
|
+
if (closeParen === -1 || context.preserveMethods.has(call.method)) continue;
|
|
386
|
+
const source = result.slice(call.start, closeParen + 1);
|
|
387
|
+
if (context.preserve.some((pattern) => matchesPattern(source, pattern))) continue;
|
|
388
|
+
if (context.comment && hasPreserveComment(result, call.start, closeParen, context.comment)) continue;
|
|
389
|
+
result = result.slice(0, call.start) + "void 0" + result.slice(closeParen + 1);
|
|
390
|
+
}
|
|
391
|
+
return result;
|
|
392
|
+
};
|
|
393
|
+
const removeConsoleFromVue = (code, context) => {
|
|
394
|
+
let output = "";
|
|
395
|
+
let cursor = 0;
|
|
396
|
+
let changed = false;
|
|
397
|
+
SCRIPT_RE.lastIndex = 0;
|
|
398
|
+
let match;
|
|
399
|
+
while (match = SCRIPT_RE.exec(code)) {
|
|
400
|
+
const scriptTag = match[0];
|
|
401
|
+
const content = match[1];
|
|
402
|
+
const contentStart = match.index + scriptTag.indexOf(">") + 1;
|
|
403
|
+
const transformed = removeConsoleCalls(content, context);
|
|
404
|
+
output += code.slice(cursor, contentStart) + transformed;
|
|
405
|
+
cursor = contentStart + content.length;
|
|
406
|
+
if (transformed !== content) changed = true;
|
|
407
|
+
}
|
|
408
|
+
if (!changed) return code;
|
|
409
|
+
return output + code.slice(cursor);
|
|
410
|
+
};
|
|
411
|
+
const createRemoveConsole = (options = {}) => {
|
|
412
|
+
var _a, _b, _c, _d;
|
|
413
|
+
let root = process.cwd();
|
|
414
|
+
const methods = new Set((_a = options.methods) != null ? _a : DEFAULT_METHODS);
|
|
415
|
+
const preserveMethods = new Set((_b = options.preserveMethods) != null ? _b : []);
|
|
416
|
+
const preserve = [...DEFAULT_PRESERVE, ...(_c = options.preserve) != null ? _c : []];
|
|
417
|
+
const comment = options.preserveComment === false ? false : (_d = options.preserveComment) != null ? _d : DEFAULT_PRESERVE_COMMENT;
|
|
418
|
+
const include = createMatcher(options.include);
|
|
419
|
+
const exclude = createMatcher(options.exclude);
|
|
420
|
+
return {
|
|
421
|
+
name: "sybz:remove-console",
|
|
422
|
+
apply: "build",
|
|
423
|
+
enforce: "pre",
|
|
424
|
+
configResolved(config) {
|
|
425
|
+
root = config.root;
|
|
426
|
+
},
|
|
427
|
+
transform(code, id) {
|
|
428
|
+
if (!code) return;
|
|
429
|
+
if (!id || id.startsWith("\0") || id.startsWith("virtual:")) return;
|
|
430
|
+
const normalizedId = normalizePath(id);
|
|
431
|
+
if (!options.transformNodeModules && normalizedId.includes("/node_modules/")) return;
|
|
432
|
+
if (normalizedId.includes("/.vitepress/cache/") || normalizedId.includes("/.vitepress/dist/")) return;
|
|
433
|
+
const cleanId = id.split("?")[0];
|
|
434
|
+
if (!cleanId || !isAbsolute(cleanId) || !SOURCE_RE.test(cleanId)) return;
|
|
435
|
+
if (cleanId.includes(".d.ts")) return;
|
|
436
|
+
const file = normalizePath(relative(root, cleanId));
|
|
437
|
+
if (!file || file.startsWith("..") || file.startsWith("/")) return;
|
|
438
|
+
if (include && !include(file) && !include(id)) return;
|
|
439
|
+
if ((exclude == null ? void 0 : exclude(file)) || (exclude == null ? void 0 : exclude(id))) return;
|
|
440
|
+
const context = { methods, preserveMethods, preserve, comment };
|
|
441
|
+
const transformed = cleanId.endsWith(".vue") ? removeConsoleFromVue(code, context) : removeConsoleCalls(code, context);
|
|
442
|
+
if (transformed === code) return;
|
|
443
|
+
return { code: transformed, map: null };
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const POSITION_STYLES = {
|
|
449
|
+
"top-left": "top:24px;left:24px;",
|
|
450
|
+
"top-right": "top:24px;right:24px;",
|
|
451
|
+
"bottom-left": "bottom:24px;left:24px;",
|
|
452
|
+
"bottom-right": "bottom:24px;right:24px;"
|
|
453
|
+
};
|
|
454
|
+
const formatBuildTime$1 = (date) => {
|
|
455
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
456
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(
|
|
457
|
+
date.getMinutes()
|
|
458
|
+
)}:${pad(date.getSeconds())}`;
|
|
459
|
+
};
|
|
460
|
+
const createVersion = () => `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
461
|
+
const joinBase = (base, file) => {
|
|
462
|
+
if (/^(?:[a-z]+:)?\/\//i.test(file) || file.startsWith("/")) return file;
|
|
463
|
+
const normalizedBase = base.endsWith("/") ? base : `${base}/`;
|
|
464
|
+
return `${normalizedBase}${file.replace(/^\.?\//, "")}`;
|
|
465
|
+
};
|
|
466
|
+
const createClientScript = (options) => {
|
|
467
|
+
var _a;
|
|
468
|
+
return `(function () {
|
|
469
|
+
var currentVersion = window.__SYBZ_VERSION__;
|
|
470
|
+
if (!currentVersion || typeof window.fetch !== 'function') return;
|
|
471
|
+
var versionUrl = ${JSON.stringify(options.versionUrl)};
|
|
472
|
+
var interval = ${JSON.stringify(options.interval)};
|
|
473
|
+
var text = ${JSON.stringify(options.text)};
|
|
474
|
+
var reloadText = ${JSON.stringify(options.reloadText)};
|
|
475
|
+
var positionStyle = ${JSON.stringify((_a = POSITION_STYLES[options.position]) != null ? _a : POSITION_STYLES["bottom-right"])};
|
|
476
|
+
var prompt = ${JSON.stringify(options.prompt)};
|
|
477
|
+
var eventName = ${JSON.stringify(options.eventName)};
|
|
478
|
+
var notified = false;
|
|
479
|
+
|
|
480
|
+
function showUpdate() {
|
|
481
|
+
if (!document.body) return;
|
|
482
|
+
|
|
483
|
+
var box = document.createElement('div');
|
|
484
|
+
box.setAttribute('role', 'status');
|
|
485
|
+
box.setAttribute('aria-live', 'polite');
|
|
486
|
+
box.style.cssText = 'position:fixed;' + positionStyle + 'box-sizing:border-box;z-index:99999;display:flex;align-items:center;gap:12px;padding:12px 16px;border:1px solid #e5e7eb;border-radius:10px;background:#fff;color:#1f2937;box-shadow:0 8px 24px rgba(15,23,42,.18);font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;';
|
|
487
|
+
|
|
488
|
+
var label = document.createElement('span');
|
|
489
|
+
label.textContent = text;
|
|
490
|
+
|
|
491
|
+
var button = document.createElement('button');
|
|
492
|
+
button.type = 'button';
|
|
493
|
+
button.textContent = reloadText;
|
|
494
|
+
button.style.cssText = 'flex:none;box-sizing:border-box;width:auto;border:0;border-radius:6px;background:#2f6fed;color:#fff;padding:8px 14px;font:inherit;cursor:pointer;';
|
|
495
|
+
button.onclick = function () { window.location.reload(); };
|
|
496
|
+
|
|
497
|
+
box.appendChild(label);
|
|
498
|
+
box.appendChild(button);
|
|
499
|
+
document.body.appendChild(box);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function notify(data) {
|
|
503
|
+
if (notified) return;
|
|
504
|
+
notified = true;
|
|
505
|
+
|
|
506
|
+
var detail = { version: data.version, currentVersion: currentVersion };
|
|
507
|
+
var event;
|
|
508
|
+
try {
|
|
509
|
+
event = new CustomEvent(eventName, { detail: detail, cancelable: true });
|
|
510
|
+
} catch (error) {
|
|
511
|
+
event = document.createEvent('CustomEvent');
|
|
512
|
+
event.initCustomEvent(eventName, false, true, detail);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
var handled = false;
|
|
516
|
+
try { handled = !window.dispatchEvent(event); } catch (error) {}
|
|
517
|
+
if (!handled && prompt) showUpdate();
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function checkVersion() {
|
|
521
|
+
if (notified) return;
|
|
522
|
+
var separator = versionUrl.indexOf('?') === -1 ? '?' : '&';
|
|
523
|
+
window.fetch(versionUrl + separator + 't=' + Date.now(), { cache: 'no-store' })
|
|
524
|
+
.then(function (response) { return response.ok ? response.json() : null; })
|
|
525
|
+
.then(function (data) {
|
|
526
|
+
if (data && data.version && data.version !== currentVersion) notify(data);
|
|
527
|
+
})
|
|
528
|
+
.catch(function () {});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
window.setInterval(checkVersion, interval);
|
|
532
|
+
window.setTimeout(checkVersion, 1000);
|
|
533
|
+
window.addEventListener('focus', checkVersion);
|
|
534
|
+
document.addEventListener('visibilitychange', function () {
|
|
535
|
+
if (!document.hidden) checkVersion();
|
|
536
|
+
});
|
|
537
|
+
})();`;
|
|
538
|
+
};
|
|
539
|
+
const createVersionCheck = (options = {}) => {
|
|
540
|
+
var _a, _b;
|
|
541
|
+
let version = (_a = options.version) != null ? _a : createVersion();
|
|
542
|
+
let buildTime = formatBuildTime$1(/* @__PURE__ */ new Date());
|
|
543
|
+
const interval = Math.max(1e3, Number(options.interval) || 5 * 60 * 1e3);
|
|
544
|
+
const versionFile = ((_b = options.versionFile) != null ? _b : "version.json").replace(/^\/+/, "");
|
|
545
|
+
let config;
|
|
546
|
+
let disabled = false;
|
|
547
|
+
return {
|
|
548
|
+
name: "sybz:version-check",
|
|
549
|
+
apply: "build",
|
|
550
|
+
configResolved(resolvedConfig) {
|
|
551
|
+
var _a2, _b2;
|
|
552
|
+
config = resolvedConfig;
|
|
553
|
+
disabled = Boolean(((_a2 = config.build) == null ? void 0 : _a2.lib) || ((_b2 = config.build) == null ? void 0 : _b2.ssr));
|
|
554
|
+
},
|
|
555
|
+
buildStart() {
|
|
556
|
+
if (!options.version) version = createVersion();
|
|
557
|
+
buildTime = formatBuildTime$1(/* @__PURE__ */ new Date());
|
|
558
|
+
},
|
|
559
|
+
generateBundle() {
|
|
560
|
+
if (disabled) return;
|
|
561
|
+
this.emitFile({
|
|
562
|
+
type: "asset",
|
|
563
|
+
fileName: versionFile,
|
|
564
|
+
source: JSON.stringify({ version, buildTime })
|
|
565
|
+
});
|
|
566
|
+
},
|
|
567
|
+
transformIndexHtml() {
|
|
568
|
+
var _a2, _b2, _c, _d, _e;
|
|
569
|
+
if (disabled) return;
|
|
570
|
+
const versionUrl = (_a2 = options.versionUrl) != null ? _a2 : joinBase((config == null ? void 0 : config.base) || "/", versionFile);
|
|
571
|
+
return [
|
|
572
|
+
{
|
|
573
|
+
tag: "meta",
|
|
574
|
+
attrs: { name: "sybz-version", content: version },
|
|
575
|
+
injectTo: "head"
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
tag: "script",
|
|
579
|
+
children: `window.__SYBZ_VERSION__ = ${JSON.stringify(version)};`,
|
|
580
|
+
injectTo: "head"
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
tag: "script",
|
|
584
|
+
children: createClientScript({
|
|
585
|
+
versionUrl,
|
|
586
|
+
interval,
|
|
587
|
+
text: (_b2 = options.text) != null ? _b2 : "\u53D1\u73B0\u65B0\u7248\u672C\uFF0C\u8BF7\u5237\u65B0\u9875\u9762",
|
|
588
|
+
reloadText: (_c = options.reloadText) != null ? _c : "\u5237\u65B0",
|
|
589
|
+
position: (_d = options.position) != null ? _d : "bottom-right",
|
|
590
|
+
prompt: options.prompt !== false,
|
|
591
|
+
eventName: (_e = options.eventName) != null ? _e : "sybz:version-update"
|
|
592
|
+
}),
|
|
593
|
+
injectTo: "body"
|
|
594
|
+
}
|
|
595
|
+
];
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
};
|
|
9
599
|
|
|
10
600
|
const require2 = createRequire(import.meta.url);
|
|
11
601
|
const virtualTailwindId = "virtual:sybz-tailwind";
|
|
12
602
|
const resolvedVirtualTailwindId = `\0${virtualTailwindId}`;
|
|
13
|
-
const
|
|
603
|
+
const virtualTailwindNoPreflightId = "virtual:sybz-tailwind-no-preflight.css";
|
|
604
|
+
const resolvedVirtualTailwindNoPreflightId = `\0${virtualTailwindNoPreflightId}`;
|
|
605
|
+
const createTailwindEntry = (preflight = false) => {
|
|
14
606
|
const tailwindCssPath = require2.resolve("tailwindcss/index.css");
|
|
607
|
+
const tailwindThemePath = require2.resolve("tailwindcss/theme.css");
|
|
608
|
+
const tailwindUtilitiesPath = require2.resolve("tailwindcss/utilities.css");
|
|
15
609
|
return {
|
|
16
610
|
name: "sybz-tailwind-entry",
|
|
17
611
|
resolveId(id) {
|
|
18
612
|
if (id === virtualTailwindId) return resolvedVirtualTailwindId;
|
|
613
|
+
if (id === virtualTailwindNoPreflightId) return resolvedVirtualTailwindNoPreflightId;
|
|
19
614
|
},
|
|
20
615
|
load(id) {
|
|
21
|
-
if (id === resolvedVirtualTailwindId)
|
|
616
|
+
if (id === resolvedVirtualTailwindId) {
|
|
617
|
+
return preflight ? `import ${JSON.stringify(tailwindCssPath)}` : `import ${JSON.stringify(virtualTailwindNoPreflightId)}`;
|
|
618
|
+
}
|
|
619
|
+
if (id === resolvedVirtualTailwindNoPreflightId) {
|
|
620
|
+
return [
|
|
621
|
+
`@import ${JSON.stringify(tailwindThemePath)} layer(theme);`,
|
|
622
|
+
`@import ${JSON.stringify(tailwindUtilitiesPath)} layer(utilities);`
|
|
623
|
+
].join("\n");
|
|
624
|
+
}
|
|
22
625
|
},
|
|
23
626
|
transformIndexHtml: {
|
|
24
627
|
order: "pre",
|
|
@@ -65,14 +668,79 @@ const createBuildTime = (metaName = "buildTime") => {
|
|
|
65
668
|
}
|
|
66
669
|
};
|
|
67
670
|
};
|
|
671
|
+
const POSTCSS_CONFIG_FILES = ["postcss.config.js", "postcss.config.cjs", "postcss.config.mjs", "postcss.config.ts"];
|
|
672
|
+
const loadLegacyPlugin = (options) => {
|
|
673
|
+
let mod;
|
|
674
|
+
try {
|
|
675
|
+
mod = require2("@vitejs/plugin-legacy");
|
|
676
|
+
} catch (e) {
|
|
677
|
+
throw new Error(
|
|
678
|
+
"[sybz-components] \u542F\u7528 compat \u9700\u8981\u5B89\u88C5\u4E0E\u5F53\u524D Vite \u7248\u672C\u5339\u914D\u7684 @vitejs/plugin-legacy\uFF0C\u4F8B\u5982\uFF1Apnpm add -D @vitejs/plugin-legacy"
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
const factory = typeof mod === "function" ? mod : mod.default;
|
|
682
|
+
if (!factory) {
|
|
683
|
+
throw new Error("[sybz-components] @vitejs/plugin-legacy \u5BFC\u51FA\u683C\u5F0F\u5F02\u5E38\uFF0C\u8BF7\u68C0\u67E5\u5B89\u88C5\u7248\u672C\u3002");
|
|
684
|
+
}
|
|
685
|
+
return factory(options);
|
|
686
|
+
};
|
|
687
|
+
const createCompatPlugin = (options) => ({
|
|
688
|
+
name: "sybz:compat",
|
|
689
|
+
enforce: "post",
|
|
690
|
+
config(config) {
|
|
691
|
+
var _a, _b, _c, _d;
|
|
692
|
+
if ((_a = config.build) == null ? void 0 : _a.lib) return;
|
|
693
|
+
const buildTarget = (_b = options.buildTarget) != null ? _b : "chrome60";
|
|
694
|
+
const result = {
|
|
695
|
+
build: {
|
|
696
|
+
target: buildTarget,
|
|
697
|
+
cssTarget: buildTarget
|
|
698
|
+
},
|
|
699
|
+
optimizeDeps: {
|
|
700
|
+
esbuildOptions: { target: buildTarget }
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
if (options.postcss !== false) {
|
|
704
|
+
if ((_c = config.css) == null ? void 0 : _c.postcss) {
|
|
705
|
+
console.warn(
|
|
706
|
+
"[sybz-components] \u5DF2\u5B58\u5728 css.postcss \u914D\u7F6E\uFF0C\u5DF2\u8DF3\u8FC7\u81EA\u52A8 PostCSS \u6CE8\u5165\uFF1B\u8BF7\u624B\u52A8\u5408\u5E76 createSybzCompatPostcssPlugins()\u3002"
|
|
707
|
+
);
|
|
708
|
+
} else {
|
|
709
|
+
const root = (_d = config.root) != null ? _d : process.cwd();
|
|
710
|
+
const hasPostcssConfig = POSTCSS_CONFIG_FILES.some((file) => existsSync(resolve(root, file)));
|
|
711
|
+
if (hasPostcssConfig) {
|
|
712
|
+
console.warn(
|
|
713
|
+
"[sybz-components] \u68C0\u6D4B\u5230 postcss.config\uFF0C\u5DF2\u8DF3\u8FC7\u81EA\u52A8 PostCSS \u6CE8\u5165\uFF1B\u8BF7\u624B\u52A8\u5408\u5E76 createSybzCompatPostcssPlugins()\u3002"
|
|
714
|
+
);
|
|
715
|
+
} else {
|
|
716
|
+
result.css = {
|
|
717
|
+
postcss: {
|
|
718
|
+
plugins: createSybzCompatPostcssPlugins({ browsers: options.browsers })
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return result;
|
|
725
|
+
}
|
|
726
|
+
});
|
|
68
727
|
const sybzVitePlugins = (options = {}) => {
|
|
728
|
+
var _a;
|
|
69
729
|
const plugins = [];
|
|
70
730
|
if (options.vueJsx !== false) {
|
|
71
731
|
plugins.push(vueJsx(typeof options.vueJsx === "object" ? options.vueJsx : {}));
|
|
72
732
|
}
|
|
73
733
|
if (options.tailwind !== false) {
|
|
74
734
|
plugins.push(...tailwindcss(typeof options.tailwind === "object" ? options.tailwind : {}));
|
|
75
|
-
if (options.tailwindCss !== false) plugins.push(createTailwindEntry());
|
|
735
|
+
if (options.tailwindCss !== false) plugins.push(createTailwindEntry(options.tailwindPreflight === true));
|
|
736
|
+
}
|
|
737
|
+
if (options.consoleSource) {
|
|
738
|
+
const consoleSourceOptions = typeof options.consoleSource === "object" ? options.consoleSource : {};
|
|
739
|
+
const inspectorOptions = typeof options.codeInspector === "object" ? options.codeInspector : void 0;
|
|
740
|
+
plugins.push(createConsoleSource({ editor: inspectorOptions == null ? void 0 : inspectorOptions.editor, ...consoleSourceOptions }));
|
|
741
|
+
}
|
|
742
|
+
if (options.removeConsole !== false) {
|
|
743
|
+
plugins.push(createRemoveConsole(typeof options.removeConsole === "object" ? options.removeConsole : {}));
|
|
76
744
|
}
|
|
77
745
|
if (options.codeInspector !== false) {
|
|
78
746
|
plugins.push(createCodeInspector(typeof options.codeInspector === "object" ? options.codeInspector : {}));
|
|
@@ -83,6 +751,18 @@ const sybzVitePlugins = (options = {}) => {
|
|
|
83
751
|
if (options.buildTime !== false) {
|
|
84
752
|
plugins.push(createBuildTime(typeof options.buildTime === "object" ? options.buildTime.metaName : void 0));
|
|
85
753
|
}
|
|
754
|
+
if (options.versionCheck !== false) {
|
|
755
|
+
plugins.push(createVersionCheck(typeof options.versionCheck === "object" ? options.versionCheck : {}));
|
|
756
|
+
}
|
|
757
|
+
if (options.compat) {
|
|
758
|
+
const compat = typeof options.compat === "object" ? options.compat : {};
|
|
759
|
+
plugins.push(createCompatPlugin(compat));
|
|
760
|
+
if (compat.legacy !== false) {
|
|
761
|
+
const legacy = compat.legacy && typeof compat.legacy !== "boolean" ? compat.legacy : loadLegacyPlugin({ targets: (_a = compat.targets) != null ? _a : SYBZ_COMPAT_BROWSERS });
|
|
762
|
+
if (Array.isArray(legacy)) plugins.push(...legacy);
|
|
763
|
+
else plugins.push(legacy);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
86
766
|
return plugins;
|
|
87
767
|
};
|
|
88
768
|
|