@weapp-vite/web 1.3.3 → 1.3.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.
- package/dist/index-w9xxIH-l.d.mts +1229 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -9712
- package/dist/plugin-CsqbKOh1.mjs +1555 -0
- package/dist/plugin-DP2iPVmw.d.mts +96 -0
- package/dist/plugin.d.mts +2 -0
- package/dist/plugin.mjs +2 -1932
- package/dist/runtime/index.d.mts +2 -0
- package/dist/runtime/index.mjs +2 -7786
- package/dist/runtime-xs_hRUwz.mjs +6247 -0
- package/dist/slugify-B4l45KNs.mjs +6 -0
- package/package.json +5 -5
- package/dist/index.d.ts +0 -5
- package/dist/plugin-D-Up1cFc.d.ts +0 -68
- package/dist/plugin.d.ts +0 -2
- package/dist/runtime/index.d.ts +0 -1607
package/dist/plugin.mjs
CHANGED
|
@@ -1,1932 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { extname as extname4, resolve as resolve3 } from "pathe";
|
|
4
|
-
|
|
5
|
-
// src/compiler/wxml/compile.ts
|
|
6
|
-
import { readFileSync } from "fs";
|
|
7
|
-
|
|
8
|
-
// src/compiler/wxml/dependency.ts
|
|
9
|
-
function createDependencyContext() {
|
|
10
|
-
return {
|
|
11
|
-
warnings: [],
|
|
12
|
-
dependencies: [],
|
|
13
|
-
dependencySet: /* @__PURE__ */ new Set(),
|
|
14
|
-
visited: /* @__PURE__ */ new Set(),
|
|
15
|
-
active: /* @__PURE__ */ new Set(),
|
|
16
|
-
circularWarnings: /* @__PURE__ */ new Set()
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function addDependency(value, context, direct) {
|
|
20
|
-
if (!context.dependencySet.has(value)) {
|
|
21
|
-
context.dependencySet.add(value);
|
|
22
|
-
context.dependencies.push(value);
|
|
23
|
-
direct?.push(value);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function warnReadTemplate(context, target) {
|
|
27
|
-
context.warnings.push(`[web] \u65E0\u6CD5\u8BFB\u53D6\u6A21\u677F\u4F9D\u8D56: ${target}`);
|
|
28
|
-
}
|
|
29
|
-
function warnCircularTemplate(context, from, target) {
|
|
30
|
-
const key = `${from}=>${target}`;
|
|
31
|
-
if (context.circularWarnings.has(key)) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
context.circularWarnings.add(key);
|
|
35
|
-
context.warnings.push(`[web] WXML \u5FAA\u73AF\u5F15\u7528: ${from} -> ${target}`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// src/compiler/wxml/navigation.ts
|
|
39
|
-
var NAVIGATION_BAR_ATTRS = /* @__PURE__ */ new Set([
|
|
40
|
-
"title",
|
|
41
|
-
"background-color",
|
|
42
|
-
"text-style",
|
|
43
|
-
"front-color",
|
|
44
|
-
"loading"
|
|
45
|
-
]);
|
|
46
|
-
function stripPageMetaNodes(nodes) {
|
|
47
|
-
const stripped = [];
|
|
48
|
-
for (const node of nodes) {
|
|
49
|
-
if (node.type === "element" && node.name === "page-meta") {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
if (node.type === "element" && node.children?.length) {
|
|
53
|
-
const nextChildren = stripPageMetaNodes(node.children);
|
|
54
|
-
if (nextChildren !== node.children) {
|
|
55
|
-
stripped.push({ ...node, children: nextChildren });
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
stripped.push(node);
|
|
60
|
-
}
|
|
61
|
-
return stripped;
|
|
62
|
-
}
|
|
63
|
-
function pickNavigationBarAttrs(attribs) {
|
|
64
|
-
if (!attribs) {
|
|
65
|
-
return void 0;
|
|
66
|
-
}
|
|
67
|
-
const picked = {};
|
|
68
|
-
for (const [key, value] of Object.entries(attribs)) {
|
|
69
|
-
if (NAVIGATION_BAR_ATTRS.has(key)) {
|
|
70
|
-
picked[key] = value;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return Object.keys(picked).length > 0 ? picked : void 0;
|
|
74
|
-
}
|
|
75
|
-
function findNavigationBarInPageMeta(node) {
|
|
76
|
-
const children = node.children ?? [];
|
|
77
|
-
for (const child of children) {
|
|
78
|
-
if (child.type === "element" && child.name === "navigation-bar") {
|
|
79
|
-
return child;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return void 0;
|
|
83
|
-
}
|
|
84
|
-
function extractNavigationBarFromPageMeta(nodes) {
|
|
85
|
-
let pageMetaIndex = -1;
|
|
86
|
-
let navigationBar;
|
|
87
|
-
for (let i = 0; i < nodes.length; i += 1) {
|
|
88
|
-
const node = nodes[i];
|
|
89
|
-
if (node.type === "element" && node.name === "page-meta") {
|
|
90
|
-
if (pageMetaIndex === -1) {
|
|
91
|
-
pageMetaIndex = i;
|
|
92
|
-
}
|
|
93
|
-
if (!navigationBar) {
|
|
94
|
-
navigationBar = findNavigationBarInPageMeta(node);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const warnings = [];
|
|
99
|
-
if (pageMetaIndex > 0) {
|
|
100
|
-
warnings.push("[web] page-meta \u9700\u8981\u4F5C\u4E3A\u9875\u9762\u7B2C\u4E00\u4E2A\u8282\u70B9\uFF0C\u5DF2\u5FFD\u7565\u5176\u4F4D\u7F6E\u7EA6\u675F\u3002");
|
|
101
|
-
}
|
|
102
|
-
const cleaned = pageMetaIndex === -1 ? nodes : stripPageMetaNodes(nodes);
|
|
103
|
-
const attrs = navigationBar ? pickNavigationBarAttrs(navigationBar.attribs) : void 0;
|
|
104
|
-
return { nodes: cleaned, attrs, warnings };
|
|
105
|
-
}
|
|
106
|
-
function toAttributeValue(value) {
|
|
107
|
-
if (value == null) {
|
|
108
|
-
return void 0;
|
|
109
|
-
}
|
|
110
|
-
if (typeof value === "string") {
|
|
111
|
-
return value;
|
|
112
|
-
}
|
|
113
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
114
|
-
return String(value);
|
|
115
|
-
}
|
|
116
|
-
return void 0;
|
|
117
|
-
}
|
|
118
|
-
function buildNavigationBarAttrs(config, overrides) {
|
|
119
|
-
const attrs = {};
|
|
120
|
-
if (config?.title !== void 0) {
|
|
121
|
-
const value = toAttributeValue(config.title);
|
|
122
|
-
if (value !== void 0) {
|
|
123
|
-
attrs.title = value;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (config?.backgroundColor !== void 0) {
|
|
127
|
-
const value = toAttributeValue(config.backgroundColor);
|
|
128
|
-
if (value !== void 0) {
|
|
129
|
-
attrs["background-color"] = value;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (config?.textStyle !== void 0) {
|
|
133
|
-
const value = toAttributeValue(config.textStyle);
|
|
134
|
-
if (value !== void 0) {
|
|
135
|
-
attrs["text-style"] = value;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (config?.frontColor !== void 0) {
|
|
139
|
-
const value = toAttributeValue(config.frontColor);
|
|
140
|
-
if (value !== void 0) {
|
|
141
|
-
attrs["front-color"] = value;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (config?.loading) {
|
|
145
|
-
attrs.loading = "true";
|
|
146
|
-
}
|
|
147
|
-
if (overrides) {
|
|
148
|
-
for (const [key, value] of Object.entries(overrides)) {
|
|
149
|
-
attrs[key] = value;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return attrs;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// src/compiler/wxml/parser.ts
|
|
156
|
-
import { parseDocument } from "htmlparser2";
|
|
157
|
-
function isRenderableNode(node) {
|
|
158
|
-
if (node.type === "directive" || node.type === "comment") {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
if (node.type === "text") {
|
|
162
|
-
const data = node.data ?? "";
|
|
163
|
-
return data.trim().length > 0;
|
|
164
|
-
}
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
function hasChildren(node) {
|
|
168
|
-
return Array.isArray(node.children);
|
|
169
|
-
}
|
|
170
|
-
function toRenderNode(node, children) {
|
|
171
|
-
if (node.type === "text") {
|
|
172
|
-
const data = node.data ?? "";
|
|
173
|
-
return { type: "text", data };
|
|
174
|
-
}
|
|
175
|
-
if (node.type === "tag" || node.type === "script" || node.type === "style") {
|
|
176
|
-
const element = node;
|
|
177
|
-
return {
|
|
178
|
-
type: "element",
|
|
179
|
-
name: element.name,
|
|
180
|
-
attribs: element.attribs ?? {},
|
|
181
|
-
children
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
return void 0;
|
|
185
|
-
}
|
|
186
|
-
function convertNode(node) {
|
|
187
|
-
if (!isRenderableNode(node)) {
|
|
188
|
-
return void 0;
|
|
189
|
-
}
|
|
190
|
-
const children = hasChildren(node) && node.children.length > 0 ? node.children.map((child) => convertNode(child)).filter((child) => Boolean(child)) : void 0;
|
|
191
|
-
return toRenderNode(node, children);
|
|
192
|
-
}
|
|
193
|
-
function parseWxml(source) {
|
|
194
|
-
const document = parseDocument(source, {
|
|
195
|
-
xmlMode: true,
|
|
196
|
-
decodeEntities: true,
|
|
197
|
-
recognizeSelfClosing: true
|
|
198
|
-
});
|
|
199
|
-
const nodes = (document.children ?? []).filter(isRenderableNode);
|
|
200
|
-
return nodes.map((node) => convertNode(node)).filter((node) => Boolean(node));
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// src/shared/wxml.ts
|
|
204
|
-
var CONTROL_ATTRS = /* @__PURE__ */ new Set([
|
|
205
|
-
"wx:if",
|
|
206
|
-
"wx:elif",
|
|
207
|
-
"wx:else",
|
|
208
|
-
"wx:for",
|
|
209
|
-
"wx:for-item",
|
|
210
|
-
"wx:for-index",
|
|
211
|
-
"wx:key"
|
|
212
|
-
]);
|
|
213
|
-
var EVENT_PREFIX_RE = /^(?:bind|catch|mut-bind|capture-bind|capture-catch)([\w-]+)$/;
|
|
214
|
-
var EVENT_KIND_ALIAS = {
|
|
215
|
-
tap: "click"
|
|
216
|
-
};
|
|
217
|
-
var SELF_CLOSING_TAGS = /* @__PURE__ */ new Set([
|
|
218
|
-
"area",
|
|
219
|
-
"base",
|
|
220
|
-
"br",
|
|
221
|
-
"col",
|
|
222
|
-
"embed",
|
|
223
|
-
"hr",
|
|
224
|
-
"img",
|
|
225
|
-
"image",
|
|
226
|
-
"input",
|
|
227
|
-
"link",
|
|
228
|
-
"meta",
|
|
229
|
-
"param",
|
|
230
|
-
"source",
|
|
231
|
-
"track",
|
|
232
|
-
"wbr"
|
|
233
|
-
]);
|
|
234
|
-
function normalizeTagName(name) {
|
|
235
|
-
switch (name) {
|
|
236
|
-
case "view":
|
|
237
|
-
case "cover-view":
|
|
238
|
-
case "navigator":
|
|
239
|
-
case "scroll-view":
|
|
240
|
-
case "swiper":
|
|
241
|
-
case "swiper-item":
|
|
242
|
-
case "movable-area":
|
|
243
|
-
case "movable-view":
|
|
244
|
-
case "cover-image":
|
|
245
|
-
return "div";
|
|
246
|
-
case "text":
|
|
247
|
-
case "icon":
|
|
248
|
-
return "span";
|
|
249
|
-
case "image":
|
|
250
|
-
return "img";
|
|
251
|
-
case "button":
|
|
252
|
-
return "weapp-button";
|
|
253
|
-
case "input":
|
|
254
|
-
return "input";
|
|
255
|
-
case "textarea":
|
|
256
|
-
return "textarea";
|
|
257
|
-
case "form":
|
|
258
|
-
return "form";
|
|
259
|
-
case "label":
|
|
260
|
-
return "label";
|
|
261
|
-
case "picker":
|
|
262
|
-
case "picker-view":
|
|
263
|
-
return "select";
|
|
264
|
-
case "block":
|
|
265
|
-
return "#fragment";
|
|
266
|
-
case "slot":
|
|
267
|
-
return "slot";
|
|
268
|
-
default:
|
|
269
|
-
return name || "div";
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
function normalizeAttributeName(name) {
|
|
273
|
-
if (name === "class" || name === "style" || name.startsWith("data-")) {
|
|
274
|
-
return name;
|
|
275
|
-
}
|
|
276
|
-
if (name === "hover-class") {
|
|
277
|
-
return "data-hover-class";
|
|
278
|
-
}
|
|
279
|
-
return name.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// src/compiler/wxml/interpolation.ts
|
|
283
|
-
function parseInterpolations(value) {
|
|
284
|
-
const parts = [];
|
|
285
|
-
if (!value.includes("{{")) {
|
|
286
|
-
return [{ type: "text", value }];
|
|
287
|
-
}
|
|
288
|
-
let cursor = 0;
|
|
289
|
-
while (cursor < value.length) {
|
|
290
|
-
const start = value.indexOf("{{", cursor);
|
|
291
|
-
if (start === -1) {
|
|
292
|
-
parts.push({ type: "text", value: value.slice(cursor) });
|
|
293
|
-
break;
|
|
294
|
-
}
|
|
295
|
-
if (start > cursor) {
|
|
296
|
-
parts.push({ type: "text", value: value.slice(cursor, start) });
|
|
297
|
-
}
|
|
298
|
-
const end = value.indexOf("}}", start + 2);
|
|
299
|
-
if (end === -1) {
|
|
300
|
-
parts.push({ type: "text", value: value.slice(start) });
|
|
301
|
-
break;
|
|
302
|
-
}
|
|
303
|
-
const expr = value.slice(start + 2, end).trim();
|
|
304
|
-
if (expr) {
|
|
305
|
-
parts.push({ type: "expr", value: expr });
|
|
306
|
-
}
|
|
307
|
-
cursor = end + 2;
|
|
308
|
-
}
|
|
309
|
-
return parts;
|
|
310
|
-
}
|
|
311
|
-
function buildExpression(parts, scopeVar, wxsVar) {
|
|
312
|
-
if (parts.length === 0) {
|
|
313
|
-
return '""';
|
|
314
|
-
}
|
|
315
|
-
if (parts.length === 1 && parts[0]?.type === "text") {
|
|
316
|
-
return JSON.stringify(parts[0].value);
|
|
317
|
-
}
|
|
318
|
-
if (parts.length === 1 && parts[0]?.type === "expr") {
|
|
319
|
-
return `ctx.eval(${JSON.stringify(parts[0].value)}, ${scopeVar}, ${wxsVar})`;
|
|
320
|
-
}
|
|
321
|
-
const segments = parts.map((part) => {
|
|
322
|
-
if (part.type === "text") {
|
|
323
|
-
return JSON.stringify(part.value);
|
|
324
|
-
}
|
|
325
|
-
return `ctx.eval(${JSON.stringify(part.value)}, ${scopeVar}, ${wxsVar})`;
|
|
326
|
-
});
|
|
327
|
-
return `(${segments.join(" + ")})`;
|
|
328
|
-
}
|
|
329
|
-
function hasTopLevelColon(expression) {
|
|
330
|
-
let depth = 0;
|
|
331
|
-
let inSingleQuote = false;
|
|
332
|
-
let inDoubleQuote = false;
|
|
333
|
-
let inTemplate = false;
|
|
334
|
-
let escaped = false;
|
|
335
|
-
let sawTopLevelQuestion = false;
|
|
336
|
-
for (let index = 0; index < expression.length; index += 1) {
|
|
337
|
-
const char = expression[index];
|
|
338
|
-
if (inSingleQuote) {
|
|
339
|
-
if (escaped) {
|
|
340
|
-
escaped = false;
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
if (char === "\\") {
|
|
344
|
-
escaped = true;
|
|
345
|
-
continue;
|
|
346
|
-
}
|
|
347
|
-
if (char === "'") {
|
|
348
|
-
inSingleQuote = false;
|
|
349
|
-
}
|
|
350
|
-
continue;
|
|
351
|
-
}
|
|
352
|
-
if (inDoubleQuote) {
|
|
353
|
-
if (escaped) {
|
|
354
|
-
escaped = false;
|
|
355
|
-
continue;
|
|
356
|
-
}
|
|
357
|
-
if (char === "\\") {
|
|
358
|
-
escaped = true;
|
|
359
|
-
continue;
|
|
360
|
-
}
|
|
361
|
-
if (char === '"') {
|
|
362
|
-
inDoubleQuote = false;
|
|
363
|
-
}
|
|
364
|
-
continue;
|
|
365
|
-
}
|
|
366
|
-
if (inTemplate) {
|
|
367
|
-
if (escaped) {
|
|
368
|
-
escaped = false;
|
|
369
|
-
continue;
|
|
370
|
-
}
|
|
371
|
-
if (char === "\\") {
|
|
372
|
-
escaped = true;
|
|
373
|
-
continue;
|
|
374
|
-
}
|
|
375
|
-
if (char === "`") {
|
|
376
|
-
inTemplate = false;
|
|
377
|
-
}
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
if (char === "'") {
|
|
381
|
-
inSingleQuote = true;
|
|
382
|
-
continue;
|
|
383
|
-
}
|
|
384
|
-
if (char === '"') {
|
|
385
|
-
inDoubleQuote = true;
|
|
386
|
-
continue;
|
|
387
|
-
}
|
|
388
|
-
if (char === "`") {
|
|
389
|
-
inTemplate = true;
|
|
390
|
-
continue;
|
|
391
|
-
}
|
|
392
|
-
if (char === "(" || char === "[" || char === "{") {
|
|
393
|
-
depth += 1;
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
if (char === ")" || char === "]" || char === "}") {
|
|
397
|
-
depth = Math.max(0, depth - 1);
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
if (depth !== 0) {
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
if (char === "?") {
|
|
404
|
-
sawTopLevelQuestion = true;
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
if (char === ":") {
|
|
408
|
-
return !sawTopLevelQuestion;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
return false;
|
|
412
|
-
}
|
|
413
|
-
function shouldWrapShorthandObject(expression) {
|
|
414
|
-
const trimmed = expression.trim();
|
|
415
|
-
if (!trimmed) {
|
|
416
|
-
return false;
|
|
417
|
-
}
|
|
418
|
-
if (trimmed.startsWith("{") || trimmed.startsWith("[") || trimmed.startsWith("(")) {
|
|
419
|
-
return false;
|
|
420
|
-
}
|
|
421
|
-
return hasTopLevelColon(trimmed);
|
|
422
|
-
}
|
|
423
|
-
function buildTemplateDataExpression(raw, scopeVar, wxsVar) {
|
|
424
|
-
const trimmed = raw.trim();
|
|
425
|
-
const parts = parseInterpolations(trimmed);
|
|
426
|
-
if (parts.length === 1 && parts[0]?.type === "expr") {
|
|
427
|
-
const expr = parts[0].value.trim();
|
|
428
|
-
if (expr) {
|
|
429
|
-
const normalizedExpr = shouldWrapShorthandObject(expr) ? `{ ${expr} }` : expr;
|
|
430
|
-
return buildExpression([{ type: "expr", value: normalizedExpr }], scopeVar, wxsVar);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
return buildExpression(parseInterpolations(raw), scopeVar, wxsVar);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
// src/compiler/wxml/attributes.ts
|
|
437
|
-
function extractFor(attribs) {
|
|
438
|
-
const expr = attribs["wx:for"];
|
|
439
|
-
const itemName = attribs["wx:for-item"]?.trim() || "item";
|
|
440
|
-
let indexName = attribs["wx:for-index"]?.trim() || "index";
|
|
441
|
-
const key = attribs["wx:key"];
|
|
442
|
-
const restAttribs = {};
|
|
443
|
-
for (const [name, value] of Object.entries(attribs)) {
|
|
444
|
-
if (CONTROL_ATTRS.has(name)) {
|
|
445
|
-
continue;
|
|
446
|
-
}
|
|
447
|
-
restAttribs[name] = value;
|
|
448
|
-
}
|
|
449
|
-
if (itemName === indexName) {
|
|
450
|
-
indexName = `${indexName}Index`;
|
|
451
|
-
}
|
|
452
|
-
return { expr, itemName, indexName, key, restAttribs };
|
|
453
|
-
}
|
|
454
|
-
function isConditionalElement(node) {
|
|
455
|
-
if (node.type !== "element") {
|
|
456
|
-
return false;
|
|
457
|
-
}
|
|
458
|
-
const attribs = node.attribs ?? {};
|
|
459
|
-
return "wx:if" in attribs || "wx:elif" in attribs || "wx:else" in attribs;
|
|
460
|
-
}
|
|
461
|
-
function stripControlAttributes(attribs) {
|
|
462
|
-
const result = {};
|
|
463
|
-
for (const [name, value] of Object.entries(attribs)) {
|
|
464
|
-
if (!CONTROL_ATTRS.has(name)) {
|
|
465
|
-
result[name] = value;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
return result;
|
|
469
|
-
}
|
|
470
|
-
function parseEventAttribute(name) {
|
|
471
|
-
if (name.includes(":")) {
|
|
472
|
-
const [prefix, rawEvent] = name.split(":", 2);
|
|
473
|
-
return { prefix, rawEvent };
|
|
474
|
-
}
|
|
475
|
-
const match = EVENT_PREFIX_RE.exec(name);
|
|
476
|
-
if (!match) {
|
|
477
|
-
return void 0;
|
|
478
|
-
}
|
|
479
|
-
return { prefix: name.slice(0, name.length - match[1].length), rawEvent: match[1] };
|
|
480
|
-
}
|
|
481
|
-
function resolveComponentTagName(name, componentTags) {
|
|
482
|
-
if (!componentTags) {
|
|
483
|
-
return void 0;
|
|
484
|
-
}
|
|
485
|
-
return componentTags[name] ?? componentTags[name.toLowerCase()];
|
|
486
|
-
}
|
|
487
|
-
var PROPERTY_BIND_EXCLUSIONS = /* @__PURE__ */ new Set(["class", "style", "id", "slot"]);
|
|
488
|
-
function shouldBindAsProperty(name) {
|
|
489
|
-
if (PROPERTY_BIND_EXCLUSIONS.has(name)) {
|
|
490
|
-
return false;
|
|
491
|
-
}
|
|
492
|
-
if (name.startsWith("data-") || name.startsWith("aria-")) {
|
|
493
|
-
return false;
|
|
494
|
-
}
|
|
495
|
-
return true;
|
|
496
|
-
}
|
|
497
|
-
function normalizePropertyName(name) {
|
|
498
|
-
return name.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
499
|
-
}
|
|
500
|
-
function renderAttributes(attribs, scopeVar, wxsVar, options) {
|
|
501
|
-
let buffer = "";
|
|
502
|
-
for (const [rawName, rawValue] of Object.entries(attribs)) {
|
|
503
|
-
if (options?.skipControl && CONTROL_ATTRS.has(rawName)) {
|
|
504
|
-
continue;
|
|
505
|
-
}
|
|
506
|
-
const eventInfo = parseEventAttribute(rawName);
|
|
507
|
-
if (eventInfo) {
|
|
508
|
-
const event = eventInfo.rawEvent.toLowerCase();
|
|
509
|
-
const handlerExpr = buildExpression(parseInterpolations(rawValue ?? ""), scopeVar, wxsVar);
|
|
510
|
-
const domEvent = EVENT_KIND_ALIAS[event] ?? event;
|
|
511
|
-
const flags = {
|
|
512
|
-
catch: eventInfo.prefix.includes("catch"),
|
|
513
|
-
capture: eventInfo.prefix.includes("capture")
|
|
514
|
-
};
|
|
515
|
-
buffer += ` @${domEvent}=\${ctx.event(${JSON.stringify(event)}, ${handlerExpr}, ${scopeVar}, ${wxsVar}, ${JSON.stringify(flags)})}`;
|
|
516
|
-
continue;
|
|
517
|
-
}
|
|
518
|
-
const useProperty = options?.preferProperty && shouldBindAsProperty(rawName);
|
|
519
|
-
const name = useProperty ? normalizePropertyName(rawName) : normalizeAttributeName(rawName);
|
|
520
|
-
const expr = buildExpression(parseInterpolations(rawValue ?? ""), scopeVar, wxsVar);
|
|
521
|
-
buffer += ` ${useProperty ? "." : ""}${name}=\${${expr}}`;
|
|
522
|
-
}
|
|
523
|
-
return buffer;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
// src/compiler/wxml/renderer.ts
|
|
527
|
-
var Renderer = class {
|
|
528
|
-
renderNodes(nodes, scopeVar, wxsVar, componentTags) {
|
|
529
|
-
const parts = [];
|
|
530
|
-
for (let index = 0; index < nodes.length; index += 1) {
|
|
531
|
-
const node = nodes[index];
|
|
532
|
-
if (isConditionalElement(node)) {
|
|
533
|
-
const { rendered, endIndex } = this.renderConditionalSequence(
|
|
534
|
-
nodes,
|
|
535
|
-
index,
|
|
536
|
-
scopeVar,
|
|
537
|
-
wxsVar,
|
|
538
|
-
componentTags
|
|
539
|
-
);
|
|
540
|
-
parts.push(rendered);
|
|
541
|
-
index = endIndex;
|
|
542
|
-
continue;
|
|
543
|
-
}
|
|
544
|
-
parts.push(this.renderNode(node, scopeVar, wxsVar, componentTags));
|
|
545
|
-
}
|
|
546
|
-
if (parts.length === 0) {
|
|
547
|
-
return '""';
|
|
548
|
-
}
|
|
549
|
-
if (parts.length === 1) {
|
|
550
|
-
return parts[0];
|
|
551
|
-
}
|
|
552
|
-
return `[${parts.join(", ")}]`;
|
|
553
|
-
}
|
|
554
|
-
renderConditionalSequence(nodes, startIndex, scopeVar, wxsVar, componentTags) {
|
|
555
|
-
const branches = [];
|
|
556
|
-
let cursor = startIndex;
|
|
557
|
-
while (cursor < nodes.length) {
|
|
558
|
-
const candidate = nodes[cursor];
|
|
559
|
-
if (!isConditionalElement(candidate)) {
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
562
|
-
const attribs = candidate.attribs ?? {};
|
|
563
|
-
if (branches.length === 0 && !("wx:if" in attribs)) {
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
if (branches.length > 0 && !("wx:elif" in attribs) && !("wx:else" in attribs)) {
|
|
567
|
-
break;
|
|
568
|
-
}
|
|
569
|
-
branches.push({ node: candidate, attribs });
|
|
570
|
-
cursor += 1;
|
|
571
|
-
if ("wx:else" in attribs) {
|
|
572
|
-
break;
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
if (!branches.length) {
|
|
576
|
-
const node = nodes[startIndex];
|
|
577
|
-
if (!node) {
|
|
578
|
-
return { rendered: '""', endIndex: startIndex };
|
|
579
|
-
}
|
|
580
|
-
return { rendered: this.renderNode(node, scopeVar, wxsVar, componentTags), endIndex: startIndex };
|
|
581
|
-
}
|
|
582
|
-
let expr = '""';
|
|
583
|
-
for (let index = branches.length - 1; index >= 0; index -= 1) {
|
|
584
|
-
const { node, attribs } = branches[index];
|
|
585
|
-
const cleanedAttribs = stripControlAttributes(attribs);
|
|
586
|
-
if ("wx:else" in attribs) {
|
|
587
|
-
expr = this.renderElement(node, scopeVar, wxsVar, componentTags, { overrideAttribs: cleanedAttribs });
|
|
588
|
-
continue;
|
|
589
|
-
}
|
|
590
|
-
const conditionExpr = attribs["wx:if"] ?? attribs["wx:elif"] ?? "";
|
|
591
|
-
const rendered = this.renderElement(node, scopeVar, wxsVar, componentTags, { overrideAttribs: cleanedAttribs });
|
|
592
|
-
const condition = buildExpression(parseInterpolations(conditionExpr), scopeVar, wxsVar);
|
|
593
|
-
expr = `(${condition} ? ${rendered} : ${expr})`;
|
|
594
|
-
}
|
|
595
|
-
return { rendered: expr, endIndex: startIndex + branches.length - 1 };
|
|
596
|
-
}
|
|
597
|
-
renderNode(node, scopeVar, wxsVar, componentTags) {
|
|
598
|
-
if (node.type === "text") {
|
|
599
|
-
const parts = parseInterpolations(node.data ?? "");
|
|
600
|
-
return buildExpression(parts, scopeVar, wxsVar);
|
|
601
|
-
}
|
|
602
|
-
if (node.type === "element") {
|
|
603
|
-
if (node.name === "template" && node.attribs?.is) {
|
|
604
|
-
return this.renderTemplateInvoke(node, scopeVar, wxsVar);
|
|
605
|
-
}
|
|
606
|
-
return this.renderElement(node, scopeVar, wxsVar, componentTags);
|
|
607
|
-
}
|
|
608
|
-
return '""';
|
|
609
|
-
}
|
|
610
|
-
renderTemplateInvoke(node, scopeVar, wxsVar) {
|
|
611
|
-
const attribs = node.attribs ?? {};
|
|
612
|
-
const isExpr = buildExpression(parseInterpolations(attribs.is ?? ""), scopeVar, wxsVar);
|
|
613
|
-
const dataExpr = attribs.data ? buildTemplateDataExpression(attribs.data, scopeVar, wxsVar) : void 0;
|
|
614
|
-
const scopeExpr = dataExpr ? `ctx.mergeScope(${scopeVar}, ${dataExpr})` : scopeVar;
|
|
615
|
-
return `ctx.renderTemplate(__templates, ${isExpr}, ${scopeExpr}, ctx)`;
|
|
616
|
-
}
|
|
617
|
-
renderElement(node, scopeVar, wxsVar, componentTags, options = {}) {
|
|
618
|
-
const attribs = options.overrideAttribs ?? node.attribs ?? {};
|
|
619
|
-
if (!options.skipFor) {
|
|
620
|
-
const forInfo = extractFor(node.attribs ?? {});
|
|
621
|
-
if (forInfo.expr) {
|
|
622
|
-
const listExpression = buildExpression(parseInterpolations(forInfo.expr), scopeVar, wxsVar);
|
|
623
|
-
const listExpr = `ctx.normalizeList(${listExpression})`;
|
|
624
|
-
const itemVar = forInfo.itemName;
|
|
625
|
-
const indexVar = forInfo.indexName;
|
|
626
|
-
const scopeExpr = `ctx.createScope(${scopeVar}, { ${itemVar}: ${itemVar}, ${indexVar}: ${indexVar} })`;
|
|
627
|
-
const itemRender = this.renderElement(
|
|
628
|
-
node,
|
|
629
|
-
"__scope",
|
|
630
|
-
wxsVar,
|
|
631
|
-
componentTags,
|
|
632
|
-
{ skipFor: true, overrideAttribs: forInfo.restAttribs }
|
|
633
|
-
);
|
|
634
|
-
const keyExpr = `ctx.key(${JSON.stringify(forInfo.key ?? "")}, ${itemVar}, ${indexVar}, ${scopeExpr}, ${wxsVar})`;
|
|
635
|
-
return `repeat(${listExpr}, (${itemVar}, ${indexVar}) => ${keyExpr}, (${itemVar}, ${indexVar}) => { const __scope = ${scopeExpr}; return ${itemRender}; })`;
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
const customTag = resolveComponentTagName(node.name ?? "", componentTags);
|
|
639
|
-
const tagName = customTag ?? normalizeTagName(node.name ?? "");
|
|
640
|
-
if (tagName === "#fragment") {
|
|
641
|
-
return this.renderNodes(node.children ?? [], scopeVar, wxsVar, componentTags);
|
|
642
|
-
}
|
|
643
|
-
const attrs = renderAttributes(attribs, scopeVar, wxsVar, {
|
|
644
|
-
skipControl: true,
|
|
645
|
-
preferProperty: Boolean(customTag)
|
|
646
|
-
});
|
|
647
|
-
const childNodes = node.children ?? [];
|
|
648
|
-
const children = childNodes.map((child) => `\${${this.renderNode(child, scopeVar, wxsVar, componentTags)}}`).join("");
|
|
649
|
-
if (SELF_CLOSING_TAGS.has(tagName) && childNodes.length === 0) {
|
|
650
|
-
return `html\`<${tagName}${attrs} />\``;
|
|
651
|
-
}
|
|
652
|
-
return `html\`<${tagName}${attrs}>${children}</${tagName}>\``;
|
|
653
|
-
}
|
|
654
|
-
};
|
|
655
|
-
var renderer = new Renderer();
|
|
656
|
-
|
|
657
|
-
// src/compiler/wxml/specialNodes.ts
|
|
658
|
-
import { dirname, relative } from "pathe";
|
|
659
|
-
function shouldMarkWxsImport(pathname) {
|
|
660
|
-
const lower = pathname.toLowerCase();
|
|
661
|
-
if (lower.endsWith(".wxs") || lower.endsWith(".wxs.ts") || lower.endsWith(".wxs.js")) {
|
|
662
|
-
return false;
|
|
663
|
-
}
|
|
664
|
-
return lower.endsWith(".ts") || lower.endsWith(".js");
|
|
665
|
-
}
|
|
666
|
-
function collectSpecialNodes(nodes, context) {
|
|
667
|
-
const renderable = [];
|
|
668
|
-
for (const node of nodes) {
|
|
669
|
-
if (node.type === "element") {
|
|
670
|
-
const name = node.name ?? "";
|
|
671
|
-
if (name === "template" && node.attribs?.name) {
|
|
672
|
-
context.templates.push({
|
|
673
|
-
name: node.attribs.name,
|
|
674
|
-
nodes: collectSpecialNodes(node.children ?? [], context)
|
|
675
|
-
});
|
|
676
|
-
continue;
|
|
677
|
-
}
|
|
678
|
-
if ((name === "import" || name === "wx-import") && node.attribs?.src) {
|
|
679
|
-
const resolved = context.resolveTemplate(node.attribs.src);
|
|
680
|
-
if (resolved) {
|
|
681
|
-
context.imports.push({
|
|
682
|
-
id: resolved,
|
|
683
|
-
importName: `__wxml_import_${context.imports.length}`
|
|
684
|
-
});
|
|
685
|
-
} else {
|
|
686
|
-
context.warnings.push(`[web] \u65E0\u6CD5\u89E3\u6790\u6A21\u677F\u4F9D\u8D56: ${node.attribs.src} (from ${context.sourceId})`);
|
|
687
|
-
}
|
|
688
|
-
continue;
|
|
689
|
-
}
|
|
690
|
-
if ((name === "include" || name === "wx-include") && node.attribs?.src) {
|
|
691
|
-
const resolved = context.resolveTemplate(node.attribs.src);
|
|
692
|
-
if (resolved) {
|
|
693
|
-
context.includes.push({
|
|
694
|
-
id: resolved,
|
|
695
|
-
importName: `__wxml_include_${context.includes.length}`
|
|
696
|
-
});
|
|
697
|
-
} else {
|
|
698
|
-
context.warnings.push(`[web] \u65E0\u6CD5\u89E3\u6790\u6A21\u677F\u4F9D\u8D56: ${node.attribs.src} (from ${context.sourceId})`);
|
|
699
|
-
}
|
|
700
|
-
continue;
|
|
701
|
-
}
|
|
702
|
-
if (name === "wxs") {
|
|
703
|
-
const moduleName = node.attribs?.module?.trim();
|
|
704
|
-
if (moduleName) {
|
|
705
|
-
const previousSource = context.wxsModules.get(moduleName);
|
|
706
|
-
if (previousSource) {
|
|
707
|
-
context.warnings.push(`[web] WXS \u6A21\u5757\u540D\u91CD\u590D: ${moduleName} (from ${context.sourceId})`);
|
|
708
|
-
}
|
|
709
|
-
context.wxsModules.set(moduleName, context.sourceId);
|
|
710
|
-
if (node.attribs?.src) {
|
|
711
|
-
const resolved = context.resolveWxs(node.attribs.src);
|
|
712
|
-
if (resolved) {
|
|
713
|
-
context.wxs.push({
|
|
714
|
-
module: moduleName,
|
|
715
|
-
kind: "src",
|
|
716
|
-
importName: `__wxs_${context.wxs.length}`,
|
|
717
|
-
value: resolved
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
} else {
|
|
721
|
-
const inlineCode = (node.children ?? []).filter((child) => child.type === "text").map((child) => child.data ?? "").join("");
|
|
722
|
-
context.wxs.push({
|
|
723
|
-
module: moduleName,
|
|
724
|
-
kind: "inline",
|
|
725
|
-
importName: `__wxs_${context.wxs.length}`,
|
|
726
|
-
value: inlineCode
|
|
727
|
-
});
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
continue;
|
|
731
|
-
}
|
|
732
|
-
if (node.children?.length) {
|
|
733
|
-
node.children = collectSpecialNodes(node.children, context);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
renderable.push(node);
|
|
737
|
-
}
|
|
738
|
-
return renderable;
|
|
739
|
-
}
|
|
740
|
-
function toRelativeImport(from, target) {
|
|
741
|
-
const fromDir = dirname(from);
|
|
742
|
-
const rel = relative(fromDir, target);
|
|
743
|
-
if (!rel || rel.startsWith(".")) {
|
|
744
|
-
const fallback = normalizeTemplatePath(target).split("/").pop() ?? "";
|
|
745
|
-
return rel || `./${fallback}`;
|
|
746
|
-
}
|
|
747
|
-
return `./${rel}`;
|
|
748
|
-
}
|
|
749
|
-
function normalizeTemplatePath(pathname) {
|
|
750
|
-
return pathname.split("\\").join("/");
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
// src/compiler/wxml/compile.ts
|
|
754
|
-
function compileWxml(options) {
|
|
755
|
-
const dependencyContext = options.dependencyContext ?? createDependencyContext();
|
|
756
|
-
const expandDependencies = options.expandDependencies ?? !options.dependencyContext;
|
|
757
|
-
const warnings = dependencyContext.warnings;
|
|
758
|
-
const expandDependencyTree = (dependencies2, importer) => {
|
|
759
|
-
for (const target of dependencies2) {
|
|
760
|
-
if (!target) {
|
|
761
|
-
continue;
|
|
762
|
-
}
|
|
763
|
-
if (dependencyContext.active.has(target)) {
|
|
764
|
-
warnCircularTemplate(dependencyContext, importer, target);
|
|
765
|
-
continue;
|
|
766
|
-
}
|
|
767
|
-
if (dependencyContext.visited.has(target)) {
|
|
768
|
-
continue;
|
|
769
|
-
}
|
|
770
|
-
dependencyContext.visited.add(target);
|
|
771
|
-
dependencyContext.active.add(target);
|
|
772
|
-
let source;
|
|
773
|
-
try {
|
|
774
|
-
source = readFileSync(target, "utf8");
|
|
775
|
-
} catch {
|
|
776
|
-
warnReadTemplate(dependencyContext, target);
|
|
777
|
-
dependencyContext.active.delete(target);
|
|
778
|
-
continue;
|
|
779
|
-
}
|
|
780
|
-
try {
|
|
781
|
-
const result = compileWxml({
|
|
782
|
-
id: target,
|
|
783
|
-
source,
|
|
784
|
-
resolveTemplatePath: options.resolveTemplatePath,
|
|
785
|
-
resolveWxsPath: options.resolveWxsPath,
|
|
786
|
-
dependencyContext,
|
|
787
|
-
expandDependencies: false
|
|
788
|
-
});
|
|
789
|
-
expandDependencyTree(result.dependencies, target);
|
|
790
|
-
} catch (error) {
|
|
791
|
-
if (error instanceof Error && error.message) {
|
|
792
|
-
warnings.push(`[web] \u65E0\u6CD5\u89E3\u6790\u6A21\u677F\u4F9D\u8D56: ${target} ${error.message}`);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
dependencyContext.active.delete(target);
|
|
796
|
-
}
|
|
797
|
-
};
|
|
798
|
-
let nodes = parseWxml(options.source);
|
|
799
|
-
let navigationBarAttrs;
|
|
800
|
-
if (options.navigationBar) {
|
|
801
|
-
const extracted = extractNavigationBarFromPageMeta(nodes);
|
|
802
|
-
nodes = extracted.nodes;
|
|
803
|
-
if (extracted.warnings.length > 0) {
|
|
804
|
-
warnings.push(...extracted.warnings);
|
|
805
|
-
}
|
|
806
|
-
navigationBarAttrs = extracted.attrs;
|
|
807
|
-
}
|
|
808
|
-
const templates = [];
|
|
809
|
-
const includes = [];
|
|
810
|
-
const imports = [];
|
|
811
|
-
const wxs = [];
|
|
812
|
-
const wxsModules = /* @__PURE__ */ new Map();
|
|
813
|
-
const renderNodesList = collectSpecialNodes(nodes, {
|
|
814
|
-
templates,
|
|
815
|
-
includes,
|
|
816
|
-
imports,
|
|
817
|
-
wxs,
|
|
818
|
-
wxsModules,
|
|
819
|
-
warnings,
|
|
820
|
-
sourceId: options.id,
|
|
821
|
-
resolveTemplate: (raw) => options.resolveTemplatePath(raw, options.id),
|
|
822
|
-
resolveWxs: (raw) => options.resolveWxsPath(raw, options.id)
|
|
823
|
-
});
|
|
824
|
-
if (options.navigationBar && options.navigationBar.config.navigationStyle !== "custom") {
|
|
825
|
-
const attrs = buildNavigationBarAttrs(options.navigationBar.config, navigationBarAttrs);
|
|
826
|
-
renderNodesList.unshift({
|
|
827
|
-
type: "element",
|
|
828
|
-
name: "weapp-navigation-bar",
|
|
829
|
-
attribs: attrs
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
const importLines = [
|
|
833
|
-
`import { html } from 'lit'`,
|
|
834
|
-
`import { repeat } from 'lit/directives/repeat.js'`
|
|
835
|
-
];
|
|
836
|
-
const bodyLines = [];
|
|
837
|
-
const directDependencies = [];
|
|
838
|
-
for (const entry of imports) {
|
|
839
|
-
const importPath = normalizeTemplatePath(toRelativeImport(options.id, entry.id));
|
|
840
|
-
importLines.push(`import { templates as ${entry.importName} } from '${importPath}'`);
|
|
841
|
-
addDependency(entry.id, dependencyContext, directDependencies);
|
|
842
|
-
}
|
|
843
|
-
for (const entry of includes) {
|
|
844
|
-
const importPath = normalizeTemplatePath(toRelativeImport(options.id, entry.id));
|
|
845
|
-
importLines.push(`import { render as ${entry.importName} } from '${importPath}'`);
|
|
846
|
-
addDependency(entry.id, dependencyContext, directDependencies);
|
|
847
|
-
}
|
|
848
|
-
for (const entry of wxs) {
|
|
849
|
-
if (entry.kind === "src") {
|
|
850
|
-
const baseImport = normalizeTemplatePath(toRelativeImport(options.id, entry.value));
|
|
851
|
-
const importPath = shouldMarkWxsImport(entry.value) ? `${baseImport}?wxs` : baseImport;
|
|
852
|
-
importLines.push(`import ${entry.importName} from '${importPath}'`);
|
|
853
|
-
addDependency(entry.value, dependencyContext, directDependencies);
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
if (templates.length > 0 || imports.length > 0) {
|
|
857
|
-
const templatePairs = [];
|
|
858
|
-
for (const entry of imports) {
|
|
859
|
-
templatePairs.push(`...${entry.importName}`);
|
|
860
|
-
}
|
|
861
|
-
for (const template of templates) {
|
|
862
|
-
const rendered = renderer.renderNodes(template.nodes, "scope", "__wxs_modules", options.componentTags);
|
|
863
|
-
templatePairs.push(`${JSON.stringify(template.name)}: (scope, ctx) => ${rendered}`);
|
|
864
|
-
}
|
|
865
|
-
bodyLines.push(`const __templates = { ${templatePairs.join(", ")} }`);
|
|
866
|
-
} else {
|
|
867
|
-
bodyLines.push(`const __templates = {}`);
|
|
868
|
-
}
|
|
869
|
-
if (wxs.length > 0) {
|
|
870
|
-
bodyLines.push(`const __wxs_inline_cache = Object.create(null)`);
|
|
871
|
-
bodyLines.push(`let __wxs_modules = {}`);
|
|
872
|
-
const wxsMapEntries = [];
|
|
873
|
-
for (const entry of wxs) {
|
|
874
|
-
if (entry.kind === "inline") {
|
|
875
|
-
const inlineCode = entry.value.trim();
|
|
876
|
-
const cacheKey = JSON.stringify(entry.module);
|
|
877
|
-
if (inlineCode) {
|
|
878
|
-
bodyLines.push(`function ${entry.importName}(ctx) {`);
|
|
879
|
-
bodyLines.push(` if (!__wxs_inline_cache[${cacheKey}]) {`);
|
|
880
|
-
bodyLines.push(` __wxs_inline_cache[${cacheKey}] = ctx.createWxsModule(${JSON.stringify(inlineCode)}, ${JSON.stringify(options.id)})`);
|
|
881
|
-
bodyLines.push(` }`);
|
|
882
|
-
bodyLines.push(` return __wxs_inline_cache[${cacheKey}]`);
|
|
883
|
-
bodyLines.push(`}`);
|
|
884
|
-
} else {
|
|
885
|
-
bodyLines.push(`function ${entry.importName}() { return {} }`);
|
|
886
|
-
}
|
|
887
|
-
wxsMapEntries.push(`${JSON.stringify(entry.module)}: ${entry.importName}(ctx)`);
|
|
888
|
-
continue;
|
|
889
|
-
}
|
|
890
|
-
wxsMapEntries.push(`${JSON.stringify(entry.module)}: ${entry.importName}`);
|
|
891
|
-
}
|
|
892
|
-
bodyLines.push(`function __resolveWxsModules(ctx) {`);
|
|
893
|
-
bodyLines.push(` return { ${wxsMapEntries.join(", ")} }`);
|
|
894
|
-
bodyLines.push(`}`);
|
|
895
|
-
} else {
|
|
896
|
-
bodyLines.push(`const __wxs_modules = {}`);
|
|
897
|
-
}
|
|
898
|
-
const includesRender = includes.map((entry) => `${entry.importName}(scope, ctx)`);
|
|
899
|
-
const renderContent = renderer.renderNodes(renderNodesList, "scope", "__wxs_modules", options.componentTags);
|
|
900
|
-
const contentExpr = includesRender.length > 0 ? `[${[...includesRender, renderContent].join(", ")}]` : renderContent;
|
|
901
|
-
bodyLines.push(`export function render(scope, ctx) {`);
|
|
902
|
-
if (wxs.length > 0) {
|
|
903
|
-
bodyLines.push(` __wxs_modules = __resolveWxsModules(ctx)`);
|
|
904
|
-
}
|
|
905
|
-
bodyLines.push(` return ${contentExpr}`);
|
|
906
|
-
bodyLines.push(`}`);
|
|
907
|
-
bodyLines.push(`export const templates = __templates`);
|
|
908
|
-
bodyLines.push(`export default render`);
|
|
909
|
-
if (expandDependencies) {
|
|
910
|
-
dependencyContext.visited.add(options.id);
|
|
911
|
-
dependencyContext.active.add(options.id);
|
|
912
|
-
expandDependencyTree(directDependencies, options.id);
|
|
913
|
-
dependencyContext.active.delete(options.id);
|
|
914
|
-
}
|
|
915
|
-
const code = [...importLines, "", ...bodyLines].join("\n");
|
|
916
|
-
const dependencies = expandDependencies ? dependencyContext.dependencies : directDependencies;
|
|
917
|
-
return {
|
|
918
|
-
code,
|
|
919
|
-
dependencies,
|
|
920
|
-
warnings: warnings.length > 0 ? warnings : void 0
|
|
921
|
-
};
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
// src/compiler/wxs.ts
|
|
925
|
-
var REQUIRE_RE = /require\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
926
|
-
function normalizePath(p) {
|
|
927
|
-
return p.split("\\\\").join("/");
|
|
928
|
-
}
|
|
929
|
-
function isPlainWxsScript(pathname) {
|
|
930
|
-
const lower = pathname.toLowerCase();
|
|
931
|
-
if (lower.endsWith(".wxs") || lower.endsWith(".wxs.ts") || lower.endsWith(".wxs.js")) {
|
|
932
|
-
return false;
|
|
933
|
-
}
|
|
934
|
-
return lower.endsWith(".ts") || lower.endsWith(".js");
|
|
935
|
-
}
|
|
936
|
-
function appendWxsQuery(pathname) {
|
|
937
|
-
if (pathname.includes("?wxs") || pathname.includes("&wxs")) {
|
|
938
|
-
return pathname;
|
|
939
|
-
}
|
|
940
|
-
return `${pathname}${pathname.includes("?") ? "&" : "?"}wxs`;
|
|
941
|
-
}
|
|
942
|
-
function isSupportedRequirePath(request) {
|
|
943
|
-
const normalized = request.replace(/\\/g, "/");
|
|
944
|
-
return normalized.startsWith(".") || normalized.startsWith("/");
|
|
945
|
-
}
|
|
946
|
-
function transformWxsToEsm(code, id, options) {
|
|
947
|
-
const dependencies = [];
|
|
948
|
-
const importLines = [];
|
|
949
|
-
const mapEntries = [];
|
|
950
|
-
const warnings = [];
|
|
951
|
-
const seen = /* @__PURE__ */ new Set();
|
|
952
|
-
while (true) {
|
|
953
|
-
const match = REQUIRE_RE.exec(code);
|
|
954
|
-
if (!match) {
|
|
955
|
-
break;
|
|
956
|
-
}
|
|
957
|
-
const request = match[1];
|
|
958
|
-
if (!request || seen.has(request)) {
|
|
959
|
-
continue;
|
|
960
|
-
}
|
|
961
|
-
seen.add(request);
|
|
962
|
-
if (!isSupportedRequirePath(request)) {
|
|
963
|
-
warnings.push(`[@weapp-vite/web] WXS require \u4EC5\u652F\u6301\u76F8\u5BF9\u6216\u7EDD\u5BF9\u8DEF\u5F84: ${request} (from ${id})`);
|
|
964
|
-
continue;
|
|
965
|
-
}
|
|
966
|
-
const normalizedRequest = request.replace(/\\/g, "/");
|
|
967
|
-
const resolved = options.resolvePath(normalizedRequest, id);
|
|
968
|
-
if (!resolved) {
|
|
969
|
-
warnings.push(`[@weapp-vite/web] \u65E0\u6CD5\u89E3\u6790 WXS require: ${request} (from ${id})`);
|
|
970
|
-
continue;
|
|
971
|
-
}
|
|
972
|
-
let importPath = normalizePath(options.toImportPath?.(resolved, id) ?? resolved);
|
|
973
|
-
if (isPlainWxsScript(resolved)) {
|
|
974
|
-
importPath = appendWxsQuery(importPath);
|
|
975
|
-
}
|
|
976
|
-
const importName = `__wxs_dep_${dependencies.length}`;
|
|
977
|
-
importLines.push(`import ${importName} from '${importPath}'`);
|
|
978
|
-
mapEntries.push(`[${JSON.stringify(request)}, ${importName}]`);
|
|
979
|
-
dependencies.push(resolved);
|
|
980
|
-
}
|
|
981
|
-
const requireMap = `const __wxs_require_map = new Map([${mapEntries.join(", ")}])`;
|
|
982
|
-
const requireFn = [
|
|
983
|
-
`const __wxs_require_warned = new Set()`,
|
|
984
|
-
`function require(id) {`,
|
|
985
|
-
` if (__wxs_require_map.has(id)) {`,
|
|
986
|
-
` return __wxs_require_map.get(id)`,
|
|
987
|
-
` }`,
|
|
988
|
-
` if (!__wxs_require_warned.has(id)) {`,
|
|
989
|
-
` __wxs_require_warned.add(id)`,
|
|
990
|
-
` if (typeof console !== 'undefined' && typeof console.warn === 'function') {`,
|
|
991
|
-
` console.warn(\`[@weapp-vite/web] WXS require \u672A\u89E3\u6790: \${id}\`)`,
|
|
992
|
-
` }`,
|
|
993
|
-
` }`,
|
|
994
|
-
` return undefined`,
|
|
995
|
-
`}`
|
|
996
|
-
].join("\\n");
|
|
997
|
-
const moduleInit = `const module = { exports: {} }\\nconst exports = module.exports`;
|
|
998
|
-
const helpers = `const getRegExp = (pattern, flags) => new RegExp(pattern, flags)\\nconst getDate = (value) => (value == null ? new Date() : new Date(value))`;
|
|
999
|
-
const body = [
|
|
1000
|
-
...importLines,
|
|
1001
|
-
requireMap,
|
|
1002
|
-
requireFn,
|
|
1003
|
-
moduleInit,
|
|
1004
|
-
helpers,
|
|
1005
|
-
code,
|
|
1006
|
-
`export default module.exports`
|
|
1007
|
-
].join("\\n");
|
|
1008
|
-
return {
|
|
1009
|
-
code: body,
|
|
1010
|
-
dependencies,
|
|
1011
|
-
warnings: warnings.length > 0 ? warnings : void 0
|
|
1012
|
-
};
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
// src/css/wxss.ts
|
|
1016
|
-
var RPX_RE = /(-?(?:\d+(?:\.\d+)?|\.\d+))rpx/gi;
|
|
1017
|
-
function transformWxssToCss(source, options) {
|
|
1018
|
-
const rpxVar = options?.rpxVar ?? "--rpx";
|
|
1019
|
-
const useVariable = typeof options?.designWidth === "number" && Number.isFinite(options.designWidth);
|
|
1020
|
-
const ratio = options?.pxPerRpx ?? 0.5;
|
|
1021
|
-
const css = source.replace(RPX_RE, (_, value) => {
|
|
1022
|
-
const numeric = Number.parseFloat(value);
|
|
1023
|
-
if (Number.isNaN(numeric)) {
|
|
1024
|
-
return `${value}px`;
|
|
1025
|
-
}
|
|
1026
|
-
if (useVariable) {
|
|
1027
|
-
return `calc(var(${rpxVar}) * ${numeric})`;
|
|
1028
|
-
}
|
|
1029
|
-
const converted = Math.round(numeric * ratio * 1e3) / 1e3;
|
|
1030
|
-
return `${converted}px`;
|
|
1031
|
-
});
|
|
1032
|
-
return {
|
|
1033
|
-
css
|
|
1034
|
-
};
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
// src/plugin/constants.ts
|
|
1038
|
-
var SCRIPT_EXTS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
|
|
1039
|
-
var STYLE_EXTS = [".wxss", ".scss", ".less", ".css"];
|
|
1040
|
-
var TRANSFORM_STYLE_EXTS = [".wxss"];
|
|
1041
|
-
var TEMPLATE_EXTS = [".wxml", ".axml", ".swan", ".ttml", ".qml", ".ksml", ".xhsml", ".html"];
|
|
1042
|
-
var WXS_EXTS = [".wxs", ".wxs.ts", ".wxs.js"];
|
|
1043
|
-
var WXS_RESOLVE_EXTS = [".wxs", ".wxs.ts", ".wxs.js", ".ts", ".js"];
|
|
1044
|
-
var ENTRY_ID = "\0@weapp-vite/web/entry";
|
|
1045
|
-
|
|
1046
|
-
// src/plugin/path.ts
|
|
1047
|
-
import fs from "fs-extra";
|
|
1048
|
-
import { dirname as dirname2, extname, normalize, posix, relative as relative2, resolve } from "pathe";
|
|
1049
|
-
function cleanUrl(url) {
|
|
1050
|
-
const queryIndex = url.indexOf("?");
|
|
1051
|
-
if (queryIndex >= 0) {
|
|
1052
|
-
return url.slice(0, queryIndex);
|
|
1053
|
-
}
|
|
1054
|
-
return url;
|
|
1055
|
-
}
|
|
1056
|
-
function normalizePath2(p) {
|
|
1057
|
-
return posix.normalize(p.split("\\").join("/"));
|
|
1058
|
-
}
|
|
1059
|
-
function isInsideDir(filePath, dir) {
|
|
1060
|
-
const rel = relative2(dir, filePath);
|
|
1061
|
-
return rel === "" || !rel.startsWith("..") && !posix.isAbsolute(rel);
|
|
1062
|
-
}
|
|
1063
|
-
function isHtmlEntry(filePath, root) {
|
|
1064
|
-
if (!filePath.toLowerCase().endsWith(".html")) {
|
|
1065
|
-
return false;
|
|
1066
|
-
}
|
|
1067
|
-
return normalizePath2(filePath) === normalizePath2(resolve(root, "index.html"));
|
|
1068
|
-
}
|
|
1069
|
-
function toPosixId(id) {
|
|
1070
|
-
return normalize(id).split("\\").join("/");
|
|
1071
|
-
}
|
|
1072
|
-
function toRelativeImport2(from, target) {
|
|
1073
|
-
const fromDir = dirname2(from);
|
|
1074
|
-
const rel = relative2(fromDir, target);
|
|
1075
|
-
if (!rel || rel.startsWith(".")) {
|
|
1076
|
-
return normalizePath2(rel || `./${posix.basename(target)}`);
|
|
1077
|
-
}
|
|
1078
|
-
return `./${normalizePath2(rel)}`;
|
|
1079
|
-
}
|
|
1080
|
-
function appendInlineQuery(id) {
|
|
1081
|
-
if (id.includes("?")) {
|
|
1082
|
-
if (id.includes("?inline") || id.includes("&inline")) {
|
|
1083
|
-
return id;
|
|
1084
|
-
}
|
|
1085
|
-
return `${id}&inline`;
|
|
1086
|
-
}
|
|
1087
|
-
return `${id}?inline`;
|
|
1088
|
-
}
|
|
1089
|
-
function relativeModuleId(root, absPath) {
|
|
1090
|
-
const rel = relative2(root, absPath);
|
|
1091
|
-
return `/${normalizePath2(rel)}`;
|
|
1092
|
-
}
|
|
1093
|
-
function resolveImportBase(raw, importer, srcRoot) {
|
|
1094
|
-
if (!raw) {
|
|
1095
|
-
return void 0;
|
|
1096
|
-
}
|
|
1097
|
-
if (raw.startsWith(".")) {
|
|
1098
|
-
return resolve(dirname2(importer), raw);
|
|
1099
|
-
}
|
|
1100
|
-
if (raw.startsWith("/")) {
|
|
1101
|
-
return resolve(srcRoot, raw.slice(1));
|
|
1102
|
-
}
|
|
1103
|
-
return resolve(srcRoot, raw);
|
|
1104
|
-
}
|
|
1105
|
-
function resolveFileWithExtensionsSync(basePath, extensions) {
|
|
1106
|
-
if (extname(basePath) && fs.pathExistsSync(basePath)) {
|
|
1107
|
-
return basePath;
|
|
1108
|
-
}
|
|
1109
|
-
for (const ext of extensions) {
|
|
1110
|
-
const candidate = `${basePath}${ext}`;
|
|
1111
|
-
if (fs.pathExistsSync(candidate)) {
|
|
1112
|
-
return candidate;
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
return void 0;
|
|
1116
|
-
}
|
|
1117
|
-
function resolveTemplatePathSync(raw, importer, srcRoot) {
|
|
1118
|
-
const base = resolveImportBase(raw, importer, srcRoot);
|
|
1119
|
-
if (!base) {
|
|
1120
|
-
return void 0;
|
|
1121
|
-
}
|
|
1122
|
-
return resolveFileWithExtensionsSync(base, TEMPLATE_EXTS);
|
|
1123
|
-
}
|
|
1124
|
-
function resolveWxsPathSync(raw, importer, srcRoot) {
|
|
1125
|
-
if (!raw) {
|
|
1126
|
-
return void 0;
|
|
1127
|
-
}
|
|
1128
|
-
let base;
|
|
1129
|
-
if (raw.startsWith(".")) {
|
|
1130
|
-
base = resolve(dirname2(importer), raw);
|
|
1131
|
-
} else if (raw.startsWith("/")) {
|
|
1132
|
-
base = resolve(srcRoot, raw.slice(1));
|
|
1133
|
-
} else {
|
|
1134
|
-
return void 0;
|
|
1135
|
-
}
|
|
1136
|
-
return resolveFileWithExtensionsSync(base, WXS_RESOLVE_EXTS);
|
|
1137
|
-
}
|
|
1138
|
-
|
|
1139
|
-
// src/plugin/entry.ts
|
|
1140
|
-
function generateEntryModule(result, root, wxssOptions, pluginOptions) {
|
|
1141
|
-
const importLines = [`import { initializePageRoutes } from '@weapp-vite/web/runtime/polyfill'`];
|
|
1142
|
-
const bodyLines = [];
|
|
1143
|
-
for (const page of result.pages) {
|
|
1144
|
-
importLines.push(`import '${relativeModuleId(root, page.script)}'`);
|
|
1145
|
-
}
|
|
1146
|
-
for (const component of result.components) {
|
|
1147
|
-
importLines.push(`import '${relativeModuleId(root, component.script)}'`);
|
|
1148
|
-
}
|
|
1149
|
-
if (result.app) {
|
|
1150
|
-
importLines.push(`import '${relativeModuleId(root, result.app)}'`);
|
|
1151
|
-
}
|
|
1152
|
-
const pageOrder = result.pages.map((page) => page.id);
|
|
1153
|
-
const rpxConfig = wxssOptions?.designWidth ? { designWidth: wxssOptions.designWidth, varName: wxssOptions.rpxVar } : void 0;
|
|
1154
|
-
const initOptions = {};
|
|
1155
|
-
if (rpxConfig) {
|
|
1156
|
-
initOptions.rpx = rpxConfig;
|
|
1157
|
-
}
|
|
1158
|
-
if (pluginOptions?.form?.preventDefault !== void 0) {
|
|
1159
|
-
initOptions.form = { preventDefault: pluginOptions.form.preventDefault };
|
|
1160
|
-
}
|
|
1161
|
-
const runtimeOptions = {};
|
|
1162
|
-
if (pluginOptions?.runtime?.executionMode) {
|
|
1163
|
-
runtimeOptions.executionMode = pluginOptions.runtime.executionMode;
|
|
1164
|
-
}
|
|
1165
|
-
if (pluginOptions?.runtime?.warnings) {
|
|
1166
|
-
runtimeOptions.warnings = pluginOptions.runtime.warnings;
|
|
1167
|
-
}
|
|
1168
|
-
if (Object.keys(runtimeOptions).length > 0) {
|
|
1169
|
-
initOptions.runtime = runtimeOptions;
|
|
1170
|
-
}
|
|
1171
|
-
const initOptionsCode = Object.keys(initOptions).length > 0 ? `, ${JSON.stringify(initOptions)}` : "";
|
|
1172
|
-
bodyLines.push(`initializePageRoutes(${JSON.stringify(pageOrder)}${initOptionsCode})`);
|
|
1173
|
-
return [...importLines, ...bodyLines].join("\n");
|
|
1174
|
-
}
|
|
1175
|
-
|
|
1176
|
-
// src/plugin/register.ts
|
|
1177
|
-
import { parse } from "@babel/parser";
|
|
1178
|
-
import _babelTraverse from "@babel/traverse";
|
|
1179
|
-
import * as t from "@babel/types";
|
|
1180
|
-
import MagicString from "magic-string";
|
|
1181
|
-
var traverseCandidate = (() => {
|
|
1182
|
-
const mod = _babelTraverse;
|
|
1183
|
-
if (typeof mod === "function") {
|
|
1184
|
-
return mod;
|
|
1185
|
-
}
|
|
1186
|
-
if (mod?.default && typeof mod.default === "function") {
|
|
1187
|
-
return mod.default;
|
|
1188
|
-
}
|
|
1189
|
-
if (mod?.traverse && typeof mod.traverse === "function") {
|
|
1190
|
-
return mod.traverse;
|
|
1191
|
-
}
|
|
1192
|
-
return void 0;
|
|
1193
|
-
})();
|
|
1194
|
-
if (typeof traverseCandidate !== "function") {
|
|
1195
|
-
throw new TypeError("[@weapp-vite/web] Failed to resolve @babel/traverse export.");
|
|
1196
|
-
}
|
|
1197
|
-
var traverse = traverseCandidate;
|
|
1198
|
-
function mapRegisterIdentifier(kind) {
|
|
1199
|
-
if (kind === "page") {
|
|
1200
|
-
return "Page";
|
|
1201
|
-
}
|
|
1202
|
-
if (kind === "component") {
|
|
1203
|
-
return "Component";
|
|
1204
|
-
}
|
|
1205
|
-
if (kind === "app") {
|
|
1206
|
-
return "App";
|
|
1207
|
-
}
|
|
1208
|
-
return "";
|
|
1209
|
-
}
|
|
1210
|
-
function getRegisterName(kind) {
|
|
1211
|
-
if (kind === "page") {
|
|
1212
|
-
return "registerPage";
|
|
1213
|
-
}
|
|
1214
|
-
if (kind === "component") {
|
|
1215
|
-
return "registerComponent";
|
|
1216
|
-
}
|
|
1217
|
-
if (kind === "app") {
|
|
1218
|
-
return "registerApp";
|
|
1219
|
-
}
|
|
1220
|
-
return void 0;
|
|
1221
|
-
}
|
|
1222
|
-
function overwriteCall(path, meta, registerName, templateIdent, styleIdent, s) {
|
|
1223
|
-
const node = path.node;
|
|
1224
|
-
const callee = node.callee;
|
|
1225
|
-
if (!t.isIdentifier(callee)) {
|
|
1226
|
-
return;
|
|
1227
|
-
}
|
|
1228
|
-
const end = node.end;
|
|
1229
|
-
const insertPosition = end - 1;
|
|
1230
|
-
const metaParts = [`id: ${JSON.stringify(meta.id)}`];
|
|
1231
|
-
if (templateIdent) {
|
|
1232
|
-
metaParts.push(`template: ${templateIdent}`);
|
|
1233
|
-
}
|
|
1234
|
-
if (styleIdent) {
|
|
1235
|
-
metaParts.push(`style: ${styleIdent}`);
|
|
1236
|
-
}
|
|
1237
|
-
const metaCode = `{ ${metaParts.join(", ")} }`;
|
|
1238
|
-
s.overwrite(callee.start, callee.end, registerName);
|
|
1239
|
-
s.appendLeft(insertPosition, `, ${metaCode}`);
|
|
1240
|
-
}
|
|
1241
|
-
function transformScriptModule({
|
|
1242
|
-
code,
|
|
1243
|
-
cleanId,
|
|
1244
|
-
meta,
|
|
1245
|
-
enableHmr
|
|
1246
|
-
}) {
|
|
1247
|
-
const registerName = getRegisterName(meta.kind);
|
|
1248
|
-
if (!registerName) {
|
|
1249
|
-
return null;
|
|
1250
|
-
}
|
|
1251
|
-
let ast;
|
|
1252
|
-
try {
|
|
1253
|
-
ast = parse(code, {
|
|
1254
|
-
sourceType: "module",
|
|
1255
|
-
plugins: ["typescript", "jsx"],
|
|
1256
|
-
errorRecovery: true,
|
|
1257
|
-
ranges: true
|
|
1258
|
-
});
|
|
1259
|
-
} catch {
|
|
1260
|
-
return null;
|
|
1261
|
-
}
|
|
1262
|
-
const s = new MagicString(code);
|
|
1263
|
-
let transformed = false;
|
|
1264
|
-
const imports = [];
|
|
1265
|
-
const templateIdent = meta.templatePath ? "__weapp_template__" : void 0;
|
|
1266
|
-
const styleIdent = meta.stylePath ? "__weapp_style__" : void 0;
|
|
1267
|
-
if (meta.templatePath && templateIdent) {
|
|
1268
|
-
imports.push(`import ${templateIdent} from '${toRelativeImport2(cleanId, meta.templatePath)}'`);
|
|
1269
|
-
}
|
|
1270
|
-
if (meta.stylePath && styleIdent) {
|
|
1271
|
-
imports.push(`import ${styleIdent} from '${appendInlineQuery(toRelativeImport2(cleanId, meta.stylePath))}'`);
|
|
1272
|
-
}
|
|
1273
|
-
const registerImports = /* @__PURE__ */ new Set();
|
|
1274
|
-
traverse(ast, {
|
|
1275
|
-
CallExpression(path) {
|
|
1276
|
-
if (!t.isIdentifier(path.node.callee)) {
|
|
1277
|
-
return;
|
|
1278
|
-
}
|
|
1279
|
-
const name = path.node.callee.name;
|
|
1280
|
-
if (name === mapRegisterIdentifier(meta.kind)) {
|
|
1281
|
-
registerImports.add(registerName);
|
|
1282
|
-
overwriteCall(path, meta, registerName, templateIdent, styleIdent, s);
|
|
1283
|
-
transformed = true;
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
});
|
|
1287
|
-
if (!transformed) {
|
|
1288
|
-
return null;
|
|
1289
|
-
}
|
|
1290
|
-
if (registerImports.size > 0) {
|
|
1291
|
-
imports.unshift(`import { ${Array.from(registerImports).join(", ")} } from '@weapp-vite/web/runtime/polyfill'`);
|
|
1292
|
-
}
|
|
1293
|
-
const prefix = `${imports.join("\n")}
|
|
1294
|
-
`;
|
|
1295
|
-
s.prepend(prefix);
|
|
1296
|
-
if (enableHmr) {
|
|
1297
|
-
s.append(`
|
|
1298
|
-
if (import.meta.hot) { import.meta.hot.accept() }
|
|
1299
|
-
`);
|
|
1300
|
-
}
|
|
1301
|
-
return {
|
|
1302
|
-
code: s.toString(),
|
|
1303
|
-
map: s.generateMap({
|
|
1304
|
-
hires: true
|
|
1305
|
-
})
|
|
1306
|
-
};
|
|
1307
|
-
}
|
|
1308
|
-
|
|
1309
|
-
// src/plugin/scan.ts
|
|
1310
|
-
import process from "process";
|
|
1311
|
-
import { dirname as dirname3, extname as extname3, join, posix as posix2, relative as relative3, resolve as resolve2 } from "pathe";
|
|
1312
|
-
|
|
1313
|
-
// src/shared/slugify.ts
|
|
1314
|
-
function slugify(id, prefix) {
|
|
1315
|
-
const normalized = id.replace(/[^a-z0-9]+/gi, "-").replace(/^-+|-+$/g, "").toLowerCase();
|
|
1316
|
-
return `${prefix}-${normalized || "index"}`;
|
|
1317
|
-
}
|
|
1318
|
-
|
|
1319
|
-
// src/plugin/files.ts
|
|
1320
|
-
import fs2 from "fs-extra";
|
|
1321
|
-
import { extname as extname2 } from "pathe";
|
|
1322
|
-
import { bundleRequire } from "rolldown-require";
|
|
1323
|
-
function isRecord(value) {
|
|
1324
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1325
|
-
}
|
|
1326
|
-
async function readJsonFile(pathname) {
|
|
1327
|
-
const candidates = [pathname];
|
|
1328
|
-
if (pathname.endsWith(".json")) {
|
|
1329
|
-
candidates.push(`${pathname}.ts`, `${pathname}.js`);
|
|
1330
|
-
}
|
|
1331
|
-
for (const candidate of candidates) {
|
|
1332
|
-
if (!await fs2.pathExists(candidate)) {
|
|
1333
|
-
continue;
|
|
1334
|
-
}
|
|
1335
|
-
if (candidate.endsWith(".json")) {
|
|
1336
|
-
const json = await fs2.readJson(candidate).catch(() => void 0);
|
|
1337
|
-
if (!isRecord(json)) {
|
|
1338
|
-
return void 0;
|
|
1339
|
-
}
|
|
1340
|
-
return json;
|
|
1341
|
-
}
|
|
1342
|
-
const { mod } = await bundleRequire({
|
|
1343
|
-
filepath: candidate,
|
|
1344
|
-
preserveTemporaryFile: true
|
|
1345
|
-
});
|
|
1346
|
-
const resolved = typeof mod.default === "function" ? await mod.default() : mod.default;
|
|
1347
|
-
if (!isRecord(resolved)) {
|
|
1348
|
-
return void 0;
|
|
1349
|
-
}
|
|
1350
|
-
return resolved;
|
|
1351
|
-
}
|
|
1352
|
-
return void 0;
|
|
1353
|
-
}
|
|
1354
|
-
async function resolveJsonPath(basePath) {
|
|
1355
|
-
const candidates = [basePath];
|
|
1356
|
-
if (basePath.endsWith(".json")) {
|
|
1357
|
-
candidates.push(`${basePath}.ts`, `${basePath}.js`);
|
|
1358
|
-
}
|
|
1359
|
-
for (const candidate of candidates) {
|
|
1360
|
-
if (await fs2.pathExists(candidate)) {
|
|
1361
|
-
return candidate;
|
|
1362
|
-
}
|
|
1363
|
-
}
|
|
1364
|
-
return void 0;
|
|
1365
|
-
}
|
|
1366
|
-
async function resolveScriptFile(basePath) {
|
|
1367
|
-
const ext = extname2(basePath);
|
|
1368
|
-
if (ext && await fs2.pathExists(basePath)) {
|
|
1369
|
-
return basePath;
|
|
1370
|
-
}
|
|
1371
|
-
for (const candidateExt of SCRIPT_EXTS) {
|
|
1372
|
-
const candidate = `${basePath}${candidateExt}`;
|
|
1373
|
-
if (await fs2.pathExists(candidate)) {
|
|
1374
|
-
return candidate;
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
return void 0;
|
|
1378
|
-
}
|
|
1379
|
-
async function resolveStyleFile(scriptPath) {
|
|
1380
|
-
const base = scriptPath.replace(new RegExp(`${extname2(scriptPath)}$`), "");
|
|
1381
|
-
for (const ext of STYLE_EXTS) {
|
|
1382
|
-
const candidate = `${base}${ext}`;
|
|
1383
|
-
if (await fs2.pathExists(candidate)) {
|
|
1384
|
-
return candidate;
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
return void 0;
|
|
1388
|
-
}
|
|
1389
|
-
async function resolveTemplateFile(scriptPath) {
|
|
1390
|
-
const base = scriptPath.replace(new RegExp(`${extname2(scriptPath)}$`), "");
|
|
1391
|
-
for (const ext of TEMPLATE_EXTS) {
|
|
1392
|
-
const candidate = `${base}${ext}`;
|
|
1393
|
-
if (await fs2.pathExists(candidate)) {
|
|
1394
|
-
return candidate;
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
return void 0;
|
|
1398
|
-
}
|
|
1399
|
-
|
|
1400
|
-
// src/plugin/navigation.ts
|
|
1401
|
-
function pickNavigationConfig(source) {
|
|
1402
|
-
const config = {};
|
|
1403
|
-
if (!source) {
|
|
1404
|
-
return config;
|
|
1405
|
-
}
|
|
1406
|
-
if (typeof source.navigationBarTitleText === "string") {
|
|
1407
|
-
config.title = source.navigationBarTitleText;
|
|
1408
|
-
}
|
|
1409
|
-
if (typeof source.navigationBarBackgroundColor === "string") {
|
|
1410
|
-
config.backgroundColor = source.navigationBarBackgroundColor;
|
|
1411
|
-
}
|
|
1412
|
-
if (typeof source.navigationBarTextStyle === "string") {
|
|
1413
|
-
config.textStyle = source.navigationBarTextStyle;
|
|
1414
|
-
}
|
|
1415
|
-
if (typeof source.navigationStyle === "string") {
|
|
1416
|
-
config.navigationStyle = source.navigationStyle;
|
|
1417
|
-
}
|
|
1418
|
-
return config;
|
|
1419
|
-
}
|
|
1420
|
-
function mergeNavigationConfig(base, overrides) {
|
|
1421
|
-
return {
|
|
1422
|
-
...base,
|
|
1423
|
-
...overrides
|
|
1424
|
-
};
|
|
1425
|
-
}
|
|
1426
|
-
|
|
1427
|
-
// src/plugin/scanConfig.ts
|
|
1428
|
-
async function collectComponentTagsFromConfig({
|
|
1429
|
-
json,
|
|
1430
|
-
importerDir,
|
|
1431
|
-
jsonPath,
|
|
1432
|
-
warn,
|
|
1433
|
-
resolveComponentScript,
|
|
1434
|
-
getComponentTag,
|
|
1435
|
-
collectComponent,
|
|
1436
|
-
onResolved
|
|
1437
|
-
}) {
|
|
1438
|
-
const usingComponents = json.usingComponents;
|
|
1439
|
-
if (!usingComponents || typeof usingComponents !== "object") {
|
|
1440
|
-
return {};
|
|
1441
|
-
}
|
|
1442
|
-
const tags = {};
|
|
1443
|
-
const resolved = [];
|
|
1444
|
-
for (const [rawKey, rawValue] of Object.entries(usingComponents)) {
|
|
1445
|
-
if (typeof rawValue !== "string") {
|
|
1446
|
-
continue;
|
|
1447
|
-
}
|
|
1448
|
-
const key = normalizeComponentKey(rawKey);
|
|
1449
|
-
if (!key) {
|
|
1450
|
-
continue;
|
|
1451
|
-
}
|
|
1452
|
-
const script = await resolveComponentScript(rawValue, importerDir);
|
|
1453
|
-
if (!script) {
|
|
1454
|
-
warn(`[@weapp-vite/web] usingComponents entry "${rawKey}" not found: ${rawValue} (${jsonPath})`);
|
|
1455
|
-
continue;
|
|
1456
|
-
}
|
|
1457
|
-
const tag = getComponentTag(script);
|
|
1458
|
-
tags[key] = tag;
|
|
1459
|
-
resolved.push({ rawValue });
|
|
1460
|
-
}
|
|
1461
|
-
onResolved?.(tags);
|
|
1462
|
-
for (const entry of resolved) {
|
|
1463
|
-
await collectComponent(entry.rawValue, importerDir);
|
|
1464
|
-
}
|
|
1465
|
-
return tags;
|
|
1466
|
-
}
|
|
1467
|
-
async function collectComponentTagsFromJson({
|
|
1468
|
-
jsonBasePath,
|
|
1469
|
-
importerDir,
|
|
1470
|
-
warn,
|
|
1471
|
-
collectFromConfig
|
|
1472
|
-
}) {
|
|
1473
|
-
const resolvedPath = await resolveJsonPath(jsonBasePath);
|
|
1474
|
-
if (!resolvedPath) {
|
|
1475
|
-
return {};
|
|
1476
|
-
}
|
|
1477
|
-
const json = await readJsonFile(resolvedPath);
|
|
1478
|
-
if (!json) {
|
|
1479
|
-
return {};
|
|
1480
|
-
}
|
|
1481
|
-
return collectFromConfig(json, importerDir, resolvedPath, warn);
|
|
1482
|
-
}
|
|
1483
|
-
function mergeComponentTags(base, overrides) {
|
|
1484
|
-
if (!Object.keys(base).length && !Object.keys(overrides).length) {
|
|
1485
|
-
return {};
|
|
1486
|
-
}
|
|
1487
|
-
return {
|
|
1488
|
-
...base,
|
|
1489
|
-
...overrides
|
|
1490
|
-
};
|
|
1491
|
-
}
|
|
1492
|
-
function normalizeComponentKey(raw) {
|
|
1493
|
-
return raw.trim().toLowerCase();
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
// src/plugin/scan.ts
|
|
1497
|
-
async function scanProject({ srcRoot, warn, state }) {
|
|
1498
|
-
state.moduleMeta.clear();
|
|
1499
|
-
state.pageNavigationMap.clear();
|
|
1500
|
-
state.templateComponentMap.clear();
|
|
1501
|
-
state.templatePathSet.clear();
|
|
1502
|
-
state.componentTagMap.clear();
|
|
1503
|
-
state.componentIdMap.clear();
|
|
1504
|
-
let appNavigationDefaults = {};
|
|
1505
|
-
let appComponentTags = {};
|
|
1506
|
-
const pages = /* @__PURE__ */ new Map();
|
|
1507
|
-
const components = /* @__PURE__ */ new Map();
|
|
1508
|
-
const reportWarning = (message) => {
|
|
1509
|
-
if (warn) {
|
|
1510
|
-
warn(message);
|
|
1511
|
-
return;
|
|
1512
|
-
}
|
|
1513
|
-
if (typeof process !== "undefined" && typeof process.emitWarning === "function") {
|
|
1514
|
-
process.emitWarning(message);
|
|
1515
|
-
}
|
|
1516
|
-
};
|
|
1517
|
-
const appScript = await resolveScriptFile(join(srcRoot, "app"));
|
|
1518
|
-
if (appScript) {
|
|
1519
|
-
state.moduleMeta.set(normalizePath2(appScript), {
|
|
1520
|
-
kind: "app",
|
|
1521
|
-
id: "app",
|
|
1522
|
-
scriptPath: appScript,
|
|
1523
|
-
stylePath: await resolveStyleFile(appScript)
|
|
1524
|
-
});
|
|
1525
|
-
}
|
|
1526
|
-
const resolveComponentScript = async (raw, importerDir) => {
|
|
1527
|
-
const base = resolveComponentBase(raw, importerDir, srcRoot);
|
|
1528
|
-
if (!base) {
|
|
1529
|
-
return void 0;
|
|
1530
|
-
}
|
|
1531
|
-
return resolveScriptFile(base);
|
|
1532
|
-
};
|
|
1533
|
-
const getComponentId = (script) => {
|
|
1534
|
-
const cached = state.componentIdMap.get(script);
|
|
1535
|
-
if (cached) {
|
|
1536
|
-
return cached;
|
|
1537
|
-
}
|
|
1538
|
-
const idRelative = relative3(srcRoot, script).replace(new RegExp(`${extname3(script)}$`), "");
|
|
1539
|
-
const componentIdPosix = toPosixId(idRelative);
|
|
1540
|
-
state.componentIdMap.set(script, componentIdPosix);
|
|
1541
|
-
return componentIdPosix;
|
|
1542
|
-
};
|
|
1543
|
-
const getComponentTag = (script) => {
|
|
1544
|
-
const cached = state.componentTagMap.get(script);
|
|
1545
|
-
if (cached) {
|
|
1546
|
-
return cached;
|
|
1547
|
-
}
|
|
1548
|
-
const id = getComponentId(script);
|
|
1549
|
-
const tag = slugify(id, "wv-component");
|
|
1550
|
-
state.componentTagMap.set(script, tag);
|
|
1551
|
-
return tag;
|
|
1552
|
-
};
|
|
1553
|
-
const collectComponent = async (componentId, importerDir) => {
|
|
1554
|
-
const base = resolveComponentBase(componentId, importerDir, srcRoot);
|
|
1555
|
-
const script = base ? await resolveScriptFile(base) : void 0;
|
|
1556
|
-
if (!script || components.has(script)) {
|
|
1557
|
-
return;
|
|
1558
|
-
}
|
|
1559
|
-
const idRelative = relative3(srcRoot, script).replace(new RegExp(`${extname3(script)}$`), "");
|
|
1560
|
-
const componentIdPosix = toPosixId(idRelative);
|
|
1561
|
-
const template = await resolveTemplateFile(script);
|
|
1562
|
-
const style = await resolveStyleFile(script);
|
|
1563
|
-
if (template) {
|
|
1564
|
-
state.templatePathSet.add(normalizePath2(template));
|
|
1565
|
-
}
|
|
1566
|
-
state.moduleMeta.set(normalizePath2(script), {
|
|
1567
|
-
kind: "component",
|
|
1568
|
-
id: componentIdPosix,
|
|
1569
|
-
scriptPath: script,
|
|
1570
|
-
templatePath: template,
|
|
1571
|
-
stylePath: style
|
|
1572
|
-
});
|
|
1573
|
-
components.set(script, { script, id: componentIdPosix });
|
|
1574
|
-
const componentJsonBasePath = `${script.replace(new RegExp(`${extname3(script)}$`), "")}.json`;
|
|
1575
|
-
const componentTags = await collectComponentTagsFromJson({
|
|
1576
|
-
jsonBasePath: componentJsonBasePath,
|
|
1577
|
-
importerDir: dirname3(script),
|
|
1578
|
-
warn: reportWarning,
|
|
1579
|
-
collectFromConfig: (json, nextImporterDir, jsonPath, nextWarn) => collectComponentTagsFromConfig({
|
|
1580
|
-
json,
|
|
1581
|
-
importerDir: nextImporterDir,
|
|
1582
|
-
jsonPath,
|
|
1583
|
-
warn: nextWarn,
|
|
1584
|
-
resolveComponentScript,
|
|
1585
|
-
getComponentTag,
|
|
1586
|
-
collectComponent
|
|
1587
|
-
})
|
|
1588
|
-
});
|
|
1589
|
-
if (!template) {
|
|
1590
|
-
return;
|
|
1591
|
-
}
|
|
1592
|
-
const mergedTags = mergeComponentTags(appComponentTags, componentTags);
|
|
1593
|
-
if (Object.keys(mergedTags).length > 0) {
|
|
1594
|
-
state.templateComponentMap.set(normalizePath2(template), mergedTags);
|
|
1595
|
-
return;
|
|
1596
|
-
}
|
|
1597
|
-
state.templateComponentMap.delete(normalizePath2(template));
|
|
1598
|
-
};
|
|
1599
|
-
const appJsonBasePath = join(srcRoot, "app.json");
|
|
1600
|
-
const appJsonPath = await resolveJsonPath(appJsonBasePath);
|
|
1601
|
-
if (appJsonPath) {
|
|
1602
|
-
const appJson = await readJsonFile(appJsonPath);
|
|
1603
|
-
if (appJson) {
|
|
1604
|
-
appComponentTags = await collectComponentTagsFromConfig({
|
|
1605
|
-
json: appJson,
|
|
1606
|
-
importerDir: srcRoot,
|
|
1607
|
-
jsonPath: appJsonPath,
|
|
1608
|
-
warn: reportWarning,
|
|
1609
|
-
resolveComponentScript,
|
|
1610
|
-
getComponentTag,
|
|
1611
|
-
collectComponent,
|
|
1612
|
-
onResolved: (tags) => {
|
|
1613
|
-
appComponentTags = tags;
|
|
1614
|
-
}
|
|
1615
|
-
});
|
|
1616
|
-
}
|
|
1617
|
-
if (appJson?.pages && Array.isArray(appJson.pages)) {
|
|
1618
|
-
for (const page of appJson.pages) {
|
|
1619
|
-
if (typeof page === "string") {
|
|
1620
|
-
await collectPage(page);
|
|
1621
|
-
}
|
|
1622
|
-
}
|
|
1623
|
-
}
|
|
1624
|
-
if (appJson?.subPackages && Array.isArray(appJson.subPackages)) {
|
|
1625
|
-
for (const pkg of appJson.subPackages) {
|
|
1626
|
-
if (!pkg || typeof pkg !== "object" || !Array.isArray(pkg.pages)) {
|
|
1627
|
-
continue;
|
|
1628
|
-
}
|
|
1629
|
-
const root = typeof pkg.root === "string" ? pkg.root : "";
|
|
1630
|
-
for (const page of pkg.pages) {
|
|
1631
|
-
if (typeof page !== "string") {
|
|
1632
|
-
continue;
|
|
1633
|
-
}
|
|
1634
|
-
await collectPage(posix2.join(root, page));
|
|
1635
|
-
}
|
|
1636
|
-
}
|
|
1637
|
-
}
|
|
1638
|
-
const windowConfig = isRecord(appJson?.window) ? appJson.window : void 0;
|
|
1639
|
-
appNavigationDefaults = pickNavigationConfig(windowConfig);
|
|
1640
|
-
}
|
|
1641
|
-
state.appNavigationDefaults = appNavigationDefaults;
|
|
1642
|
-
state.appComponentTags = appComponentTags;
|
|
1643
|
-
state.scanResult = {
|
|
1644
|
-
app: appScript,
|
|
1645
|
-
pages: Array.from(pages.values()),
|
|
1646
|
-
components: Array.from(components.values())
|
|
1647
|
-
};
|
|
1648
|
-
async function collectPage(pageId) {
|
|
1649
|
-
const base = join(srcRoot, pageId);
|
|
1650
|
-
const script = await resolveScriptFile(base);
|
|
1651
|
-
if (!script) {
|
|
1652
|
-
return;
|
|
1653
|
-
}
|
|
1654
|
-
const template = await resolveTemplateFile(base);
|
|
1655
|
-
if (template) {
|
|
1656
|
-
state.templatePathSet.add(normalizePath2(template));
|
|
1657
|
-
}
|
|
1658
|
-
const style = await resolveStyleFile(base);
|
|
1659
|
-
const pageJsonBasePath = join(srcRoot, `${pageId}.json`);
|
|
1660
|
-
const pageJsonPath = await resolveJsonPath(pageJsonBasePath);
|
|
1661
|
-
const pageJson = pageJsonPath ? await readJsonFile(pageJsonPath) : void 0;
|
|
1662
|
-
state.moduleMeta.set(normalizePath2(script), {
|
|
1663
|
-
kind: "page",
|
|
1664
|
-
id: toPosixId(pageId),
|
|
1665
|
-
scriptPath: script,
|
|
1666
|
-
templatePath: template,
|
|
1667
|
-
stylePath: style
|
|
1668
|
-
});
|
|
1669
|
-
pages.set(script, {
|
|
1670
|
-
script,
|
|
1671
|
-
id: toPosixId(pageId)
|
|
1672
|
-
});
|
|
1673
|
-
const pageComponentTags = pageJson && pageJsonPath ? await collectComponentTagsFromConfig({
|
|
1674
|
-
json: pageJson,
|
|
1675
|
-
importerDir: dirname3(script),
|
|
1676
|
-
jsonPath: pageJsonPath,
|
|
1677
|
-
warn: reportWarning,
|
|
1678
|
-
resolveComponentScript,
|
|
1679
|
-
getComponentTag,
|
|
1680
|
-
collectComponent
|
|
1681
|
-
}) : await collectComponentTagsFromJson({
|
|
1682
|
-
jsonBasePath: pageJsonBasePath,
|
|
1683
|
-
importerDir: dirname3(script),
|
|
1684
|
-
warn: reportWarning,
|
|
1685
|
-
collectFromConfig: (json, importerDir, jsonPath, nextWarn) => collectComponentTagsFromConfig({
|
|
1686
|
-
json,
|
|
1687
|
-
importerDir,
|
|
1688
|
-
jsonPath,
|
|
1689
|
-
warn: nextWarn,
|
|
1690
|
-
resolveComponentScript,
|
|
1691
|
-
getComponentTag,
|
|
1692
|
-
collectComponent
|
|
1693
|
-
})
|
|
1694
|
-
});
|
|
1695
|
-
if (template) {
|
|
1696
|
-
const mergedTags = mergeComponentTags(appComponentTags, pageComponentTags);
|
|
1697
|
-
if (Object.keys(mergedTags).length > 0) {
|
|
1698
|
-
state.templateComponentMap.set(normalizePath2(template), mergedTags);
|
|
1699
|
-
} else {
|
|
1700
|
-
state.templateComponentMap.delete(normalizePath2(template));
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
if (!template) {
|
|
1704
|
-
return;
|
|
1705
|
-
}
|
|
1706
|
-
if (pageJson) {
|
|
1707
|
-
state.pageNavigationMap.set(
|
|
1708
|
-
normalizePath2(template),
|
|
1709
|
-
mergeNavigationConfig(appNavigationDefaults, pickNavigationConfig(pageJson))
|
|
1710
|
-
);
|
|
1711
|
-
return;
|
|
1712
|
-
}
|
|
1713
|
-
state.pageNavigationMap.set(normalizePath2(template), { ...appNavigationDefaults });
|
|
1714
|
-
}
|
|
1715
|
-
}
|
|
1716
|
-
function resolveComponentBase(raw, importerDir, srcRoot) {
|
|
1717
|
-
if (!raw) {
|
|
1718
|
-
return void 0;
|
|
1719
|
-
}
|
|
1720
|
-
if (raw.startsWith(".")) {
|
|
1721
|
-
return resolve2(importerDir, raw);
|
|
1722
|
-
}
|
|
1723
|
-
if (raw.startsWith("/")) {
|
|
1724
|
-
return resolve2(srcRoot, raw.slice(1));
|
|
1725
|
-
}
|
|
1726
|
-
return resolve2(srcRoot, raw);
|
|
1727
|
-
}
|
|
1728
|
-
|
|
1729
|
-
// src/plugin/state.ts
|
|
1730
|
-
function createEmptyScanState() {
|
|
1731
|
-
return {
|
|
1732
|
-
moduleMeta: /* @__PURE__ */ new Map(),
|
|
1733
|
-
pageNavigationMap: /* @__PURE__ */ new Map(),
|
|
1734
|
-
templateComponentMap: /* @__PURE__ */ new Map(),
|
|
1735
|
-
templatePathSet: /* @__PURE__ */ new Set(),
|
|
1736
|
-
componentTagMap: /* @__PURE__ */ new Map(),
|
|
1737
|
-
componentIdMap: /* @__PURE__ */ new Map(),
|
|
1738
|
-
appNavigationDefaults: {},
|
|
1739
|
-
appComponentTags: {},
|
|
1740
|
-
scanResult: {
|
|
1741
|
-
app: void 0,
|
|
1742
|
-
pages: [],
|
|
1743
|
-
components: []
|
|
1744
|
-
}
|
|
1745
|
-
};
|
|
1746
|
-
}
|
|
1747
|
-
|
|
1748
|
-
// src/plugin/index.ts
|
|
1749
|
-
function isTemplateFile(id) {
|
|
1750
|
-
const lower = id.toLowerCase();
|
|
1751
|
-
return TEMPLATE_EXTS.some((ext) => lower.endsWith(ext));
|
|
1752
|
-
}
|
|
1753
|
-
function isWxsFile(id) {
|
|
1754
|
-
const lower = id.toLowerCase();
|
|
1755
|
-
return WXS_EXTS.some((ext) => lower.endsWith(ext));
|
|
1756
|
-
}
|
|
1757
|
-
function hasWxsQuery(id) {
|
|
1758
|
-
return id.includes("?wxs") || id.includes("&wxs");
|
|
1759
|
-
}
|
|
1760
|
-
function createInlineStyleModule(css) {
|
|
1761
|
-
const serialized = JSON.stringify(css);
|
|
1762
|
-
return [
|
|
1763
|
-
`const __weapp_injected_styles__ = new Map()`,
|
|
1764
|
-
`function __weapp_createStyleId__(css) {`,
|
|
1765
|
-
` let hash = 2166136261`,
|
|
1766
|
-
` for (let i = 0; i < css.length; i++) {`,
|
|
1767
|
-
` hash ^= css.charCodeAt(i)`,
|
|
1768
|
-
` hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)`,
|
|
1769
|
-
` }`,
|
|
1770
|
-
` return 'weapp-web-style-' + (hash >>> 0).toString(36)`,
|
|
1771
|
-
`}`,
|
|
1772
|
-
`function __weapp_removeStyle__(id) {`,
|
|
1773
|
-
` if (typeof document === 'undefined') {`,
|
|
1774
|
-
` return`,
|
|
1775
|
-
` }`,
|
|
1776
|
-
` const style = __weapp_injected_styles__.get(id)`,
|
|
1777
|
-
` if (style) {`,
|
|
1778
|
-
` style.remove()`,
|
|
1779
|
-
` __weapp_injected_styles__.delete(id)`,
|
|
1780
|
-
` }`,
|
|
1781
|
-
`}`,
|
|
1782
|
-
`function injectStyle(css, id) {`,
|
|
1783
|
-
` if (typeof document === 'undefined') {`,
|
|
1784
|
-
` return () => {}`,
|
|
1785
|
-
` }`,
|
|
1786
|
-
` const styleId = id ?? __weapp_createStyleId__(css)`,
|
|
1787
|
-
` const existing = __weapp_injected_styles__.get(styleId)`,
|
|
1788
|
-
` if (existing) {`,
|
|
1789
|
-
` if (existing.textContent !== css) {`,
|
|
1790
|
-
` existing.textContent = css`,
|
|
1791
|
-
` }`,
|
|
1792
|
-
` return () => __weapp_removeStyle__(styleId)`,
|
|
1793
|
-
` }`,
|
|
1794
|
-
` const style = document.createElement('style')`,
|
|
1795
|
-
` style.id = styleId`,
|
|
1796
|
-
` style.textContent = css`,
|
|
1797
|
-
` document.head.append(style)`,
|
|
1798
|
-
` __weapp_injected_styles__.set(styleId, style)`,
|
|
1799
|
-
` return () => __weapp_removeStyle__(styleId)`,
|
|
1800
|
-
`}`,
|
|
1801
|
-
`const css = ${serialized}`,
|
|
1802
|
-
`export default css`,
|
|
1803
|
-
`export function useStyle(id) {`,
|
|
1804
|
-
` return injectStyle(css, id)`,
|
|
1805
|
-
`}`
|
|
1806
|
-
].join("\n");
|
|
1807
|
-
}
|
|
1808
|
-
function weappWebPlugin(options = {}) {
|
|
1809
|
-
let root = process2.cwd();
|
|
1810
|
-
let srcRoot = resolve3(root, options.srcDir ?? "src");
|
|
1811
|
-
let enableHmr = false;
|
|
1812
|
-
const state = createEmptyScanState();
|
|
1813
|
-
const wxssOptions = options.wxss;
|
|
1814
|
-
const resolveTemplatePath = (raw, importer) => resolveTemplatePathSync(raw, importer, srcRoot);
|
|
1815
|
-
const resolveWxsPath = (raw, importer) => resolveWxsPathSync(raw, importer, srcRoot);
|
|
1816
|
-
return {
|
|
1817
|
-
name: "@weapp-vite/web",
|
|
1818
|
-
enforce: "pre",
|
|
1819
|
-
async configResolved(config) {
|
|
1820
|
-
root = config.root;
|
|
1821
|
-
srcRoot = resolve3(root, options.srcDir ?? "src");
|
|
1822
|
-
enableHmr = config.command === "serve";
|
|
1823
|
-
await scanProject({ srcRoot, warn: this.warn?.bind(this), state });
|
|
1824
|
-
},
|
|
1825
|
-
async buildStart() {
|
|
1826
|
-
await scanProject({ srcRoot, warn: this.warn?.bind(this), state });
|
|
1827
|
-
},
|
|
1828
|
-
resolveId(id) {
|
|
1829
|
-
if (id === "/@weapp-vite/web/entry" || id === "@weapp-vite/web/entry") {
|
|
1830
|
-
return ENTRY_ID;
|
|
1831
|
-
}
|
|
1832
|
-
return null;
|
|
1833
|
-
},
|
|
1834
|
-
load(id) {
|
|
1835
|
-
if (id === ENTRY_ID) {
|
|
1836
|
-
return generateEntryModule(state.scanResult, root, wxssOptions, options);
|
|
1837
|
-
}
|
|
1838
|
-
return null;
|
|
1839
|
-
},
|
|
1840
|
-
async handleHotUpdate(ctx) {
|
|
1841
|
-
const clean = cleanUrl(ctx.file);
|
|
1842
|
-
if (clean.endsWith(".json") || isTemplateFile(clean) || isWxsFile(clean) || clean.endsWith(".wxss") || SCRIPT_EXTS.includes(extname4(clean))) {
|
|
1843
|
-
await scanProject({ srcRoot, warn: this.warn?.bind(this), state });
|
|
1844
|
-
}
|
|
1845
|
-
},
|
|
1846
|
-
transform(code, id) {
|
|
1847
|
-
const clean = cleanUrl(id);
|
|
1848
|
-
if (isTemplateFile(clean)) {
|
|
1849
|
-
if (isHtmlEntry(clean, root)) {
|
|
1850
|
-
return null;
|
|
1851
|
-
}
|
|
1852
|
-
const normalizedId = normalizePath2(clean);
|
|
1853
|
-
if (state.templatePathSet.size > 0) {
|
|
1854
|
-
if (!isInsideDir(clean, srcRoot)) {
|
|
1855
|
-
return null;
|
|
1856
|
-
}
|
|
1857
|
-
if (!state.templatePathSet.has(normalizedId)) {
|
|
1858
|
-
return null;
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
const navigationConfig = state.pageNavigationMap.get(normalizedId);
|
|
1862
|
-
const componentTags = state.templateComponentMap.get(normalizedId);
|
|
1863
|
-
const { code: compiled, dependencies, warnings } = compileWxml({
|
|
1864
|
-
id: clean,
|
|
1865
|
-
source: code,
|
|
1866
|
-
resolveTemplatePath,
|
|
1867
|
-
resolveWxsPath,
|
|
1868
|
-
navigationBar: navigationConfig ? { config: navigationConfig } : void 0,
|
|
1869
|
-
componentTags
|
|
1870
|
-
});
|
|
1871
|
-
if (dependencies.length > 0 && "addWatchFile" in this) {
|
|
1872
|
-
for (const dep of dependencies) {
|
|
1873
|
-
this.addWatchFile(dep);
|
|
1874
|
-
}
|
|
1875
|
-
}
|
|
1876
|
-
if (warnings?.length && "warn" in this) {
|
|
1877
|
-
for (const warning of warnings) {
|
|
1878
|
-
this.warn(warning);
|
|
1879
|
-
}
|
|
1880
|
-
}
|
|
1881
|
-
return { code: compiled, map: null };
|
|
1882
|
-
}
|
|
1883
|
-
if (isWxsFile(clean) || hasWxsQuery(id)) {
|
|
1884
|
-
const { code: compiled, dependencies, warnings } = transformWxsToEsm(code, clean, {
|
|
1885
|
-
resolvePath: resolveWxsPath,
|
|
1886
|
-
toImportPath: (resolved, importer) => normalizePath2(toRelativeImport2(importer, resolved))
|
|
1887
|
-
});
|
|
1888
|
-
if (dependencies.length > 0 && "addWatchFile" in this) {
|
|
1889
|
-
for (const dep of dependencies) {
|
|
1890
|
-
this.addWatchFile(dep);
|
|
1891
|
-
}
|
|
1892
|
-
}
|
|
1893
|
-
if (warnings?.length && "warn" in this) {
|
|
1894
|
-
for (const warning of warnings) {
|
|
1895
|
-
this.warn(warning);
|
|
1896
|
-
}
|
|
1897
|
-
}
|
|
1898
|
-
return { code: compiled, map: null };
|
|
1899
|
-
}
|
|
1900
|
-
if (TRANSFORM_STYLE_EXTS.some((ext) => clean.endsWith(ext))) {
|
|
1901
|
-
const { css } = transformWxssToCss(code, wxssOptions);
|
|
1902
|
-
return {
|
|
1903
|
-
code: createInlineStyleModule(css),
|
|
1904
|
-
map: null
|
|
1905
|
-
};
|
|
1906
|
-
}
|
|
1907
|
-
if (STYLE_EXTS.some((ext) => clean.endsWith(ext)) && !clean.endsWith(".wxss")) {
|
|
1908
|
-
const { css } = transformWxssToCss(code, wxssOptions);
|
|
1909
|
-
return { code: css, map: null };
|
|
1910
|
-
}
|
|
1911
|
-
if (!SCRIPT_EXTS.some((ext) => clean.endsWith(ext))) {
|
|
1912
|
-
return null;
|
|
1913
|
-
}
|
|
1914
|
-
if (clean.includes("node_modules")) {
|
|
1915
|
-
return null;
|
|
1916
|
-
}
|
|
1917
|
-
const meta = state.moduleMeta.get(normalizePath2(clean));
|
|
1918
|
-
if (!meta) {
|
|
1919
|
-
return null;
|
|
1920
|
-
}
|
|
1921
|
-
return transformScriptModule({
|
|
1922
|
-
code,
|
|
1923
|
-
cleanId: clean,
|
|
1924
|
-
meta,
|
|
1925
|
-
enableHmr
|
|
1926
|
-
});
|
|
1927
|
-
}
|
|
1928
|
-
};
|
|
1929
|
-
}
|
|
1930
|
-
export {
|
|
1931
|
-
weappWebPlugin
|
|
1932
|
-
};
|
|
1
|
+
import { t as weappWebPlugin } from "./plugin-CsqbKOh1.mjs";
|
|
2
|
+
export { weappWebPlugin };
|