weapp-vite 1.9.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CompilerContext-BpoRCDyM.d.ts +243 -0
- package/dist/CompilerContext-D9i0PEsF.d.cts +243 -0
- package/dist/auto-import-components/resolvers.cjs +5 -4
- package/dist/auto-import-components/resolvers.mjs +6 -5
- package/dist/{chunk-WKC6OQ3Q.mjs → chunk-3HVIVQUO.mjs} +4 -2
- package/dist/{chunk-2FYXPWJ7.mjs → chunk-5E4BX2PC.mjs} +3 -1
- package/dist/chunk-EO33M7LR.mjs +2714 -0
- package/dist/cli.cjs +1878 -1227
- package/dist/cli.mjs +38 -23
- package/dist/{config-YkkDB5_Y.d.ts → config-BM_dhf8V.d.ts} +42 -46
- package/dist/{config-D__5C2s3.d.cts → config-DYSwq1ku.d.cts} +42 -46
- package/dist/config.cjs +2 -0
- package/dist/config.d.cts +3 -4
- package/dist/config.d.ts +3 -4
- package/dist/config.mjs +2 -2
- package/dist/index.cjs +1830 -1187
- package/dist/index.d.cts +8 -11
- package/dist/index.d.ts +8 -11
- package/dist/index.mjs +4 -5
- package/dist/json.cjs +11 -10
- package/dist/json.d.cts +6 -7
- package/dist/json.d.ts +6 -7
- package/dist/json.mjs +12 -11
- package/package.json +25 -11
- package/bin/weapp-vite-cjs.js +0 -2
- package/bin/weapp-vite-esm.js +0 -2
- package/dist/CompilerContext-C0ZHcUt-.d.cts +0 -128
- package/dist/CompilerContext-DzZuTlqZ.d.ts +0 -128
- package/dist/chunk-I36YQNJ7.mjs +0 -2078
package/dist/chunk-I36YQNJ7.mjs
DELETED
|
@@ -1,2078 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__commonJS,
|
|
3
|
-
__toESM,
|
|
4
|
-
init_esm_shims
|
|
5
|
-
} from "./chunk-WKC6OQ3Q.mjs";
|
|
6
|
-
|
|
7
|
-
// ../../node_modules/.pnpm/debounce@2.2.0/node_modules/debounce/index.js
|
|
8
|
-
var require_debounce = __commonJS({
|
|
9
|
-
"../../node_modules/.pnpm/debounce@2.2.0/node_modules/debounce/index.js"(exports, module) {
|
|
10
|
-
"use strict";
|
|
11
|
-
init_esm_shims();
|
|
12
|
-
function debounce2(function_, wait = 100, options = {}) {
|
|
13
|
-
if (typeof function_ !== "function") {
|
|
14
|
-
throw new TypeError(`Expected the first parameter to be a function, got \`${typeof function_}\`.`);
|
|
15
|
-
}
|
|
16
|
-
if (wait < 0) {
|
|
17
|
-
throw new RangeError("`wait` must not be negative.");
|
|
18
|
-
}
|
|
19
|
-
const { immediate } = typeof options === "boolean" ? { immediate: options } : options;
|
|
20
|
-
let storedContext;
|
|
21
|
-
let storedArguments;
|
|
22
|
-
let timeoutId;
|
|
23
|
-
let timestamp;
|
|
24
|
-
let result;
|
|
25
|
-
function run() {
|
|
26
|
-
const callContext = storedContext;
|
|
27
|
-
const callArguments = storedArguments;
|
|
28
|
-
storedContext = void 0;
|
|
29
|
-
storedArguments = void 0;
|
|
30
|
-
result = function_.apply(callContext, callArguments);
|
|
31
|
-
return result;
|
|
32
|
-
}
|
|
33
|
-
function later() {
|
|
34
|
-
const last = Date.now() - timestamp;
|
|
35
|
-
if (last < wait && last >= 0) {
|
|
36
|
-
timeoutId = setTimeout(later, wait - last);
|
|
37
|
-
} else {
|
|
38
|
-
timeoutId = void 0;
|
|
39
|
-
if (!immediate) {
|
|
40
|
-
result = run();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const debounced = function(...arguments_) {
|
|
45
|
-
if (storedContext && this !== storedContext && Object.getPrototypeOf(this) === Object.getPrototypeOf(storedContext)) {
|
|
46
|
-
throw new Error("Debounced method called with different contexts of the same prototype.");
|
|
47
|
-
}
|
|
48
|
-
storedContext = this;
|
|
49
|
-
storedArguments = arguments_;
|
|
50
|
-
timestamp = Date.now();
|
|
51
|
-
const callNow = immediate && !timeoutId;
|
|
52
|
-
if (!timeoutId) {
|
|
53
|
-
timeoutId = setTimeout(later, wait);
|
|
54
|
-
}
|
|
55
|
-
if (callNow) {
|
|
56
|
-
result = run();
|
|
57
|
-
}
|
|
58
|
-
return result;
|
|
59
|
-
};
|
|
60
|
-
Object.defineProperty(debounced, "isPending", {
|
|
61
|
-
get() {
|
|
62
|
-
return timeoutId !== void 0;
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
debounced.clear = () => {
|
|
66
|
-
if (!timeoutId) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
clearTimeout(timeoutId);
|
|
70
|
-
timeoutId = void 0;
|
|
71
|
-
};
|
|
72
|
-
debounced.flush = () => {
|
|
73
|
-
if (!timeoutId) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
debounced.trigger();
|
|
77
|
-
};
|
|
78
|
-
debounced.trigger = () => {
|
|
79
|
-
result = run();
|
|
80
|
-
debounced.clear();
|
|
81
|
-
};
|
|
82
|
-
return debounced;
|
|
83
|
-
}
|
|
84
|
-
module.exports.debounce = debounce2;
|
|
85
|
-
module.exports = debounce2;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// src/constants.ts
|
|
90
|
-
init_esm_shims();
|
|
91
|
-
|
|
92
|
-
// package.json
|
|
93
|
-
var version = "1.9.2";
|
|
94
|
-
|
|
95
|
-
// src/constants.ts
|
|
96
|
-
var VERSION = version;
|
|
97
|
-
var jsExtensions = ["ts", "js"];
|
|
98
|
-
var configExtensions = [...jsExtensions.map((x) => `json.${x}`), "json"];
|
|
99
|
-
var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
|
|
100
|
-
var templateExtensions = ["wxml", "html"];
|
|
101
|
-
|
|
102
|
-
// src/logger.ts
|
|
103
|
-
init_esm_shims();
|
|
104
|
-
import logger from "@weapp-core/logger";
|
|
105
|
-
var logger_default = logger;
|
|
106
|
-
|
|
107
|
-
// src/context/CompilerContext.ts
|
|
108
|
-
init_esm_shims();
|
|
109
|
-
import process from "node:process";
|
|
110
|
-
import { defu as defu5, get as get2, isObject as isObject5, removeExtension as removeExtension3, set as set2 } from "@weapp-core/shared";
|
|
111
|
-
import { deleteAsync } from "del";
|
|
112
|
-
import fs8 from "fs-extra";
|
|
113
|
-
import path8 from "pathe";
|
|
114
|
-
import pm from "picomatch";
|
|
115
|
-
import { build as build2 } from "vite";
|
|
116
|
-
|
|
117
|
-
// src/defaults.ts
|
|
118
|
-
init_esm_shims();
|
|
119
|
-
var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
|
|
120
|
-
function getOutputExtensions(platform) {
|
|
121
|
-
switch (platform) {
|
|
122
|
-
// https://opendocs.alipay.com/mini/0ai07p?pathHash=01051631
|
|
123
|
-
// Native 渲染
|
|
124
|
-
case "alipay": {
|
|
125
|
-
return {
|
|
126
|
-
js: "js",
|
|
127
|
-
json: "json",
|
|
128
|
-
wxml: "axml",
|
|
129
|
-
wxss: "acss",
|
|
130
|
-
wxs: "sjs"
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
case "tt": {
|
|
134
|
-
return {
|
|
135
|
-
js: "js",
|
|
136
|
-
json: "json",
|
|
137
|
-
wxml: "ttml",
|
|
138
|
-
wxss: "ttss"
|
|
139
|
-
// tt 没有 wxs
|
|
140
|
-
// wxs: 'wxs',
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
case "weapp":
|
|
144
|
-
default: {
|
|
145
|
-
return {
|
|
146
|
-
js: "js",
|
|
147
|
-
json: "json",
|
|
148
|
-
wxml: "wxml",
|
|
149
|
-
wxss: "wxss",
|
|
150
|
-
wxs: "wxs"
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
function getWeappViteConfig() {
|
|
156
|
-
return {
|
|
157
|
-
enhance: {
|
|
158
|
-
wxml: true,
|
|
159
|
-
wxs: true
|
|
160
|
-
},
|
|
161
|
-
platform: "weapp"
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// src/plugins/index.ts
|
|
166
|
-
init_esm_shims();
|
|
167
|
-
var import_debounce = __toESM(require_debounce(), 1);
|
|
168
|
-
import { isObject as isObject2, removeExtension } from "@weapp-core/shared";
|
|
169
|
-
import { fdir as Fdir } from "fdir";
|
|
170
|
-
import fs3 from "fs-extra";
|
|
171
|
-
import MagicString2 from "magic-string";
|
|
172
|
-
import path4 from "pathe";
|
|
173
|
-
import { isCSSRequest } from "vite";
|
|
174
|
-
|
|
175
|
-
// src/debugger.ts
|
|
176
|
-
init_esm_shims();
|
|
177
|
-
import createDebug from "debug";
|
|
178
|
-
function createDebugger(namespace) {
|
|
179
|
-
const debug3 = createDebug(namespace);
|
|
180
|
-
if (debug3.enabled) {
|
|
181
|
-
return debug3;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// src/postcss/index.ts
|
|
186
|
-
init_esm_shims();
|
|
187
|
-
import postcss from "postcss";
|
|
188
|
-
|
|
189
|
-
// src/postcss/constants.ts
|
|
190
|
-
init_esm_shims();
|
|
191
|
-
var cssAtRulePrefix = "wv";
|
|
192
|
-
var IFDEF = "#ifdef";
|
|
193
|
-
var IFNDEF = "#ifndef";
|
|
194
|
-
var ENDIF = "#endif";
|
|
195
|
-
|
|
196
|
-
// src/postcss/post.ts
|
|
197
|
-
init_esm_shims();
|
|
198
|
-
var postCreator = (options = { platform: "weapp" }) => {
|
|
199
|
-
const atRulePrefixRegExp = new RegExp(`^${cssAtRulePrefix}-`);
|
|
200
|
-
return {
|
|
201
|
-
postcssPlugin: "postcss-weapp-vite-plugin-post",
|
|
202
|
-
prepare() {
|
|
203
|
-
return {
|
|
204
|
-
AtRule(atRule) {
|
|
205
|
-
if (atRulePrefixRegExp.test(atRule.name)) {
|
|
206
|
-
if (atRule.name === `${cssAtRulePrefix}-keep-import`) {
|
|
207
|
-
atRule.name = "import";
|
|
208
|
-
} else if (atRule.name === `${cssAtRulePrefix}-if`) {
|
|
209
|
-
const matches2 = atRule.params.matchAll(/\((\w+)\)/g);
|
|
210
|
-
const isRemove = [...matches2].every((x) => x[1] !== options.platform);
|
|
211
|
-
if (isRemove) {
|
|
212
|
-
atRule.remove();
|
|
213
|
-
} else {
|
|
214
|
-
atRule.replaceWith(atRule.nodes);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
Comment(comment) {
|
|
220
|
-
const wordList = comment.text.split(" ");
|
|
221
|
-
if (wordList.includes(IFDEF)) {
|
|
222
|
-
if (!wordList.includes(options.platform)) {
|
|
223
|
-
let next = comment.next();
|
|
224
|
-
while (next) {
|
|
225
|
-
if (next.type === "comment" && next.text.trim() === ENDIF) {
|
|
226
|
-
break;
|
|
227
|
-
}
|
|
228
|
-
const temp = next.next();
|
|
229
|
-
next.remove();
|
|
230
|
-
next = temp;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (wordList.includes(IFNDEF)) {
|
|
235
|
-
if (wordList.includes(options.platform)) {
|
|
236
|
-
let next = comment.next();
|
|
237
|
-
while (next) {
|
|
238
|
-
if (next.type === "comment" && next.text.trim() === ENDIF) {
|
|
239
|
-
break;
|
|
240
|
-
}
|
|
241
|
-
const temp = next.next();
|
|
242
|
-
next.remove();
|
|
243
|
-
next = temp;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
comment.remove();
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
};
|
|
253
|
-
postCreator.postcss = true;
|
|
254
|
-
|
|
255
|
-
// src/postcss/index.ts
|
|
256
|
-
async function cssPostProcess(code, options) {
|
|
257
|
-
if (new RegExp(`@${cssAtRulePrefix}-|${IFDEF}|${IFNDEF}`).test(code)) {
|
|
258
|
-
const { css } = await postcss([postCreator(options)]).process(code).async();
|
|
259
|
-
return css;
|
|
260
|
-
}
|
|
261
|
-
return code;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// src/utils/index.ts
|
|
265
|
-
init_esm_shims();
|
|
266
|
-
|
|
267
|
-
// src/utils/file.ts
|
|
268
|
-
init_esm_shims();
|
|
269
|
-
import fs from "fs-extra";
|
|
270
|
-
import path from "pathe";
|
|
271
|
-
function isJsOrTs(name) {
|
|
272
|
-
if (typeof name === "string") {
|
|
273
|
-
return jsExtensions.some((x) => name.endsWith(`.${x}`));
|
|
274
|
-
}
|
|
275
|
-
return false;
|
|
276
|
-
}
|
|
277
|
-
function changeFileExtension(filePath, extension) {
|
|
278
|
-
if (typeof filePath !== "string") {
|
|
279
|
-
throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`);
|
|
280
|
-
}
|
|
281
|
-
if (typeof extension !== "string") {
|
|
282
|
-
throw new TypeError(`Expected \`extension\` to be a string, got \`${typeof extension}\`.`);
|
|
283
|
-
}
|
|
284
|
-
if (filePath === "") {
|
|
285
|
-
return "";
|
|
286
|
-
}
|
|
287
|
-
extension = extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
|
|
288
|
-
const basename = path.basename(filePath, path.extname(filePath));
|
|
289
|
-
return path.join(path.dirname(filePath), basename + extension);
|
|
290
|
-
}
|
|
291
|
-
async function findJsEntry(filepath) {
|
|
292
|
-
for (const ext of jsExtensions) {
|
|
293
|
-
const p = changeFileExtension(filepath, ext);
|
|
294
|
-
if (await fs.exists(p)) {
|
|
295
|
-
return p;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
async function findJsonEntry(filepath) {
|
|
300
|
-
for (const ext of configExtensions) {
|
|
301
|
-
const p = changeFileExtension(filepath, ext);
|
|
302
|
-
if (await fs.exists(p)) {
|
|
303
|
-
return p;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
async function findTemplateEntry(filepath) {
|
|
308
|
-
for (const ext of templateExtensions) {
|
|
309
|
-
const p = changeFileExtension(filepath, ext);
|
|
310
|
-
if (await fs.exists(p)) {
|
|
311
|
-
return p;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
function resolveGlobs(globs, subPackageMeta) {
|
|
316
|
-
if (Array.isArray(globs)) {
|
|
317
|
-
return globs;
|
|
318
|
-
} else if (typeof globs === "function") {
|
|
319
|
-
return globs(subPackageMeta);
|
|
320
|
-
}
|
|
321
|
-
return [];
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// src/utils/json.ts
|
|
325
|
-
init_esm_shims();
|
|
326
|
-
import { get, isObject, set } from "@weapp-core/shared";
|
|
327
|
-
import { parse as parseJson, stringify } from "comment-json";
|
|
328
|
-
import path2 from "pathe";
|
|
329
|
-
function jsonFileRemoveJsExtension(fileName) {
|
|
330
|
-
return fileName.replace(/\.[jt]s$/, "");
|
|
331
|
-
}
|
|
332
|
-
function stringifyJson(value, replacer) {
|
|
333
|
-
return stringify(value, replacer, 2);
|
|
334
|
-
}
|
|
335
|
-
function matches(pattern, importee) {
|
|
336
|
-
if (pattern instanceof RegExp) {
|
|
337
|
-
return pattern.test(importee);
|
|
338
|
-
}
|
|
339
|
-
if (importee.length < pattern.length) {
|
|
340
|
-
return false;
|
|
341
|
-
}
|
|
342
|
-
if (importee === pattern) {
|
|
343
|
-
return true;
|
|
344
|
-
}
|
|
345
|
-
return importee.startsWith(pattern + "/");
|
|
346
|
-
}
|
|
347
|
-
function getAliasEntries({ entries } = {}) {
|
|
348
|
-
if (!entries) {
|
|
349
|
-
return [];
|
|
350
|
-
}
|
|
351
|
-
if (Array.isArray(entries)) {
|
|
352
|
-
return entries.map((entry) => {
|
|
353
|
-
return {
|
|
354
|
-
find: entry.find,
|
|
355
|
-
replacement: entry.replacement
|
|
356
|
-
};
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
return Object.entries(entries).map(([key, value]) => {
|
|
360
|
-
return { find: key, replacement: value };
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
function resolveImportee(importee, entry, aliasEntries) {
|
|
364
|
-
if (Array.isArray(aliasEntries)) {
|
|
365
|
-
if (!entry.jsonPath) {
|
|
366
|
-
return importee;
|
|
367
|
-
}
|
|
368
|
-
const matchedEntry = aliasEntries.find((x) => matches(x.find, importee));
|
|
369
|
-
if (!matchedEntry) {
|
|
370
|
-
return importee;
|
|
371
|
-
}
|
|
372
|
-
const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement);
|
|
373
|
-
return path2.relative(path2.dirname(entry.jsonPath), updatedId);
|
|
374
|
-
}
|
|
375
|
-
return importee;
|
|
376
|
-
}
|
|
377
|
-
function resolveJson(entry, aliasEntries) {
|
|
378
|
-
if (entry.json) {
|
|
379
|
-
const json = structuredClone(entry.json);
|
|
380
|
-
if (entry.jsonPath && Array.isArray(aliasEntries)) {
|
|
381
|
-
const usingComponents = get(json, "usingComponents");
|
|
382
|
-
if (isObject(usingComponents)) {
|
|
383
|
-
for (const [key, importee] of Object.entries(usingComponents)) {
|
|
384
|
-
const resolvedId = resolveImportee(importee, entry, aliasEntries);
|
|
385
|
-
set(json, `usingComponents.${key}`, resolvedId);
|
|
386
|
-
}
|
|
387
|
-
set(json, "usingComponents", usingComponents);
|
|
388
|
-
}
|
|
389
|
-
if (entry.type === "app") {
|
|
390
|
-
const fields = ["subPackages", "subpackages"];
|
|
391
|
-
for (const field of fields) {
|
|
392
|
-
const subPackages = get(json, field);
|
|
393
|
-
if (Array.isArray(subPackages)) {
|
|
394
|
-
for (const subPackage of subPackages) {
|
|
395
|
-
if (subPackage.entry) {
|
|
396
|
-
subPackage.entry = changeFileExtension(subPackage.entry, "js");
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
if (Reflect.has(json, "$schema")) {
|
|
404
|
-
delete json.$schema;
|
|
405
|
-
}
|
|
406
|
-
return stringifyJson(json);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
// src/utils/projectConfig.ts
|
|
411
|
-
init_esm_shims();
|
|
412
|
-
import fs2 from "fs-extra";
|
|
413
|
-
import path3 from "pathe";
|
|
414
|
-
async function getProjectConfig(root, options) {
|
|
415
|
-
const baseJsonPath = path3.resolve(root, "project.config.json");
|
|
416
|
-
const privateJsonPath = path3.resolve(root, "project.private.config.json");
|
|
417
|
-
let baseJson = {};
|
|
418
|
-
let privateJson = {};
|
|
419
|
-
if (await fs2.exists(baseJsonPath)) {
|
|
420
|
-
try {
|
|
421
|
-
baseJson = await fs2.readJson(baseJsonPath) || {};
|
|
422
|
-
} catch {
|
|
423
|
-
throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
|
|
424
|
-
}
|
|
425
|
-
} else {
|
|
426
|
-
throw new Error(`\u5728 ${root} \u76EE\u5F55\u4E0B\u627E\u4E0D\u5230 project.config.json`);
|
|
427
|
-
}
|
|
428
|
-
if (!options?.ignorePrivate) {
|
|
429
|
-
if (await fs2.exists(privateJsonPath)) {
|
|
430
|
-
try {
|
|
431
|
-
privateJson = await fs2.readJson(privateJsonPath) || {};
|
|
432
|
-
} catch {
|
|
433
|
-
throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.private.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return Object.assign({}, privateJson, baseJson);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// src/utils/regexp.ts
|
|
441
|
-
init_esm_shims();
|
|
442
|
-
function isRegexp(value) {
|
|
443
|
-
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
444
|
-
}
|
|
445
|
-
function regExpTest(arr = [], str) {
|
|
446
|
-
if (Array.isArray(arr)) {
|
|
447
|
-
for (const item of arr) {
|
|
448
|
-
if (typeof item === "string") {
|
|
449
|
-
if (str.includes(item)) {
|
|
450
|
-
return true;
|
|
451
|
-
}
|
|
452
|
-
} else if (isRegexp(item)) {
|
|
453
|
-
item.lastIndex = 0;
|
|
454
|
-
if (item.test(str)) {
|
|
455
|
-
return true;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
return false;
|
|
460
|
-
}
|
|
461
|
-
throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// src/wxml/index.ts
|
|
465
|
-
init_esm_shims();
|
|
466
|
-
import { defu as defu2 } from "@weapp-core/shared";
|
|
467
|
-
import { Parser } from "htmlparser2";
|
|
468
|
-
import MagicString from "magic-string";
|
|
469
|
-
|
|
470
|
-
// src/auto-import-components/builtin.ts
|
|
471
|
-
init_esm_shims();
|
|
472
|
-
|
|
473
|
-
// src/auto-import-components/builtin.auto.ts
|
|
474
|
-
init_esm_shims();
|
|
475
|
-
var components = [
|
|
476
|
-
"wxs",
|
|
477
|
-
"template",
|
|
478
|
-
"block",
|
|
479
|
-
"import",
|
|
480
|
-
"include",
|
|
481
|
-
"cover-image",
|
|
482
|
-
"cover-view",
|
|
483
|
-
"match-media",
|
|
484
|
-
"movable-area",
|
|
485
|
-
"movable-view",
|
|
486
|
-
"page-container",
|
|
487
|
-
"root-portal",
|
|
488
|
-
"scroll-view",
|
|
489
|
-
"swiper",
|
|
490
|
-
"swiper-item",
|
|
491
|
-
"view",
|
|
492
|
-
"icon",
|
|
493
|
-
"progress",
|
|
494
|
-
"rich-text",
|
|
495
|
-
"selection",
|
|
496
|
-
"text",
|
|
497
|
-
"button",
|
|
498
|
-
"checkbox",
|
|
499
|
-
"checkbox-group",
|
|
500
|
-
"editor",
|
|
501
|
-
"form",
|
|
502
|
-
"input",
|
|
503
|
-
"keyboard-accessory",
|
|
504
|
-
"label",
|
|
505
|
-
"picker",
|
|
506
|
-
"picker-view",
|
|
507
|
-
"picker-view-column",
|
|
508
|
-
"radio",
|
|
509
|
-
"radio-group",
|
|
510
|
-
"slider",
|
|
511
|
-
"switch",
|
|
512
|
-
"textarea",
|
|
513
|
-
"double-tap-gesture-handler",
|
|
514
|
-
"force-press-gesture-handler",
|
|
515
|
-
"horizontal-drag-gesture-handler",
|
|
516
|
-
"long-press-gesture-handler",
|
|
517
|
-
"pan-gesture-handler",
|
|
518
|
-
"scale-gesture-handler",
|
|
519
|
-
"tap-gesture-handler",
|
|
520
|
-
"vertical-drag-gesture-handler",
|
|
521
|
-
"draggable-sheet",
|
|
522
|
-
"grid-builder",
|
|
523
|
-
"grid-view",
|
|
524
|
-
"list-builder",
|
|
525
|
-
"list-view",
|
|
526
|
-
"nested-scroll-body",
|
|
527
|
-
"nested-scroll-header",
|
|
528
|
-
"open-container",
|
|
529
|
-
"share-element",
|
|
530
|
-
"snapshot",
|
|
531
|
-
"span",
|
|
532
|
-
"sticky-header",
|
|
533
|
-
"sticky-section",
|
|
534
|
-
"functional-page-navigator",
|
|
535
|
-
"navigator",
|
|
536
|
-
"audio",
|
|
537
|
-
"camera",
|
|
538
|
-
"channel-live",
|
|
539
|
-
"channel-video",
|
|
540
|
-
"image",
|
|
541
|
-
"live-player",
|
|
542
|
-
"live-pusher",
|
|
543
|
-
"video",
|
|
544
|
-
"voip-room",
|
|
545
|
-
"map",
|
|
546
|
-
"canvas",
|
|
547
|
-
"ad",
|
|
548
|
-
"ad-custom",
|
|
549
|
-
"official-account",
|
|
550
|
-
"open-data",
|
|
551
|
-
"store-home",
|
|
552
|
-
"store-product",
|
|
553
|
-
"web-view",
|
|
554
|
-
"native-component",
|
|
555
|
-
"aria-component",
|
|
556
|
-
"navigation-bar",
|
|
557
|
-
"page-meta"
|
|
558
|
-
];
|
|
559
|
-
|
|
560
|
-
// src/auto-import-components/builtin.ts
|
|
561
|
-
var builtinComponentsSet = new Set(components);
|
|
562
|
-
function isBuiltinComponent(tag) {
|
|
563
|
-
return builtinComponentsSet.has(tag);
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
// src/wxs/index.ts
|
|
567
|
-
init_esm_shims();
|
|
568
|
-
import babel from "@babel/core";
|
|
569
|
-
import t from "@babel/types";
|
|
570
|
-
import { defu } from "@weapp-core/shared";
|
|
571
|
-
|
|
572
|
-
// src/wxs/utils.ts
|
|
573
|
-
init_esm_shims();
|
|
574
|
-
import { addExtension, removeExtensionDeep } from "@weapp-core/shared";
|
|
575
|
-
function normalizeWxsFilename(value) {
|
|
576
|
-
return addExtension(removeExtensionDeep(value), ".wxs");
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// src/wxs/index.ts
|
|
580
|
-
function transformWxsCode(code, options) {
|
|
581
|
-
const { filename } = defu(options, {
|
|
582
|
-
filename: "wxs.ts"
|
|
583
|
-
});
|
|
584
|
-
return babel.transformSync(code, {
|
|
585
|
-
presets: [
|
|
586
|
-
["@babel/preset-env"],
|
|
587
|
-
["@babel/preset-typescript"]
|
|
588
|
-
],
|
|
589
|
-
filename,
|
|
590
|
-
plugins: [
|
|
591
|
-
{
|
|
592
|
-
visitor: {
|
|
593
|
-
Directive: {
|
|
594
|
-
enter(p) {
|
|
595
|
-
p.remove();
|
|
596
|
-
}
|
|
597
|
-
},
|
|
598
|
-
CallExpression: {
|
|
599
|
-
enter(p) {
|
|
600
|
-
if (p.get("callee").isIdentifier({
|
|
601
|
-
name: "require"
|
|
602
|
-
}) && p.get("arguments").length === 1) {
|
|
603
|
-
const importee = p.get("arguments")[0];
|
|
604
|
-
if (importee.isStringLiteral()) {
|
|
605
|
-
importee.node.value = normalizeWxsFilename(importee.node.value);
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
},
|
|
610
|
-
ExpressionStatement(p) {
|
|
611
|
-
const expression = p.node.expression;
|
|
612
|
-
if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression" && t.isIdentifier(expression.callee.object) && expression.callee.object.name === "Object" && t.isIdentifier(expression.callee.property) && expression.callee.property.name === "defineProperty" && expression.arguments.length >= 2 && t.isIdentifier(expression.arguments[0]) && expression.arguments[0].name === "exports" && t.isStringLiteral(expression.arguments[1]) && expression.arguments[1].value === "__esModule") {
|
|
613
|
-
p.remove();
|
|
614
|
-
}
|
|
615
|
-
},
|
|
616
|
-
NewExpression: {
|
|
617
|
-
enter(p) {
|
|
618
|
-
if (p.get("callee").isIdentifier({
|
|
619
|
-
name: "RegExp"
|
|
620
|
-
})) {
|
|
621
|
-
p.replaceWith(
|
|
622
|
-
t.callExpression(t.identifier("getRegExp"), p.get("arguments").map((x) => x.node))
|
|
623
|
-
);
|
|
624
|
-
} else if (p.get("callee").isIdentifier({
|
|
625
|
-
name: "Date"
|
|
626
|
-
})) {
|
|
627
|
-
p.replaceWith(
|
|
628
|
-
t.callExpression(t.identifier("getDate"), p.get("arguments").map((x) => x.node))
|
|
629
|
-
);
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
},
|
|
633
|
-
RegExpLiteral: {
|
|
634
|
-
enter(p) {
|
|
635
|
-
const args = [t.stringLiteral(p.node.pattern)];
|
|
636
|
-
if (p.node.flags) {
|
|
637
|
-
args.push(t.stringLiteral(p.node.flags));
|
|
638
|
-
}
|
|
639
|
-
p.replaceWith(
|
|
640
|
-
t.callExpression(t.identifier("getRegExp"), args)
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
|
-
},
|
|
644
|
-
MemberExpression: {
|
|
645
|
-
enter(p) {
|
|
646
|
-
if (p.get("object").isIdentifier({
|
|
647
|
-
name: "exports"
|
|
648
|
-
})) {
|
|
649
|
-
p.replaceWith(
|
|
650
|
-
t.memberExpression(
|
|
651
|
-
t.memberExpression(t.identifier("module"), t.identifier("exports")),
|
|
652
|
-
p.get("property").node
|
|
653
|
-
)
|
|
654
|
-
);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
]
|
|
661
|
-
});
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
// src/wxml/index.ts
|
|
665
|
-
var srcImportTagsMap = {
|
|
666
|
-
// audio: ['src', 'poster'],
|
|
667
|
-
// video: ['src', 'poster'],
|
|
668
|
-
// image: ['src'],
|
|
669
|
-
// https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
|
|
670
|
-
wxs: ["src"]
|
|
671
|
-
// https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html
|
|
672
|
-
// import: ['src'],
|
|
673
|
-
// include: ['src'],
|
|
674
|
-
};
|
|
675
|
-
function processWxml(wxml, options) {
|
|
676
|
-
const opts = defu2(options, {
|
|
677
|
-
excludeComponent: (tagName) => {
|
|
678
|
-
return isBuiltinComponent(tagName);
|
|
679
|
-
},
|
|
680
|
-
platform: "weapp",
|
|
681
|
-
removeComment: true
|
|
682
|
-
});
|
|
683
|
-
const ms = new MagicString(wxml.toString());
|
|
684
|
-
const deps = [];
|
|
685
|
-
let currentTagName = "";
|
|
686
|
-
let importAttrs;
|
|
687
|
-
let attrs = {};
|
|
688
|
-
const components2 = {};
|
|
689
|
-
let tagStartIndex = 0;
|
|
690
|
-
const removeStartStack = [];
|
|
691
|
-
const removeEndStack = [];
|
|
692
|
-
const commentsPositions = [];
|
|
693
|
-
const parser = new Parser(
|
|
694
|
-
{
|
|
695
|
-
onopentagname(name) {
|
|
696
|
-
currentTagName = name;
|
|
697
|
-
importAttrs = srcImportTagsMap[currentTagName];
|
|
698
|
-
tagStartIndex = parser.startIndex;
|
|
699
|
-
},
|
|
700
|
-
onattribute(name, value, quote) {
|
|
701
|
-
attrs[name] = value;
|
|
702
|
-
if (importAttrs) {
|
|
703
|
-
for (const attrName of importAttrs) {
|
|
704
|
-
if (attrName === name) {
|
|
705
|
-
deps.push({
|
|
706
|
-
name,
|
|
707
|
-
value,
|
|
708
|
-
quote,
|
|
709
|
-
tagName: currentTagName,
|
|
710
|
-
start: parser.startIndex,
|
|
711
|
-
end: parser.endIndex,
|
|
712
|
-
attrs
|
|
713
|
-
});
|
|
714
|
-
if (currentTagName === "wxs" && name === "src") {
|
|
715
|
-
if (/\.wxs.[jt]s$/.test(value)) {
|
|
716
|
-
ms.update(parser.startIndex + 5, parser.endIndex - 1, normalizeWxsFilename(value));
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
if (name.startsWith("@")) {
|
|
723
|
-
const start = parser.startIndex;
|
|
724
|
-
const end = parser.startIndex + name.length;
|
|
725
|
-
const { dir, mods } = name.split(".").filter((x) => x).reduce((acc, cur, idx) => {
|
|
726
|
-
if (idx === 0) {
|
|
727
|
-
acc.dir = cur;
|
|
728
|
-
} else {
|
|
729
|
-
acc.mods[cur] = true;
|
|
730
|
-
}
|
|
731
|
-
return acc;
|
|
732
|
-
}, { dir: "", mods: {} });
|
|
733
|
-
let rep;
|
|
734
|
-
if (mods.catch && mods.capture) {
|
|
735
|
-
rep = `capture-catch:${dir.slice(1)}`;
|
|
736
|
-
} else if (mods.catch) {
|
|
737
|
-
rep = `catch:${dir.slice(1)}`;
|
|
738
|
-
} else if (mods.mut) {
|
|
739
|
-
rep = `mut-bind:${dir.slice(1)}`;
|
|
740
|
-
} else if (mods.capture) {
|
|
741
|
-
rep = `capture-bind:${dir.slice(1)}`;
|
|
742
|
-
} else {
|
|
743
|
-
rep = `bind:${dir.slice(1)}`;
|
|
744
|
-
}
|
|
745
|
-
if (rep) {
|
|
746
|
-
ms.update(start, end, rep);
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
if (currentTagName === "wxs" && name === "lang" && jsExtensions.includes(value)) {
|
|
750
|
-
ms.update(parser.startIndex, parser.endIndex, "");
|
|
751
|
-
}
|
|
752
|
-
},
|
|
753
|
-
onclosetag() {
|
|
754
|
-
if (currentTagName && !opts.excludeComponent(currentTagName)) {
|
|
755
|
-
if (Array.isArray(components2[currentTagName])) {
|
|
756
|
-
components2[currentTagName].push({
|
|
757
|
-
start: tagStartIndex,
|
|
758
|
-
end: parser.endIndex + 1
|
|
759
|
-
});
|
|
760
|
-
} else {
|
|
761
|
-
components2[currentTagName] = [{
|
|
762
|
-
start: tagStartIndex,
|
|
763
|
-
end: parser.endIndex + 1
|
|
764
|
-
}];
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
currentTagName = "";
|
|
768
|
-
attrs = {};
|
|
769
|
-
importAttrs = void 0;
|
|
770
|
-
tagStartIndex = 0;
|
|
771
|
-
},
|
|
772
|
-
ontext(data) {
|
|
773
|
-
if (currentTagName === "wxs" && jsExtensions.includes(attrs.lang)) {
|
|
774
|
-
const res = transformWxsCode(data);
|
|
775
|
-
if (res && res.code) {
|
|
776
|
-
ms.update(parser.startIndex, parser.endIndex, `
|
|
777
|
-
${res.code}`);
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
},
|
|
781
|
-
// <!-- #ifdef %PLATFORM% -->
|
|
782
|
-
// 平台特有的组件
|
|
783
|
-
// <!-- #endif -->
|
|
784
|
-
oncomment(data) {
|
|
785
|
-
let match = /#ifdef\s+(\w+)/.exec(data);
|
|
786
|
-
if (match) {
|
|
787
|
-
if (match[1] !== opts.platform) {
|
|
788
|
-
removeStartStack.push(parser.startIndex);
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
match = /#endif/.exec(data);
|
|
792
|
-
if (match) {
|
|
793
|
-
removeEndStack.push(parser.endIndex + 1);
|
|
794
|
-
}
|
|
795
|
-
commentsPositions.push({
|
|
796
|
-
start: parser.startIndex,
|
|
797
|
-
end: parser.endIndex + 1
|
|
798
|
-
});
|
|
799
|
-
}
|
|
800
|
-
},
|
|
801
|
-
{
|
|
802
|
-
lowerCaseTags: false,
|
|
803
|
-
xmlMode: true
|
|
804
|
-
}
|
|
805
|
-
);
|
|
806
|
-
parser.write(
|
|
807
|
-
ms.original
|
|
808
|
-
);
|
|
809
|
-
parser.end();
|
|
810
|
-
for (let i = 0; i < removeStartStack.length; i++) {
|
|
811
|
-
const startIndex = removeStartStack[i];
|
|
812
|
-
for (let j = i; j < removeEndStack.length; j++) {
|
|
813
|
-
const endIndex = removeEndStack[j];
|
|
814
|
-
if (endIndex > startIndex) {
|
|
815
|
-
ms.remove(startIndex, endIndex);
|
|
816
|
-
break;
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
if (opts.removeComment) {
|
|
821
|
-
for (const { end, start } of commentsPositions) {
|
|
822
|
-
ms.remove(start, end);
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
return {
|
|
826
|
-
components: components2,
|
|
827
|
-
deps,
|
|
828
|
-
code: ms.toString(),
|
|
829
|
-
removeStartStack,
|
|
830
|
-
removeEndStack
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
// src/plugins/parse.ts
|
|
835
|
-
init_esm_shims();
|
|
836
|
-
function parseRequest(id) {
|
|
837
|
-
const [filename, rawQuery] = id.split(`?`, 2);
|
|
838
|
-
const query = Object.fromEntries(new URLSearchParams(rawQuery));
|
|
839
|
-
if (Reflect.has(query, "wxss")) {
|
|
840
|
-
query.wxss = true;
|
|
841
|
-
}
|
|
842
|
-
return {
|
|
843
|
-
filename,
|
|
844
|
-
query
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
function getCssRealPath(res) {
|
|
848
|
-
if (res.query.wxss) {
|
|
849
|
-
return changeFileExtension(res.filename, "wxss");
|
|
850
|
-
}
|
|
851
|
-
return res.filename;
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// src/plugins/index.ts
|
|
855
|
-
var debug = createDebugger("weapp-vite:plugin");
|
|
856
|
-
function isEmptyObject(obj) {
|
|
857
|
-
for (const key in obj) {
|
|
858
|
-
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
859
|
-
return false;
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
return true;
|
|
863
|
-
}
|
|
864
|
-
function isTemplateRequest(request) {
|
|
865
|
-
return request.endsWith(".wxml") || request.endsWith(".html");
|
|
866
|
-
}
|
|
867
|
-
var debouncedLoggerSuccess = (0, import_debounce.default)((message) => {
|
|
868
|
-
return logger_default.success(message);
|
|
869
|
-
}, 25);
|
|
870
|
-
function vitePluginWeapp(ctx, subPackageMeta) {
|
|
871
|
-
let configResolved;
|
|
872
|
-
function getInputOption(entries2) {
|
|
873
|
-
return entries2.reduce((acc, cur) => {
|
|
874
|
-
acc[ctx.relativeCwd(cur)] = cur;
|
|
875
|
-
return acc;
|
|
876
|
-
}, {});
|
|
877
|
-
}
|
|
878
|
-
let entriesSet;
|
|
879
|
-
let entries;
|
|
880
|
-
const cachedEmittedFiles = [];
|
|
881
|
-
const cachedWatchFiles = [];
|
|
882
|
-
return [
|
|
883
|
-
{
|
|
884
|
-
name: "weapp-vite:pre",
|
|
885
|
-
enforce: "pre",
|
|
886
|
-
// config->configResolved->|watching|options->buildStart
|
|
887
|
-
// config(config, env) {
|
|
888
|
-
// debug?.(config, env)
|
|
889
|
-
// },
|
|
890
|
-
configResolved(config) {
|
|
891
|
-
configResolved = config;
|
|
892
|
-
if (isObject2(configResolved.env)) {
|
|
893
|
-
for (const [key, value] of Object.entries(configResolved.env)) {
|
|
894
|
-
ctx.setDefineEnv(key, value);
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
},
|
|
898
|
-
async options(options) {
|
|
899
|
-
cachedEmittedFiles.length = 0;
|
|
900
|
-
cachedWatchFiles.length = 0;
|
|
901
|
-
ctx.resetAutoImport();
|
|
902
|
-
const { build: build3, weapp } = configResolved;
|
|
903
|
-
const ignore = [
|
|
904
|
-
...defaultExcluded,
|
|
905
|
-
`${build3.outDir}/**`
|
|
906
|
-
];
|
|
907
|
-
if (!subPackageMeta) {
|
|
908
|
-
for (const root of Object.keys(ctx.subPackageMeta)) {
|
|
909
|
-
ignore.push(path4.join(root, "**"));
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
ignore.push(...resolveGlobs(weapp?.copy?.exclude));
|
|
913
|
-
const targetDir = subPackageMeta ? path4.join(ctx.srcRoot, subPackageMeta.subPackage.root) : ctx.srcRoot;
|
|
914
|
-
const assetGlobs = [
|
|
915
|
-
// 支持 html
|
|
916
|
-
"**/*.{wxml,html,wxs}",
|
|
917
|
-
"**/*.{png,jpg,jpeg,gif,svg,webp}"
|
|
918
|
-
];
|
|
919
|
-
assetGlobs.push(...resolveGlobs(weapp?.copy?.include));
|
|
920
|
-
const patterns = assetGlobs.map(
|
|
921
|
-
(x) => {
|
|
922
|
-
return path4.join(
|
|
923
|
-
targetDir,
|
|
924
|
-
x
|
|
925
|
-
);
|
|
926
|
-
}
|
|
927
|
-
);
|
|
928
|
-
const relFiles = await new Fdir().withRelativePaths().globWithOptions(
|
|
929
|
-
patterns,
|
|
930
|
-
{
|
|
931
|
-
cwd: ctx.cwd,
|
|
932
|
-
ignore,
|
|
933
|
-
windows: true,
|
|
934
|
-
posixSlashes: true
|
|
935
|
-
}
|
|
936
|
-
).crawl(ctx.cwd).withPromise();
|
|
937
|
-
const wxmlFiles = [];
|
|
938
|
-
const wxsFiles = [];
|
|
939
|
-
const mediaFiles = [];
|
|
940
|
-
for (const relPath of relFiles) {
|
|
941
|
-
const absPath = path4.resolve(ctx.cwd, relPath);
|
|
942
|
-
cachedWatchFiles.push(absPath);
|
|
943
|
-
const isWxs = /\.wxs$/.test(relPath);
|
|
944
|
-
const fileName = ctx.relativeSrcRoot(relPath);
|
|
945
|
-
if (isTemplateRequest(relPath)) {
|
|
946
|
-
if (weapp?.enhance?.autoImportComponents && ctx.autoImportFilter(relPath, subPackageMeta)) {
|
|
947
|
-
await ctx.scanPotentialComponentEntries(absPath);
|
|
948
|
-
}
|
|
949
|
-
wxmlFiles.push({
|
|
950
|
-
relPath,
|
|
951
|
-
absPath,
|
|
952
|
-
fileName
|
|
953
|
-
});
|
|
954
|
-
} else if (isWxs) {
|
|
955
|
-
wxsFiles.push({
|
|
956
|
-
relPath,
|
|
957
|
-
absPath,
|
|
958
|
-
fileName
|
|
959
|
-
});
|
|
960
|
-
} else {
|
|
961
|
-
mediaFiles.push({
|
|
962
|
-
relPath,
|
|
963
|
-
absPath,
|
|
964
|
-
fileName
|
|
965
|
-
});
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
await Promise.all([
|
|
969
|
-
...wxmlFiles.map(async ({ fileName, absPath }) => {
|
|
970
|
-
const source = await fs3.readFile(absPath, "utf8");
|
|
971
|
-
let _source;
|
|
972
|
-
if (weapp?.enhance?.wxml) {
|
|
973
|
-
const { code, deps, components: components2 } = processWxml(source, weapp.enhance.wxml === true ? {} : weapp.enhance.wxml);
|
|
974
|
-
_source = code;
|
|
975
|
-
for (const wxsDep of deps.filter((x) => x.tagName === "wxs")) {
|
|
976
|
-
if (jsExtensions.includes(wxsDep.attrs.lang) || /\.wxs\.[jt]s$/.test(wxsDep.value)) {
|
|
977
|
-
const wxsPath = path4.resolve(path4.dirname(absPath), wxsDep.value);
|
|
978
|
-
if (await fs3.exists(wxsPath)) {
|
|
979
|
-
cachedWatchFiles.push(wxsPath);
|
|
980
|
-
const code2 = await fs3.readFile(wxsPath, "utf8");
|
|
981
|
-
const res = transformWxsCode(code2, {
|
|
982
|
-
filename: wxsPath
|
|
983
|
-
});
|
|
984
|
-
if (res && res.code) {
|
|
985
|
-
cachedEmittedFiles.push(
|
|
986
|
-
{
|
|
987
|
-
type: "asset",
|
|
988
|
-
fileName: ctx.relativeSrcRoot(ctx.relativeCwd(removeExtension(wxsPath))),
|
|
989
|
-
source: res.code
|
|
990
|
-
}
|
|
991
|
-
);
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
debug?.(components2);
|
|
997
|
-
if (!isEmptyObject(components2)) {
|
|
998
|
-
ctx.wxmlComponentsMap.set(removeExtension(absPath), components2);
|
|
999
|
-
}
|
|
1000
|
-
} else {
|
|
1001
|
-
_source = source;
|
|
1002
|
-
}
|
|
1003
|
-
cachedEmittedFiles.push({
|
|
1004
|
-
type: "asset",
|
|
1005
|
-
fileName,
|
|
1006
|
-
source: _source
|
|
1007
|
-
});
|
|
1008
|
-
}),
|
|
1009
|
-
...wxsFiles.map(async ({ fileName, absPath }) => {
|
|
1010
|
-
const source = await fs3.readFile(absPath);
|
|
1011
|
-
cachedEmittedFiles.push({
|
|
1012
|
-
type: "asset",
|
|
1013
|
-
fileName,
|
|
1014
|
-
source
|
|
1015
|
-
});
|
|
1016
|
-
}),
|
|
1017
|
-
...mediaFiles.map(async ({ fileName, absPath }) => {
|
|
1018
|
-
const source = await fs3.readFile(absPath);
|
|
1019
|
-
cachedEmittedFiles.push({
|
|
1020
|
-
type: "asset",
|
|
1021
|
-
fileName,
|
|
1022
|
-
source
|
|
1023
|
-
});
|
|
1024
|
-
})
|
|
1025
|
-
]);
|
|
1026
|
-
debug?.(ctx.potentialComponentMap);
|
|
1027
|
-
if (subPackageMeta) {
|
|
1028
|
-
entriesSet = subPackageMeta.entriesSet;
|
|
1029
|
-
entries = subPackageMeta.entries;
|
|
1030
|
-
} else {
|
|
1031
|
-
await ctx.scanAppEntry();
|
|
1032
|
-
entriesSet = ctx.entriesSet;
|
|
1033
|
-
entries = ctx.entries;
|
|
1034
|
-
}
|
|
1035
|
-
const input = getInputOption([...entriesSet]);
|
|
1036
|
-
options.input = input;
|
|
1037
|
-
},
|
|
1038
|
-
buildStart() {
|
|
1039
|
-
for (const filePath of cachedWatchFiles) {
|
|
1040
|
-
this.addWatchFile(filePath);
|
|
1041
|
-
}
|
|
1042
|
-
for (const emitFile of cachedEmittedFiles) {
|
|
1043
|
-
this.emitFile(emitFile);
|
|
1044
|
-
}
|
|
1045
|
-
},
|
|
1046
|
-
async buildEnd() {
|
|
1047
|
-
for (const entry of entriesSet) {
|
|
1048
|
-
const moduleInfo = this.getModuleInfo(entry);
|
|
1049
|
-
if (moduleInfo) {
|
|
1050
|
-
const stack = [moduleInfo.id];
|
|
1051
|
-
const visitedModules = /* @__PURE__ */ new Set();
|
|
1052
|
-
while (stack.length > 0) {
|
|
1053
|
-
const id = stack.pop();
|
|
1054
|
-
if (id && !visitedModules.has(id)) {
|
|
1055
|
-
visitedModules.add(id);
|
|
1056
|
-
const info = this.getModuleInfo(id);
|
|
1057
|
-
if (info) {
|
|
1058
|
-
this.addWatchFile(info.id);
|
|
1059
|
-
stack.push(...info.importedIds);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
debug?.("buildEnd start");
|
|
1066
|
-
const watchFiles = this.getWatchFiles();
|
|
1067
|
-
debug?.("watchFiles count: ", watchFiles.length);
|
|
1068
|
-
for (const entry of entries) {
|
|
1069
|
-
if (entry.jsonPath) {
|
|
1070
|
-
this.addWatchFile(entry.jsonPath);
|
|
1071
|
-
if (entry.json) {
|
|
1072
|
-
const fileName = jsonFileRemoveJsExtension(ctx.relativeSrcRoot(ctx.relativeCwd(entry.jsonPath)));
|
|
1073
|
-
this.emitFile({
|
|
1074
|
-
type: "asset",
|
|
1075
|
-
fileName,
|
|
1076
|
-
source: resolveJson(entry, ctx.aliasEntries)
|
|
1077
|
-
});
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
if (entry.type === "app") {
|
|
1081
|
-
const appEntry = ctx.appEntry;
|
|
1082
|
-
if (appEntry) {
|
|
1083
|
-
if (appEntry.sitemapJsonPath) {
|
|
1084
|
-
this.addWatchFile(appEntry.sitemapJsonPath);
|
|
1085
|
-
if (appEntry.sitemapJson) {
|
|
1086
|
-
const fileName = jsonFileRemoveJsExtension(
|
|
1087
|
-
ctx.relativeSrcRoot(ctx.relativeCwd(appEntry.sitemapJsonPath))
|
|
1088
|
-
);
|
|
1089
|
-
this.emitFile({
|
|
1090
|
-
type: "asset",
|
|
1091
|
-
fileName,
|
|
1092
|
-
source: resolveJson({
|
|
1093
|
-
json: appEntry.sitemapJson,
|
|
1094
|
-
jsonPath: appEntry.sitemapJsonPath
|
|
1095
|
-
}, ctx.aliasEntries)
|
|
1096
|
-
});
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
if (appEntry.themeJsonPath) {
|
|
1100
|
-
this.addWatchFile(appEntry.themeJsonPath);
|
|
1101
|
-
if (appEntry.themeJson) {
|
|
1102
|
-
const fileName = jsonFileRemoveJsExtension(
|
|
1103
|
-
ctx.relativeSrcRoot(ctx.relativeCwd(appEntry.themeJsonPath))
|
|
1104
|
-
);
|
|
1105
|
-
this.emitFile({
|
|
1106
|
-
type: "asset",
|
|
1107
|
-
fileName,
|
|
1108
|
-
source: resolveJson({
|
|
1109
|
-
json: appEntry.themeJson,
|
|
1110
|
-
jsonPath: appEntry.themeJsonPath
|
|
1111
|
-
}, ctx.aliasEntries)
|
|
1112
|
-
});
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
debug?.("buildEnd end");
|
|
1119
|
-
},
|
|
1120
|
-
resolveId(source) {
|
|
1121
|
-
if (/\.wxss$/.test(source)) {
|
|
1122
|
-
return source.replace(/\.wxss$/, ".css?wxss");
|
|
1123
|
-
}
|
|
1124
|
-
},
|
|
1125
|
-
async load(id) {
|
|
1126
|
-
if (entriesSet.has(id)) {
|
|
1127
|
-
const code = await fs3.readFile(id, "utf8");
|
|
1128
|
-
const ms = new MagicString2(code);
|
|
1129
|
-
for (const ext of supportedCssLangs) {
|
|
1130
|
-
const mayBeCssPath = changeFileExtension(id, ext);
|
|
1131
|
-
if (await fs3.exists(mayBeCssPath)) {
|
|
1132
|
-
this.addWatchFile(mayBeCssPath);
|
|
1133
|
-
ms.prepend(`import '${mayBeCssPath}'
|
|
1134
|
-
`);
|
|
1135
|
-
}
|
|
1136
|
-
}
|
|
1137
|
-
return {
|
|
1138
|
-
code: ms.toString()
|
|
1139
|
-
};
|
|
1140
|
-
} else if (isCSSRequest(id)) {
|
|
1141
|
-
const parsed = parseRequest(id);
|
|
1142
|
-
const realPath = getCssRealPath(parsed);
|
|
1143
|
-
if (await fs3.exists(realPath)) {
|
|
1144
|
-
const css = await fs3.readFile(realPath, "utf8");
|
|
1145
|
-
return {
|
|
1146
|
-
code: css
|
|
1147
|
-
};
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
},
|
|
1151
|
-
// for debug
|
|
1152
|
-
watchChange(id, change) {
|
|
1153
|
-
debouncedLoggerSuccess(`[${change.event}] ${ctx.relativeCwd(id)}`);
|
|
1154
|
-
},
|
|
1155
|
-
async generateBundle(_options, bundle) {
|
|
1156
|
-
debug?.("generateBundle start");
|
|
1157
|
-
const bundleKeys = Object.keys(bundle);
|
|
1158
|
-
await Promise.all(
|
|
1159
|
-
bundleKeys.map(async (bundleKey) => {
|
|
1160
|
-
const asset = bundle[bundleKey];
|
|
1161
|
-
if (asset.type === "asset") {
|
|
1162
|
-
if (bundleKey.endsWith(".css")) {
|
|
1163
|
-
await Promise.all(asset.originalFileNames.map(async (originalFileName) => {
|
|
1164
|
-
if (isJsOrTs(originalFileName)) {
|
|
1165
|
-
const newFileName = ctx.relativeSrcRoot(
|
|
1166
|
-
changeFileExtension(originalFileName, ctx.outputExtensions.wxss)
|
|
1167
|
-
);
|
|
1168
|
-
const css = await cssPostProcess(
|
|
1169
|
-
asset.source.toString(),
|
|
1170
|
-
{ platform: ctx.platform }
|
|
1171
|
-
);
|
|
1172
|
-
this.emitFile({
|
|
1173
|
-
type: "asset",
|
|
1174
|
-
fileName: newFileName,
|
|
1175
|
-
source: css
|
|
1176
|
-
});
|
|
1177
|
-
}
|
|
1178
|
-
}));
|
|
1179
|
-
delete bundle[bundleKey];
|
|
1180
|
-
} else if (isTemplateRequest(bundleKey)) {
|
|
1181
|
-
const newFileName = changeFileExtension(bundleKey, ctx.outputExtensions.wxml);
|
|
1182
|
-
if (newFileName !== bundleKey) {
|
|
1183
|
-
delete bundle[bundleKey];
|
|
1184
|
-
this.emitFile({
|
|
1185
|
-
type: "asset",
|
|
1186
|
-
fileName: newFileName,
|
|
1187
|
-
source: asset.source
|
|
1188
|
-
});
|
|
1189
|
-
}
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
})
|
|
1193
|
-
);
|
|
1194
|
-
debug?.("generateBundle end");
|
|
1195
|
-
}
|
|
1196
|
-
},
|
|
1197
|
-
{
|
|
1198
|
-
// todo
|
|
1199
|
-
name: "weapp-vite"
|
|
1200
|
-
// https://github.com/vitejs/vite/blob/3400a5e258a597499c0f0808c8fca4d92eeabc17/packages/vite/src/node/plugins/css.ts#L6
|
|
1201
|
-
},
|
|
1202
|
-
{
|
|
1203
|
-
// todo
|
|
1204
|
-
name: "weapp-vite:post",
|
|
1205
|
-
enforce: "post"
|
|
1206
|
-
}
|
|
1207
|
-
];
|
|
1208
|
-
}
|
|
1209
|
-
|
|
1210
|
-
// src/context/methods/index.ts
|
|
1211
|
-
init_esm_shims();
|
|
1212
|
-
|
|
1213
|
-
// src/context/methods/buildNpm.ts
|
|
1214
|
-
init_esm_shims();
|
|
1215
|
-
import { defu as defu3, isObject as isObject3 } from "@weapp-core/shared";
|
|
1216
|
-
import fs4 from "fs-extra";
|
|
1217
|
-
import { getPackageInfo, resolveModule } from "local-pkg";
|
|
1218
|
-
import path5 from "pathe";
|
|
1219
|
-
|
|
1220
|
-
// src/context/shared.ts
|
|
1221
|
-
init_esm_shims();
|
|
1222
|
-
var debug2 = createDebugger("weapp-vite:context");
|
|
1223
|
-
var once = false;
|
|
1224
|
-
function logBuildIndependentSubPackageFinish(root) {
|
|
1225
|
-
if (!once) {
|
|
1226
|
-
logger_default.success(`\u72EC\u7ACB\u5206\u5305 ${root} \u6784\u5EFA\u5B8C\u6210\uFF01`);
|
|
1227
|
-
once = true;
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
|
|
1231
|
-
// src/context/methods/buildNpm.ts
|
|
1232
|
-
async function buildNpm(subPackage, options) {
|
|
1233
|
-
debug2?.("buildNpm start");
|
|
1234
|
-
const { build: tsupBuild } = await import("tsup");
|
|
1235
|
-
const isDependenciesCacheOutdate = await this.checkDependenciesCacheOutdate();
|
|
1236
|
-
let packNpmRelationList = [];
|
|
1237
|
-
if (this.projectConfig.setting?.packNpmManually && Array.isArray(this.projectConfig.setting.packNpmRelationList)) {
|
|
1238
|
-
packNpmRelationList = this.projectConfig.setting.packNpmRelationList;
|
|
1239
|
-
} else {
|
|
1240
|
-
packNpmRelationList = [
|
|
1241
|
-
{
|
|
1242
|
-
miniprogramNpmDistDir: ".",
|
|
1243
|
-
packageJsonPath: "./package.json"
|
|
1244
|
-
}
|
|
1245
|
-
];
|
|
1246
|
-
}
|
|
1247
|
-
const heading = subPackage?.root ? `\u5206\u5305[${subPackage.root}]:` : "";
|
|
1248
|
-
for (const relation of packNpmRelationList) {
|
|
1249
|
-
const packageJsonPath = path5.resolve(this.cwd, relation.packageJsonPath);
|
|
1250
|
-
if (await fs4.exists(packageJsonPath)) {
|
|
1251
|
-
const pkgJson = await fs4.readJson(packageJsonPath);
|
|
1252
|
-
const outDir = path5.resolve(this.cwd, relation.miniprogramNpmDistDir, subPackage?.root ?? "", "miniprogram_npm");
|
|
1253
|
-
if (pkgJson.dependencies) {
|
|
1254
|
-
const dependencies = Object.keys(pkgJson.dependencies);
|
|
1255
|
-
if (dependencies.length > 0) {
|
|
1256
|
-
for (const dep of dependencies) {
|
|
1257
|
-
if (Array.isArray(subPackage?.dependencies)) {
|
|
1258
|
-
if (!regExpTest(subPackage.dependencies, dep)) {
|
|
1259
|
-
continue;
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1262
|
-
const packageInfo = await getPackageInfo(dep);
|
|
1263
|
-
if (!packageInfo) {
|
|
1264
|
-
continue;
|
|
1265
|
-
}
|
|
1266
|
-
const { packageJson: targetJson, rootPath } = packageInfo;
|
|
1267
|
-
if (Reflect.has(targetJson, "miniprogram") && targetJson.miniprogram) {
|
|
1268
|
-
const destOutDir = path5.join(outDir, dep);
|
|
1269
|
-
if (!isDependenciesCacheOutdate && await fs4.exists(destOutDir)) {
|
|
1270
|
-
logger_default.info(`${heading} ${dep} \u4F9D\u8D56\u672A\u53D1\u751F\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5904\u7406!`);
|
|
1271
|
-
continue;
|
|
1272
|
-
}
|
|
1273
|
-
await fs4.copy(
|
|
1274
|
-
path5.resolve(
|
|
1275
|
-
rootPath,
|
|
1276
|
-
targetJson.miniprogram
|
|
1277
|
-
),
|
|
1278
|
-
destOutDir
|
|
1279
|
-
);
|
|
1280
|
-
} else {
|
|
1281
|
-
const destOutDir = path5.join(outDir, dep);
|
|
1282
|
-
if (!isDependenciesCacheOutdate && await fs4.exists(destOutDir)) {
|
|
1283
|
-
logger_default.info(`${heading} ${dep} \u4F9D\u8D56\u672A\u53D1\u751F\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5904\u7406!`);
|
|
1284
|
-
continue;
|
|
1285
|
-
}
|
|
1286
|
-
const index = resolveModule(dep);
|
|
1287
|
-
if (!index) {
|
|
1288
|
-
continue;
|
|
1289
|
-
}
|
|
1290
|
-
const mergedOptions = defu3(options, {
|
|
1291
|
-
entry: {
|
|
1292
|
-
index
|
|
1293
|
-
},
|
|
1294
|
-
format: ["cjs"],
|
|
1295
|
-
outDir: destOutDir,
|
|
1296
|
-
silent: true,
|
|
1297
|
-
shims: true,
|
|
1298
|
-
outExtension: () => {
|
|
1299
|
-
return {
|
|
1300
|
-
js: ".js"
|
|
1301
|
-
};
|
|
1302
|
-
},
|
|
1303
|
-
sourcemap: false,
|
|
1304
|
-
config: false,
|
|
1305
|
-
// https://tsup.egoist.dev/#compile-time-environment-variables
|
|
1306
|
-
env: {
|
|
1307
|
-
NODE_ENV: "production"
|
|
1308
|
-
}
|
|
1309
|
-
// external: [],
|
|
1310
|
-
// clean: false,
|
|
1311
|
-
});
|
|
1312
|
-
const resolvedOptions = this.inlineConfig.weapp?.npm?.tsup?.(mergedOptions, { entry: index, name: dep });
|
|
1313
|
-
let finalOptions;
|
|
1314
|
-
if (resolvedOptions === void 0) {
|
|
1315
|
-
finalOptions = mergedOptions;
|
|
1316
|
-
} else if (isObject3(resolvedOptions)) {
|
|
1317
|
-
finalOptions = resolvedOptions;
|
|
1318
|
-
}
|
|
1319
|
-
finalOptions && await tsupBuild(finalOptions);
|
|
1320
|
-
}
|
|
1321
|
-
logger_default.success(`${heading} ${dep} \u4F9D\u8D56\u5904\u7406\u5B8C\u6210!`);
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
await this.writeDependenciesCache();
|
|
1328
|
-
debug2?.("buildNpm end");
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
// src/context/methods/buildSubPackage.ts
|
|
1332
|
-
init_esm_shims();
|
|
1333
|
-
import { build } from "vite";
|
|
1334
|
-
async function buildSubPackage() {
|
|
1335
|
-
debug2?.("buildSubPackage start");
|
|
1336
|
-
for (const [root, meta] of Object.entries(this.subPackageMeta)) {
|
|
1337
|
-
const inlineConfig = this.getConfig(meta, {
|
|
1338
|
-
build: {
|
|
1339
|
-
rollupOptions: {
|
|
1340
|
-
output: {
|
|
1341
|
-
chunkFileNames() {
|
|
1342
|
-
return `${root}/[name]-[hash].js`;
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
|
-
});
|
|
1348
|
-
await this.buildNpm(meta.subPackage);
|
|
1349
|
-
const output = await build(
|
|
1350
|
-
inlineConfig
|
|
1351
|
-
);
|
|
1352
|
-
if (this.isDev) {
|
|
1353
|
-
const watcher = output;
|
|
1354
|
-
this.setRollupWatcher(watcher, root);
|
|
1355
|
-
await new Promise((resolve, reject) => {
|
|
1356
|
-
watcher.on("event", (e) => {
|
|
1357
|
-
if (e.code === "END") {
|
|
1358
|
-
logBuildIndependentSubPackageFinish(root);
|
|
1359
|
-
resolve(e);
|
|
1360
|
-
} else if (e.code === "ERROR") {
|
|
1361
|
-
reject(e);
|
|
1362
|
-
}
|
|
1363
|
-
});
|
|
1364
|
-
});
|
|
1365
|
-
} else {
|
|
1366
|
-
logBuildIndependentSubPackageFinish(root);
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
debug2?.("buildSubPackage end");
|
|
1370
|
-
}
|
|
1371
|
-
|
|
1372
|
-
// src/context/methods/loadDefaultConfig.ts
|
|
1373
|
-
init_esm_shims();
|
|
1374
|
-
import { addExtension as addExtension2, defu as defu4, removeExtension as removeExtension2 } from "@weapp-core/shared";
|
|
1375
|
-
import fs5 from "fs-extra";
|
|
1376
|
-
import path6 from "pathe";
|
|
1377
|
-
import { loadConfigFromFile } from "vite";
|
|
1378
|
-
import tsconfigPaths from "vite-tsconfig-paths";
|
|
1379
|
-
async function loadDefaultConfig() {
|
|
1380
|
-
const projectConfig = await getProjectConfig(this.cwd);
|
|
1381
|
-
this.projectConfig = projectConfig;
|
|
1382
|
-
if (!this.mpDistRoot) {
|
|
1383
|
-
logger_default.error("\u8BF7\u5728 `project.config.json` \u91CC\u8BBE\u7F6E `miniprogramRoot`, \u6BD4\u5982\u53EF\u4EE5\u8BBE\u7F6E\u4E3A `dist/` ");
|
|
1384
|
-
return;
|
|
1385
|
-
}
|
|
1386
|
-
const packageJsonPath = path6.resolve(this.cwd, "package.json");
|
|
1387
|
-
const external = [];
|
|
1388
|
-
if (await fs5.exists(packageJsonPath)) {
|
|
1389
|
-
const localPackageJson = await fs5.readJson(packageJsonPath, {
|
|
1390
|
-
throws: false
|
|
1391
|
-
}) || {};
|
|
1392
|
-
this.packageJson = localPackageJson;
|
|
1393
|
-
if (localPackageJson.dependencies) {
|
|
1394
|
-
external.push(...Object.keys(localPackageJson.dependencies));
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
const loaded = await loadConfigFromFile({
|
|
1398
|
-
command: this.isDev ? "serve" : "build",
|
|
1399
|
-
mode: this.mode
|
|
1400
|
-
}, void 0, this.cwd);
|
|
1401
|
-
this.inlineConfig = defu4({
|
|
1402
|
-
configFile: false
|
|
1403
|
-
}, loaded?.config, {
|
|
1404
|
-
build: {
|
|
1405
|
-
rollupOptions: {
|
|
1406
|
-
output: {
|
|
1407
|
-
format: "cjs",
|
|
1408
|
-
strict: false,
|
|
1409
|
-
entryFileNames: (chunkInfo) => {
|
|
1410
|
-
const name = this.relativeSrcRoot(chunkInfo.name);
|
|
1411
|
-
if (name.endsWith(".ts")) {
|
|
1412
|
-
const baseFileName = removeExtension2(name);
|
|
1413
|
-
if (baseFileName.endsWith(".wxs")) {
|
|
1414
|
-
return baseFileName;
|
|
1415
|
-
}
|
|
1416
|
-
return addExtension2(baseFileName, ".js");
|
|
1417
|
-
}
|
|
1418
|
-
return name;
|
|
1419
|
-
}
|
|
1420
|
-
},
|
|
1421
|
-
external
|
|
1422
|
-
},
|
|
1423
|
-
assetsDir: ".",
|
|
1424
|
-
commonjsOptions: {
|
|
1425
|
-
transformMixedEsModules: true,
|
|
1426
|
-
include: void 0
|
|
1427
|
-
// esmExternals: true,
|
|
1428
|
-
}
|
|
1429
|
-
},
|
|
1430
|
-
logLevel: "warn",
|
|
1431
|
-
weapp: getWeappViteConfig()
|
|
1432
|
-
});
|
|
1433
|
-
const platform = this.inlineConfig.weapp?.platform;
|
|
1434
|
-
this.platform = platform;
|
|
1435
|
-
this.outputExtensions = getOutputExtensions(platform);
|
|
1436
|
-
this.inlineConfig.plugins ??= [];
|
|
1437
|
-
this.inlineConfig.plugins?.push(tsconfigPaths(this.inlineConfig.weapp?.tsconfigPaths));
|
|
1438
|
-
this.aliasEntries = getAliasEntries(this.inlineConfig.weapp?.jsonAlias);
|
|
1439
|
-
}
|
|
1440
|
-
|
|
1441
|
-
// src/context/methods/readCommentJson.ts
|
|
1442
|
-
init_esm_shims();
|
|
1443
|
-
import { bundleRequire } from "bundle-require";
|
|
1444
|
-
import { parse as parseJson2 } from "comment-json";
|
|
1445
|
-
import fs6 from "fs-extra";
|
|
1446
|
-
function parseCommentJson(json) {
|
|
1447
|
-
return parseJson2(json, void 0, true);
|
|
1448
|
-
}
|
|
1449
|
-
async function readCommentJson(filepath) {
|
|
1450
|
-
try {
|
|
1451
|
-
if (/\.json\.[jt]s$/.test(filepath)) {
|
|
1452
|
-
const { mod } = await bundleRequire({
|
|
1453
|
-
filepath,
|
|
1454
|
-
cwd: this.cwd,
|
|
1455
|
-
esbuildOptions: {
|
|
1456
|
-
define: this.defineImportMetaEnv
|
|
1457
|
-
}
|
|
1458
|
-
});
|
|
1459
|
-
return typeof mod.default === "function" ? await mod.default(this) : mod.default;
|
|
1460
|
-
} else {
|
|
1461
|
-
return parseCommentJson(await fs6.readFile(filepath, "utf8"));
|
|
1462
|
-
}
|
|
1463
|
-
} catch (error) {
|
|
1464
|
-
logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
|
|
1465
|
-
logger_default.error(error);
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
|
|
1469
|
-
// src/context/mixins/index.ts
|
|
1470
|
-
init_esm_shims();
|
|
1471
|
-
|
|
1472
|
-
// src/context/mixins/dependenciesCache.ts
|
|
1473
|
-
init_esm_shims();
|
|
1474
|
-
import { isObject as isObject4, objectHash } from "@weapp-core/shared";
|
|
1475
|
-
import fs7 from "fs-extra";
|
|
1476
|
-
import path7 from "pathe";
|
|
1477
|
-
function dependenciesCache(proto) {
|
|
1478
|
-
Object.defineProperty(proto, "dependenciesCacheFilePath", {
|
|
1479
|
-
get() {
|
|
1480
|
-
return path7.resolve(this.cwd, "node_modules/weapp-vite/.cache/npm.json");
|
|
1481
|
-
}
|
|
1482
|
-
});
|
|
1483
|
-
Object.defineProperty(proto, "dependenciesCacheHash", {
|
|
1484
|
-
get() {
|
|
1485
|
-
return objectHash(this.packageJson.dependencies ?? {});
|
|
1486
|
-
}
|
|
1487
|
-
});
|
|
1488
|
-
proto.writeDependenciesCache = function writeDependenciesCache() {
|
|
1489
|
-
return fs7.outputJSON(this.dependenciesCacheFilePath, {
|
|
1490
|
-
"/": this.dependenciesCacheHash
|
|
1491
|
-
});
|
|
1492
|
-
};
|
|
1493
|
-
proto.readDependenciesCache = async function readDependenciesCache() {
|
|
1494
|
-
if (await fs7.exists(this.dependenciesCacheFilePath)) {
|
|
1495
|
-
return await fs7.readJson(this.dependenciesCacheFilePath, { throws: false });
|
|
1496
|
-
}
|
|
1497
|
-
};
|
|
1498
|
-
proto.checkDependenciesCacheOutdate = async function checkDependenciesCacheOutdate() {
|
|
1499
|
-
const json = await this.readDependenciesCache();
|
|
1500
|
-
if (isObject4(json)) {
|
|
1501
|
-
return this.dependenciesCacheHash !== json["/"];
|
|
1502
|
-
}
|
|
1503
|
-
return true;
|
|
1504
|
-
};
|
|
1505
|
-
}
|
|
1506
|
-
|
|
1507
|
-
// src/context/CompilerContext.ts
|
|
1508
|
-
var CompilerContext = class {
|
|
1509
|
-
/**
|
|
1510
|
-
* loadDefaultConfig 的时候会被重新赋予
|
|
1511
|
-
*/
|
|
1512
|
-
inlineConfig;
|
|
1513
|
-
cwd;
|
|
1514
|
-
isDev;
|
|
1515
|
-
projectConfig;
|
|
1516
|
-
mode;
|
|
1517
|
-
packageJson;
|
|
1518
|
-
rollupWatcherMap;
|
|
1519
|
-
entriesSet;
|
|
1520
|
-
entries;
|
|
1521
|
-
// for auto import
|
|
1522
|
-
potentialComponentMap;
|
|
1523
|
-
appEntry;
|
|
1524
|
-
subPackageMeta;
|
|
1525
|
-
aliasEntries;
|
|
1526
|
-
platform;
|
|
1527
|
-
outputExtensions;
|
|
1528
|
-
/**
|
|
1529
|
-
* esbuild 定义的环境变量
|
|
1530
|
-
*/
|
|
1531
|
-
defineEnv;
|
|
1532
|
-
wxmlComponentsMap;
|
|
1533
|
-
/**
|
|
1534
|
-
* CompilerContext 类的构造函数,用于初始化编译上下文。
|
|
1535
|
-
* @param options - 可选的编译上下文选项,包括当前工作目录、是否为开发环境、内联配置、项目配置、模式、package.json 和平台等信息。
|
|
1536
|
-
* 如果未提供选项,则使用默认值进行初始化。
|
|
1537
|
-
* @property cwd - 当前工作目录。
|
|
1538
|
-
* @property inlineConfig - 内联配置。
|
|
1539
|
-
* @property isDev - 是否为开发环境。
|
|
1540
|
-
* @property projectConfig - 项目配置。
|
|
1541
|
-
* @property mode - 编译模式。
|
|
1542
|
-
* @property packageJson - package.json 文件内容。
|
|
1543
|
-
* @property rollupWatcherMap - Rollup 监视器映射。
|
|
1544
|
-
* @property subPackageMeta - 子包元数据。
|
|
1545
|
-
* @property entriesSet - 入口文件集合。
|
|
1546
|
-
* @property entries - 入口文件数组。
|
|
1547
|
-
* @property potentialComponentMap - 潜在组件映射。
|
|
1548
|
-
* @property aliasEntries - 别名入口数组。
|
|
1549
|
-
* @property platform - 平台类型。
|
|
1550
|
-
* @property outputExtensions - 输出文件扩展名。
|
|
1551
|
-
* @property defineEnv - 定义的环境变量。
|
|
1552
|
-
* @property wxmlComponentsMap - WXML 组件映射。
|
|
1553
|
-
*/
|
|
1554
|
-
constructor(options) {
|
|
1555
|
-
const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, platform } = defu5(options, {
|
|
1556
|
-
cwd: process.cwd(),
|
|
1557
|
-
isDev: false,
|
|
1558
|
-
projectConfig: {},
|
|
1559
|
-
inlineConfig: {},
|
|
1560
|
-
packageJson: {},
|
|
1561
|
-
platform: "weapp"
|
|
1562
|
-
});
|
|
1563
|
-
this.cwd = cwd;
|
|
1564
|
-
this.inlineConfig = inlineConfig;
|
|
1565
|
-
this.isDev = isDev;
|
|
1566
|
-
this.projectConfig = projectConfig;
|
|
1567
|
-
this.mode = mode;
|
|
1568
|
-
this.packageJson = packageJson;
|
|
1569
|
-
this.rollupWatcherMap = /* @__PURE__ */ new Map();
|
|
1570
|
-
this.subPackageMeta = {};
|
|
1571
|
-
this.entriesSet = /* @__PURE__ */ new Set();
|
|
1572
|
-
this.entries = [];
|
|
1573
|
-
this.potentialComponentMap = /* @__PURE__ */ new Map();
|
|
1574
|
-
this.aliasEntries = [];
|
|
1575
|
-
this.platform = platform;
|
|
1576
|
-
this.outputExtensions = getOutputExtensions(platform);
|
|
1577
|
-
this.defineEnv = {};
|
|
1578
|
-
this.wxmlComponentsMap = /* @__PURE__ */ new Map();
|
|
1579
|
-
}
|
|
1580
|
-
// https://github.com/vitejs/vite/blob/192d555f88bba7576e8a40cc027e8a11e006079c/packages/vite/src/node/plugins/define.ts#L41
|
|
1581
|
-
/**
|
|
1582
|
-
* 插件真正计算出来的 define options
|
|
1583
|
-
*/
|
|
1584
|
-
/**
|
|
1585
|
-
* 获取编译上下文中的环境变量定义,用于在小程序环境中暴露全局变量。
|
|
1586
|
-
* 该函数将当前平台、用户自定义的环境变量合并,并将其转换为 import.meta.env 对象的属性。
|
|
1587
|
-
* @returns {Record<string, any>} 包含所有环境变量的对象,键为 import.meta.env 下的属性名,值为对应的环境变量值。
|
|
1588
|
-
*/
|
|
1589
|
-
get defineImportMetaEnv() {
|
|
1590
|
-
const env = {
|
|
1591
|
-
MP_PLATFORM: this.platform,
|
|
1592
|
-
...this.defineEnv
|
|
1593
|
-
};
|
|
1594
|
-
const define = {};
|
|
1595
|
-
for (const [key, value] of Object.entries(env)) {
|
|
1596
|
-
define[`import.meta.env.${key}`] = JSON.stringify(value);
|
|
1597
|
-
}
|
|
1598
|
-
define[`import.meta.env`] = JSON.stringify(env);
|
|
1599
|
-
return define;
|
|
1600
|
-
}
|
|
1601
|
-
setDefineEnv(key, value) {
|
|
1602
|
-
this.defineEnv[key] = value;
|
|
1603
|
-
}
|
|
1604
|
-
get srcRoot() {
|
|
1605
|
-
return this.inlineConfig?.weapp?.srcRoot ?? "";
|
|
1606
|
-
}
|
|
1607
|
-
relativeCwd(p) {
|
|
1608
|
-
return path8.relative(this.cwd, p);
|
|
1609
|
-
}
|
|
1610
|
-
relativeSrcRoot(p) {
|
|
1611
|
-
if (this.srcRoot) {
|
|
1612
|
-
return path8.relative(this.srcRoot, p);
|
|
1613
|
-
}
|
|
1614
|
-
return p;
|
|
1615
|
-
}
|
|
1616
|
-
/**
|
|
1617
|
-
* @description 写在 projectConfig 里面的 miniprogramRoot / srcMiniprogramRoot
|
|
1618
|
-
* 默认为 'dist'
|
|
1619
|
-
*
|
|
1620
|
-
*/
|
|
1621
|
-
get mpDistRoot() {
|
|
1622
|
-
return this.projectConfig.miniprogramRoot || this.projectConfig.srcMiniprogramRoot;
|
|
1623
|
-
}
|
|
1624
|
-
get outDir() {
|
|
1625
|
-
return path8.resolve(this.cwd, this.mpDistRoot ?? "");
|
|
1626
|
-
}
|
|
1627
|
-
async runDev() {
|
|
1628
|
-
if (process.env.NODE_ENV === void 0) {
|
|
1629
|
-
process.env.NODE_ENV = "development";
|
|
1630
|
-
}
|
|
1631
|
-
debug2?.("dev build watcher start");
|
|
1632
|
-
const watcher = await build2(
|
|
1633
|
-
this.getConfig()
|
|
1634
|
-
);
|
|
1635
|
-
debug2?.("dev build watcher end");
|
|
1636
|
-
debug2?.("dev watcher listen start");
|
|
1637
|
-
await new Promise((resolve, reject) => {
|
|
1638
|
-
watcher.on("event", async (e) => {
|
|
1639
|
-
if (e.code === "END") {
|
|
1640
|
-
debug2?.("dev watcher listen end");
|
|
1641
|
-
await this.buildSubPackage();
|
|
1642
|
-
resolve(e);
|
|
1643
|
-
} else if (e.code === "ERROR") {
|
|
1644
|
-
reject(e);
|
|
1645
|
-
}
|
|
1646
|
-
});
|
|
1647
|
-
});
|
|
1648
|
-
this.setRollupWatcher(watcher);
|
|
1649
|
-
return watcher;
|
|
1650
|
-
}
|
|
1651
|
-
getConfig(subPackageMeta, ...configs) {
|
|
1652
|
-
if (this.isDev) {
|
|
1653
|
-
return defu5(
|
|
1654
|
-
this.inlineConfig,
|
|
1655
|
-
...configs,
|
|
1656
|
-
{
|
|
1657
|
-
root: this.cwd,
|
|
1658
|
-
mode: "development",
|
|
1659
|
-
plugins: [vitePluginWeapp(this, subPackageMeta)],
|
|
1660
|
-
// https://github.com/vitejs/vite/blob/a0336bd5197bb4427251be4c975e30fb596c658f/packages/vite/src/node/config.ts#L1117
|
|
1661
|
-
define: this.defineImportMetaEnv,
|
|
1662
|
-
build: {
|
|
1663
|
-
watch: {
|
|
1664
|
-
exclude: [
|
|
1665
|
-
...defaultExcluded,
|
|
1666
|
-
this.mpDistRoot ? path8.join(this.mpDistRoot, "**") : "dist/**"
|
|
1667
|
-
],
|
|
1668
|
-
include: [path8.join(this.srcRoot, "**")],
|
|
1669
|
-
chokidar: {
|
|
1670
|
-
ignored: [...defaultExcluded]
|
|
1671
|
-
}
|
|
1672
|
-
},
|
|
1673
|
-
minify: false,
|
|
1674
|
-
emptyOutDir: false
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
|
-
);
|
|
1678
|
-
} else {
|
|
1679
|
-
const inlineConfig = defu5(
|
|
1680
|
-
this.inlineConfig,
|
|
1681
|
-
...configs,
|
|
1682
|
-
{
|
|
1683
|
-
root: this.cwd,
|
|
1684
|
-
plugins: [vitePluginWeapp(this, subPackageMeta)],
|
|
1685
|
-
mode: "production",
|
|
1686
|
-
define: this.defineImportMetaEnv,
|
|
1687
|
-
build: {
|
|
1688
|
-
emptyOutDir: false
|
|
1689
|
-
}
|
|
1690
|
-
}
|
|
1691
|
-
);
|
|
1692
|
-
inlineConfig.logLevel = "info";
|
|
1693
|
-
return inlineConfig;
|
|
1694
|
-
}
|
|
1695
|
-
}
|
|
1696
|
-
getPagesSet() {
|
|
1697
|
-
const set3 = /* @__PURE__ */ new Set();
|
|
1698
|
-
const pages = this.appEntry?.json?.pages;
|
|
1699
|
-
pages?.forEach((x) => {
|
|
1700
|
-
set3.add(x);
|
|
1701
|
-
});
|
|
1702
|
-
this.appEntry?.json?.subPackages?.forEach((subPkg) => {
|
|
1703
|
-
subPkg.pages?.forEach((page) => {
|
|
1704
|
-
set3.add(`${subPkg.root}/${page}`);
|
|
1705
|
-
});
|
|
1706
|
-
});
|
|
1707
|
-
this.appEntry?.json?.subpackages?.forEach((subPkg) => {
|
|
1708
|
-
subPkg.pages?.forEach((page) => {
|
|
1709
|
-
set3.add(`${subPkg.root}/${page}`);
|
|
1710
|
-
});
|
|
1711
|
-
});
|
|
1712
|
-
return set3;
|
|
1713
|
-
}
|
|
1714
|
-
async runProd() {
|
|
1715
|
-
debug2?.("prod build start");
|
|
1716
|
-
const output = await build2(
|
|
1717
|
-
this.getConfig()
|
|
1718
|
-
);
|
|
1719
|
-
debug2?.("prod build end");
|
|
1720
|
-
await this.buildSubPackage();
|
|
1721
|
-
return output;
|
|
1722
|
-
}
|
|
1723
|
-
async build() {
|
|
1724
|
-
if (this.mpDistRoot) {
|
|
1725
|
-
const deletedFilePaths = await deleteAsync(
|
|
1726
|
-
[
|
|
1727
|
-
path8.resolve(this.outDir, "**")
|
|
1728
|
-
],
|
|
1729
|
-
{
|
|
1730
|
-
ignore: ["**/miniprogram_npm/**"]
|
|
1731
|
-
}
|
|
1732
|
-
);
|
|
1733
|
-
debug2?.("deletedFilePaths", deletedFilePaths);
|
|
1734
|
-
logger_default.success(`\u5DF2\u6E05\u7A7A ${this.mpDistRoot} \u76EE\u5F55`);
|
|
1735
|
-
}
|
|
1736
|
-
debug2?.("build start");
|
|
1737
|
-
if (this.isDev) {
|
|
1738
|
-
await this.runDev();
|
|
1739
|
-
} else {
|
|
1740
|
-
await this.runProd();
|
|
1741
|
-
}
|
|
1742
|
-
debug2?.("build end");
|
|
1743
|
-
}
|
|
1744
|
-
/**
|
|
1745
|
-
* @deps [this.scanComponentEntry]
|
|
1746
|
-
* @param entry
|
|
1747
|
-
* @param relDir
|
|
1748
|
-
*/
|
|
1749
|
-
async usingComponentsHandler(entry, relDir, subPackageMeta) {
|
|
1750
|
-
const { usingComponents } = entry.json;
|
|
1751
|
-
if (usingComponents) {
|
|
1752
|
-
for (const [, componentUrl] of Object.entries(usingComponents)) {
|
|
1753
|
-
if (/plugin:\/\//.test(componentUrl)) {
|
|
1754
|
-
continue;
|
|
1755
|
-
}
|
|
1756
|
-
const tokens = componentUrl.split("/");
|
|
1757
|
-
if (tokens[0] && isObject5(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
|
|
1758
|
-
continue;
|
|
1759
|
-
} else if (tokens[0] === "") {
|
|
1760
|
-
await this.scanComponentEntry(componentUrl.substring(1), path8.resolve(this.cwd, this.srcRoot), subPackageMeta);
|
|
1761
|
-
} else {
|
|
1762
|
-
const importee = resolveImportee(componentUrl, entry, this.aliasEntries);
|
|
1763
|
-
await this.scanComponentEntry(importee, relDir, subPackageMeta);
|
|
1764
|
-
}
|
|
1765
|
-
}
|
|
1766
|
-
}
|
|
1767
|
-
}
|
|
1768
|
-
resetEntries() {
|
|
1769
|
-
this.entriesSet.clear();
|
|
1770
|
-
this.entries.length = 0;
|
|
1771
|
-
this.subPackageMeta = {};
|
|
1772
|
-
}
|
|
1773
|
-
resetAutoImport() {
|
|
1774
|
-
this.potentialComponentMap.clear();
|
|
1775
|
-
this.wxmlComponentsMap.clear();
|
|
1776
|
-
}
|
|
1777
|
-
resolvedComponentName(entry) {
|
|
1778
|
-
const base = path8.basename(entry);
|
|
1779
|
-
if (base === "index") {
|
|
1780
|
-
const dirName = path8.dirname(entry);
|
|
1781
|
-
if (dirName === ".") {
|
|
1782
|
-
return;
|
|
1783
|
-
}
|
|
1784
|
-
return path8.basename(dirName);
|
|
1785
|
-
}
|
|
1786
|
-
return base;
|
|
1787
|
-
}
|
|
1788
|
-
// for auto import
|
|
1789
|
-
async scanPotentialComponentEntries(filePath) {
|
|
1790
|
-
const baseName = removeExtension3(filePath);
|
|
1791
|
-
const jsEntry = await findJsEntry(baseName);
|
|
1792
|
-
if (!jsEntry) {
|
|
1793
|
-
return;
|
|
1794
|
-
}
|
|
1795
|
-
if (jsEntry) {
|
|
1796
|
-
const jsonPath = await findJsonEntry(baseName);
|
|
1797
|
-
if (jsonPath) {
|
|
1798
|
-
const json = await this.readCommentJson(jsonPath);
|
|
1799
|
-
if (json && json.component) {
|
|
1800
|
-
const partialEntry = {
|
|
1801
|
-
path: jsEntry,
|
|
1802
|
-
json,
|
|
1803
|
-
jsonPath,
|
|
1804
|
-
type: "component",
|
|
1805
|
-
templatePath: filePath
|
|
1806
|
-
};
|
|
1807
|
-
const componentName = this.resolvedComponentName(baseName);
|
|
1808
|
-
if (componentName) {
|
|
1809
|
-
if (this.potentialComponentMap.has(componentName)) {
|
|
1810
|
-
logger_default.warn(`\u53D1\u73B0\u7EC4\u4EF6\u91CD\u540D! \u8DF3\u8FC7\u7EC4\u4EF6 ${this.relativeCwd(baseName)} \u7684\u81EA\u52A8\u5F15\u5165`);
|
|
1811
|
-
return;
|
|
1812
|
-
}
|
|
1813
|
-
this.potentialComponentMap.set(componentName, {
|
|
1814
|
-
entry: partialEntry,
|
|
1815
|
-
value: {
|
|
1816
|
-
name: componentName,
|
|
1817
|
-
from: `/${this.relativeSrcRoot(this.relativeCwd(removeExtension3(partialEntry.jsonPath)))}`
|
|
1818
|
-
}
|
|
1819
|
-
});
|
|
1820
|
-
}
|
|
1821
|
-
}
|
|
1822
|
-
}
|
|
1823
|
-
}
|
|
1824
|
-
}
|
|
1825
|
-
async scanAppEntry() {
|
|
1826
|
-
debug2?.("scanAppEntry start");
|
|
1827
|
-
this.resetEntries();
|
|
1828
|
-
const appDirname = path8.resolve(this.cwd, this.srcRoot);
|
|
1829
|
-
const appBasename = path8.resolve(appDirname, "app");
|
|
1830
|
-
const appConfigFile = await findJsonEntry(appBasename);
|
|
1831
|
-
const appEntryPath = await findJsEntry(appBasename);
|
|
1832
|
-
if (appEntryPath && appConfigFile) {
|
|
1833
|
-
const config = await this.readCommentJson(appConfigFile);
|
|
1834
|
-
if (isObject5(config)) {
|
|
1835
|
-
if (this.entriesSet.has(appEntryPath)) {
|
|
1836
|
-
return;
|
|
1837
|
-
}
|
|
1838
|
-
this.entriesSet.add(appEntryPath);
|
|
1839
|
-
const appEntry = {
|
|
1840
|
-
path: appEntryPath,
|
|
1841
|
-
json: config,
|
|
1842
|
-
jsonPath: appConfigFile,
|
|
1843
|
-
type: "app"
|
|
1844
|
-
};
|
|
1845
|
-
this.entries.push(appEntry);
|
|
1846
|
-
this.appEntry = appEntry;
|
|
1847
|
-
const { pages, subpackages = [], subPackages = [], sitemapLocation = "sitemap.json", themeLocation = "theme.json" } = config;
|
|
1848
|
-
if (sitemapLocation) {
|
|
1849
|
-
const sitemapJsonPath = await findJsonEntry(path8.resolve(appDirname, sitemapLocation));
|
|
1850
|
-
if (sitemapJsonPath) {
|
|
1851
|
-
appEntry.sitemapJsonPath = sitemapJsonPath;
|
|
1852
|
-
appEntry.sitemapJson = await this.readCommentJson(sitemapJsonPath);
|
|
1853
|
-
}
|
|
1854
|
-
}
|
|
1855
|
-
if (themeLocation) {
|
|
1856
|
-
const themeJsonPath = await findJsonEntry(path8.resolve(appDirname, themeLocation));
|
|
1857
|
-
if (themeJsonPath) {
|
|
1858
|
-
appEntry.themeJsonPath = themeJsonPath;
|
|
1859
|
-
appEntry.themeJson = await this.readCommentJson(themeJsonPath);
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
const subs = [...subpackages, ...subPackages];
|
|
1863
|
-
await this.usingComponentsHandler(appEntry, appDirname);
|
|
1864
|
-
if (Array.isArray(pages)) {
|
|
1865
|
-
for (const page of pages) {
|
|
1866
|
-
await this.scanComponentEntry(page, appDirname);
|
|
1867
|
-
}
|
|
1868
|
-
}
|
|
1869
|
-
for (const sub of subs) {
|
|
1870
|
-
if (sub.independent || this.inlineConfig.weapp?.subPackages?.[sub.root]?.independent) {
|
|
1871
|
-
const meta = {
|
|
1872
|
-
entries: [],
|
|
1873
|
-
entriesSet: /* @__PURE__ */ new Set(),
|
|
1874
|
-
// 合并选项
|
|
1875
|
-
subPackage: {
|
|
1876
|
-
...sub,
|
|
1877
|
-
dependencies: this.inlineConfig.weapp?.subPackages?.[sub.root].dependencies
|
|
1878
|
-
}
|
|
1879
|
-
};
|
|
1880
|
-
if (Array.isArray(sub.pages)) {
|
|
1881
|
-
for (const page of sub.pages) {
|
|
1882
|
-
await this.scanComponentEntry(path8.join(sub.root, page), appDirname, meta);
|
|
1883
|
-
}
|
|
1884
|
-
}
|
|
1885
|
-
if (sub.entry) {
|
|
1886
|
-
await this.scanComponentEntry(path8.join(sub.root, sub.entry), appDirname, meta);
|
|
1887
|
-
}
|
|
1888
|
-
this.subPackageMeta[sub.root] = meta;
|
|
1889
|
-
} else {
|
|
1890
|
-
if (Array.isArray(sub.pages)) {
|
|
1891
|
-
for (const page of sub.pages) {
|
|
1892
|
-
await this.scanComponentEntry(path8.join(sub.root, page), appDirname);
|
|
1893
|
-
}
|
|
1894
|
-
}
|
|
1895
|
-
if (sub.entry) {
|
|
1896
|
-
await this.scanComponentEntry(path8.join(sub.root, sub.entry), appDirname);
|
|
1897
|
-
}
|
|
1898
|
-
}
|
|
1899
|
-
}
|
|
1900
|
-
if (get2(appEntry, "json.tabBar.custom")) {
|
|
1901
|
-
await this.scanComponentEntry("custom-tab-bar/index", appDirname);
|
|
1902
|
-
}
|
|
1903
|
-
if (get2(appEntry, "json.appBar")) {
|
|
1904
|
-
await this.scanComponentEntry("app-bar/index", appDirname);
|
|
1905
|
-
}
|
|
1906
|
-
debug2?.("scanAppEntry end");
|
|
1907
|
-
return appEntry;
|
|
1908
|
-
}
|
|
1909
|
-
} else {
|
|
1910
|
-
throw new Error(`\u5728 ${appDirname} \u76EE\u5F55\u4E0B\u6CA1\u6709\u627E\u5230 \`app.json\`, \u8BF7\u786E\u4FDD\u4F60\u521D\u59CB\u5316\u4E86\u5C0F\u7A0B\u5E8F\u9879\u76EE\uFF0C\u6216\u8005\u5728 \`vite.config.ts\` \u4E2D\u8BBE\u7F6E\u7684\u6B63\u786E\u7684 \`weapp.srcRoot\` \u914D\u7F6E\u8DEF\u5F84 `);
|
|
1911
|
-
}
|
|
1912
|
-
}
|
|
1913
|
-
// usingComponents
|
|
1914
|
-
// subpackages / subPackages
|
|
1915
|
-
// pages
|
|
1916
|
-
// https://developers.weixin.qq.com/miniprogram/dev/framework/structure.html
|
|
1917
|
-
// 页面可以没有 JSON
|
|
1918
|
-
/**
|
|
1919
|
-
* 扫描并处理组件入口文件。
|
|
1920
|
-
* @param componentEntry 组件入口文件名
|
|
1921
|
-
* @param dirname 当前目录路径
|
|
1922
|
-
* @param subPackageMeta 分包元信息(可选)
|
|
1923
|
-
* @returns Promise<void>
|
|
1924
|
-
*
|
|
1925
|
-
* 该函数用于扫描并处理组件入口文件,包括查找 JS 入口、JSON 配置文件、模板入口等。
|
|
1926
|
-
* 同时处理引入组件的情况,自动注入 usingComponents。
|
|
1927
|
-
*/
|
|
1928
|
-
async scanComponentEntry(componentEntry, dirname, subPackageMeta) {
|
|
1929
|
-
const meta = subPackageMeta ?? {
|
|
1930
|
-
entriesSet: this.entriesSet,
|
|
1931
|
-
entries: this.entries
|
|
1932
|
-
};
|
|
1933
|
-
debug2?.("scanComponentEntry start", componentEntry);
|
|
1934
|
-
let baseName = removeExtension3(path8.resolve(dirname, componentEntry));
|
|
1935
|
-
if (await fs8.exists(baseName)) {
|
|
1936
|
-
const stat = await fs8.stat(baseName);
|
|
1937
|
-
if (stat.isDirectory()) {
|
|
1938
|
-
baseName = path8.join(baseName, "index");
|
|
1939
|
-
}
|
|
1940
|
-
}
|
|
1941
|
-
const jsEntry = await findJsEntry(baseName);
|
|
1942
|
-
const partialEntry = {
|
|
1943
|
-
path: jsEntry
|
|
1944
|
-
};
|
|
1945
|
-
if (jsEntry && !meta.entriesSet.has(jsEntry)) {
|
|
1946
|
-
meta.entriesSet.add(jsEntry);
|
|
1947
|
-
meta.entries.push(partialEntry);
|
|
1948
|
-
}
|
|
1949
|
-
const configFile = await findJsonEntry(baseName);
|
|
1950
|
-
if (configFile) {
|
|
1951
|
-
const config = await this.readCommentJson(configFile);
|
|
1952
|
-
const jsonFragment = {
|
|
1953
|
-
json: config,
|
|
1954
|
-
jsonPath: configFile
|
|
1955
|
-
};
|
|
1956
|
-
const pagesSet = this.getPagesSet();
|
|
1957
|
-
const templatePath = await findTemplateEntry(baseName);
|
|
1958
|
-
if (templatePath) {
|
|
1959
|
-
partialEntry.templatePath = templatePath;
|
|
1960
|
-
}
|
|
1961
|
-
if (isObject5(config) && config.component === true) {
|
|
1962
|
-
partialEntry.type = "component";
|
|
1963
|
-
} else {
|
|
1964
|
-
const pagePath = this.relativeSrcRoot(this.relativeCwd(baseName));
|
|
1965
|
-
if (pagesSet.has(pagePath)) {
|
|
1966
|
-
partialEntry.type = "page";
|
|
1967
|
-
}
|
|
1968
|
-
}
|
|
1969
|
-
const hit = this.wxmlComponentsMap.get(baseName);
|
|
1970
|
-
if (hit) {
|
|
1971
|
-
const depComponentNames = Object.keys(hit);
|
|
1972
|
-
debug2?.(this.potentialComponentMap, jsonFragment.json.usingComponents);
|
|
1973
|
-
for (const depComponentName of depComponentNames) {
|
|
1974
|
-
const res = this.potentialComponentMap.get(depComponentName);
|
|
1975
|
-
if (res) {
|
|
1976
|
-
const { entry: componentEntry2, value } = res;
|
|
1977
|
-
if (componentEntry2 && componentEntry2.jsonPath) {
|
|
1978
|
-
if (isObject5(jsonFragment.json.usingComponents) && Reflect.has(jsonFragment.json.usingComponents, value.name)) {
|
|
1979
|
-
continue;
|
|
1980
|
-
}
|
|
1981
|
-
set2(jsonFragment.json, `usingComponents.${value.name}`, value.from);
|
|
1982
|
-
}
|
|
1983
|
-
} else if (Array.isArray(this.inlineConfig.weapp?.enhance?.autoImportComponents?.resolvers)) {
|
|
1984
|
-
for (const resolver of this.inlineConfig.weapp.enhance.autoImportComponents.resolvers) {
|
|
1985
|
-
const value = resolver(depComponentName, baseName);
|
|
1986
|
-
if (value) {
|
|
1987
|
-
if (!(isObject5(jsonFragment.json.usingComponents) && Reflect.has(jsonFragment.json.usingComponents, value.name))) {
|
|
1988
|
-
set2(jsonFragment.json, `usingComponents.${value.name}`, value.from);
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
}
|
|
1992
|
-
}
|
|
1993
|
-
}
|
|
1994
|
-
}
|
|
1995
|
-
if (jsEntry) {
|
|
1996
|
-
partialEntry.json = jsonFragment.json;
|
|
1997
|
-
partialEntry.jsonPath = jsonFragment.jsonPath;
|
|
1998
|
-
}
|
|
1999
|
-
if (isObject5(config)) {
|
|
2000
|
-
await this.usingComponentsHandler(jsonFragment, path8.dirname(configFile), subPackageMeta);
|
|
2001
|
-
}
|
|
2002
|
-
}
|
|
2003
|
-
debug2?.("scanComponentEntry end", componentEntry);
|
|
2004
|
-
}
|
|
2005
|
-
setRollupWatcher(watcher, root = "/") {
|
|
2006
|
-
const oldWatcher = this.rollupWatcherMap.get(root);
|
|
2007
|
-
oldWatcher?.close();
|
|
2008
|
-
this.rollupWatcherMap.set(root, watcher);
|
|
2009
|
-
}
|
|
2010
|
-
// eslint-disable-next-line ts/no-unused-vars
|
|
2011
|
-
autoImportFilter(id, meta) {
|
|
2012
|
-
if (this.inlineConfig.weapp?.enhance?.autoImportComponents?.globs) {
|
|
2013
|
-
const isMatch = pm(this.inlineConfig.weapp.enhance.autoImportComponents.globs, {
|
|
2014
|
-
cwd: this.cwd,
|
|
2015
|
-
windows: true,
|
|
2016
|
-
posixSlashes: true
|
|
2017
|
-
});
|
|
2018
|
-
return isMatch(id);
|
|
2019
|
-
}
|
|
2020
|
-
return false;
|
|
2021
|
-
}
|
|
2022
|
-
// #region placeholder for class type
|
|
2023
|
-
async buildSubPackage() {
|
|
2024
|
-
}
|
|
2025
|
-
/**
|
|
2026
|
-
* 不修改 ctx
|
|
2027
|
-
*/
|
|
2028
|
-
// eslint-disable-next-line ts/no-unused-vars
|
|
2029
|
-
async readCommentJson(filepath) {
|
|
2030
|
-
}
|
|
2031
|
-
// https://cn.vitejs.dev/guide/build.html#library-mode
|
|
2032
|
-
// miniprogram_dist
|
|
2033
|
-
// miniprogram
|
|
2034
|
-
// https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6%E7%9B%B8%E5%85%B3%E7%A4%BA%E4%BE%8B
|
|
2035
|
-
// eslint-disable-next-line ts/no-unused-vars
|
|
2036
|
-
async buildNpm(subPackage, options) {
|
|
2037
|
-
}
|
|
2038
|
-
async loadDefaultConfig() {
|
|
2039
|
-
}
|
|
2040
|
-
get dependenciesCacheFilePath() {
|
|
2041
|
-
return "";
|
|
2042
|
-
}
|
|
2043
|
-
get dependenciesCacheHash() {
|
|
2044
|
-
return "";
|
|
2045
|
-
}
|
|
2046
|
-
writeDependenciesCache() {
|
|
2047
|
-
}
|
|
2048
|
-
async readDependenciesCache() {
|
|
2049
|
-
}
|
|
2050
|
-
async checkDependenciesCacheOutdate() {
|
|
2051
|
-
return true;
|
|
2052
|
-
}
|
|
2053
|
-
// #endregion
|
|
2054
|
-
};
|
|
2055
|
-
CompilerContext.prototype.buildSubPackage = buildSubPackage;
|
|
2056
|
-
CompilerContext.prototype.readCommentJson = readCommentJson;
|
|
2057
|
-
CompilerContext.prototype.buildNpm = buildNpm;
|
|
2058
|
-
CompilerContext.prototype.loadDefaultConfig = loadDefaultConfig;
|
|
2059
|
-
dependenciesCache(CompilerContext.prototype);
|
|
2060
|
-
|
|
2061
|
-
// src/context.ts
|
|
2062
|
-
init_esm_shims();
|
|
2063
|
-
import { defu as defu6 } from "@weapp-core/shared";
|
|
2064
|
-
async function createCompilerContext(options, opts) {
|
|
2065
|
-
const { loadConfig } = defu6(opts, { loadConfig: true });
|
|
2066
|
-
const ctx = new CompilerContext(options);
|
|
2067
|
-
if (loadConfig) {
|
|
2068
|
-
await ctx.loadDefaultConfig();
|
|
2069
|
-
}
|
|
2070
|
-
return ctx;
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
|
-
export {
|
|
2074
|
-
VERSION,
|
|
2075
|
-
logger_default,
|
|
2076
|
-
CompilerContext,
|
|
2077
|
-
createCompilerContext
|
|
2078
|
-
};
|