weapp-vite 0.0.2-alpha.1 → 0.0.2-alpha.3
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/chunk-6FYX6NKW.js +442 -0
- package/dist/cli.cjs +254 -125
- package/dist/cli.js +20 -323
- package/dist/index.cjs +452 -4
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +8 -5
- package/package.json +10 -3
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// src/build.ts
|
|
2
|
+
import process2 from "node:process";
|
|
3
|
+
import { build } from "vite";
|
|
4
|
+
import { addExtension as addExtension3, defu as defu2, removeExtension as removeExtension3 } from "@weapp-core/shared";
|
|
5
|
+
|
|
6
|
+
// src/plugins/index.ts
|
|
7
|
+
import path4 from "node:path";
|
|
8
|
+
import fs3 from "fs-extra";
|
|
9
|
+
import MagicString from "magic-string";
|
|
10
|
+
import { addExtension as addExtension2, removeExtension as removeExtension2 } from "@weapp-core/shared";
|
|
11
|
+
import fg from "fast-glob";
|
|
12
|
+
import { isCSSRequest, preprocessCSS } from "vite";
|
|
13
|
+
|
|
14
|
+
// src/utils/scan.ts
|
|
15
|
+
import path from "pathe";
|
|
16
|
+
import fs from "fs-extra";
|
|
17
|
+
import klaw from "klaw";
|
|
18
|
+
import { addExtension, isObject, removeExtension } from "@weapp-core/shared";
|
|
19
|
+
var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
|
|
20
|
+
function searchAppEntry(root, formatPath) {
|
|
21
|
+
const extensions = ["js", "ts"];
|
|
22
|
+
const appJsonPath = path.resolve(root, "app.json");
|
|
23
|
+
if (fs.existsSync(appJsonPath)) {
|
|
24
|
+
for (const ext of extensions) {
|
|
25
|
+
const entryPath = path.resolve(root, addExtension("app", `.${ext}`));
|
|
26
|
+
if (fs.existsSync(entryPath)) {
|
|
27
|
+
const appJson = fs.readJSONSync(appJsonPath, { throws: false });
|
|
28
|
+
const deps = [];
|
|
29
|
+
if (appJson) {
|
|
30
|
+
if (Array.isArray(appJson.pages)) {
|
|
31
|
+
deps.push(...appJson.pages.map((x) => {
|
|
32
|
+
return {
|
|
33
|
+
path: x,
|
|
34
|
+
type: "page"
|
|
35
|
+
};
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
if (isObject(appJson.usingComponents)) {
|
|
39
|
+
deps.push(...parseJsonUseComponents(appJson));
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(appJson.subpackages)) {
|
|
42
|
+
for (const subpackage of appJson.subpackages) {
|
|
43
|
+
deps.push(...subpackage.map((x) => {
|
|
44
|
+
return {
|
|
45
|
+
type: "subpackage",
|
|
46
|
+
...x
|
|
47
|
+
};
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
path: formatPath ? formatPath(entryPath) : entryPath,
|
|
54
|
+
deps,
|
|
55
|
+
type: "app"
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function searchPageEntry(wxmlPath) {
|
|
62
|
+
if (fs.existsSync(wxmlPath)) {
|
|
63
|
+
const extensions = ["js", "ts"];
|
|
64
|
+
const base = removeExtension(wxmlPath);
|
|
65
|
+
for (const ext of extensions) {
|
|
66
|
+
const entryPath = addExtension(base, `.${ext}`);
|
|
67
|
+
if (fs.existsSync(entryPath)) {
|
|
68
|
+
return entryPath;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function parseJsonUseComponents(json) {
|
|
74
|
+
const deps = [];
|
|
75
|
+
if (isObject(json.usingComponents)) {
|
|
76
|
+
deps.push(...Object.values(json.usingComponents).map((x) => {
|
|
77
|
+
return {
|
|
78
|
+
path: x,
|
|
79
|
+
type: "component"
|
|
80
|
+
};
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
return deps;
|
|
84
|
+
}
|
|
85
|
+
function getWxmlEntry(wxmlPath, formatPath) {
|
|
86
|
+
const pageEntry = searchPageEntry(wxmlPath);
|
|
87
|
+
if (pageEntry) {
|
|
88
|
+
const jsonPath = addExtension(removeExtension(wxmlPath), ".json");
|
|
89
|
+
const finalPath = formatPath(pageEntry);
|
|
90
|
+
if (fs.existsSync(jsonPath)) {
|
|
91
|
+
const json = fs.readJsonSync(jsonPath, { throws: false });
|
|
92
|
+
if (json && json.component) {
|
|
93
|
+
return {
|
|
94
|
+
deps: parseJsonUseComponents(json),
|
|
95
|
+
path: finalPath,
|
|
96
|
+
type: "component"
|
|
97
|
+
};
|
|
98
|
+
} else {
|
|
99
|
+
return {
|
|
100
|
+
deps: parseJsonUseComponents(json),
|
|
101
|
+
path: finalPath,
|
|
102
|
+
type: "page"
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
deps: [],
|
|
108
|
+
path: finalPath,
|
|
109
|
+
type: "page"
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function scanEntries(root, options) {
|
|
114
|
+
function formatPath(to) {
|
|
115
|
+
if (options?.relative) {
|
|
116
|
+
return path.relative(root, to);
|
|
117
|
+
}
|
|
118
|
+
return path.normalize(to);
|
|
119
|
+
}
|
|
120
|
+
const appEntry = searchAppEntry(root, formatPath);
|
|
121
|
+
if (appEntry) {
|
|
122
|
+
const pageEntries = [];
|
|
123
|
+
const componentEntries = [];
|
|
124
|
+
for await (const file of klaw(
|
|
125
|
+
root,
|
|
126
|
+
{
|
|
127
|
+
filter: options?.filter
|
|
128
|
+
}
|
|
129
|
+
)) {
|
|
130
|
+
if (file.stats.isFile()) {
|
|
131
|
+
if (/\.wxml$/.test(file.path)) {
|
|
132
|
+
const entry = getWxmlEntry(file.path, formatPath);
|
|
133
|
+
if (entry) {
|
|
134
|
+
if (entry.type === "component") {
|
|
135
|
+
componentEntries.push(entry);
|
|
136
|
+
} else if (entry.type === "page") {
|
|
137
|
+
pageEntries.push(entry);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
app: appEntry,
|
|
145
|
+
pages: pageEntries,
|
|
146
|
+
components: componentEntries,
|
|
147
|
+
subpackages: []
|
|
148
|
+
// walkPathsSet,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/utils/projectConfig.ts
|
|
154
|
+
import path2 from "pathe";
|
|
155
|
+
import fs2 from "fs-extra";
|
|
156
|
+
|
|
157
|
+
// src/utils/index.ts
|
|
158
|
+
var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
|
|
159
|
+
var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
|
|
160
|
+
|
|
161
|
+
// src/entry.ts
|
|
162
|
+
import process from "node:process";
|
|
163
|
+
import mm from "micromatch";
|
|
164
|
+
import { defu } from "@weapp-core/shared";
|
|
165
|
+
import path3 from "pathe";
|
|
166
|
+
function createFilter(include, exclude, options) {
|
|
167
|
+
const opts = defu(options, {
|
|
168
|
+
ignore: exclude
|
|
169
|
+
// dot: true,
|
|
170
|
+
// contains: true,
|
|
171
|
+
});
|
|
172
|
+
return function(id) {
|
|
173
|
+
if (typeof id !== "string") {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (/\0/.test(id)) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
return mm.isMatch(id, include, opts);
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function getEntries(options) {
|
|
183
|
+
const { root = process.cwd(), outDir = "dist", relative, srcRoot = "" } = options;
|
|
184
|
+
const filter = createFilter(
|
|
185
|
+
[path3.join(srcRoot, "**/*")],
|
|
186
|
+
[...defaultExcluded, path3.join(root, `${outDir}/**`)],
|
|
187
|
+
{ cwd: root }
|
|
188
|
+
);
|
|
189
|
+
return scanEntries(root, { filter, relative });
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/cache.ts
|
|
193
|
+
function createPluginCache(cache) {
|
|
194
|
+
return {
|
|
195
|
+
delete(id) {
|
|
196
|
+
return delete cache[id];
|
|
197
|
+
},
|
|
198
|
+
get(id) {
|
|
199
|
+
const item = cache[id];
|
|
200
|
+
if (!item) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
item[0] = 0;
|
|
204
|
+
return item[1];
|
|
205
|
+
},
|
|
206
|
+
has(id) {
|
|
207
|
+
const item = cache[id];
|
|
208
|
+
if (!item) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
item[0] = 0;
|
|
212
|
+
return true;
|
|
213
|
+
},
|
|
214
|
+
set(id, value) {
|
|
215
|
+
cache[id] = [0, value];
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/debugger.ts
|
|
221
|
+
import createDebug from "debug";
|
|
222
|
+
function createDebugger(namespace) {
|
|
223
|
+
const debug2 = createDebug(namespace);
|
|
224
|
+
if (debug2.enabled) {
|
|
225
|
+
return debug2;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/plugins/parse.ts
|
|
230
|
+
function parseRequest(id) {
|
|
231
|
+
const [filename, rawQuery] = id.split(`?`, 2);
|
|
232
|
+
const query = Object.fromEntries(new URLSearchParams(rawQuery));
|
|
233
|
+
if (Reflect.has(query, "wxss")) {
|
|
234
|
+
query.wxss = true;
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
filename,
|
|
238
|
+
query
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/plugins/index.ts
|
|
243
|
+
var debug = createDebugger("weapp-vite:plugin");
|
|
244
|
+
function normalizeCssPath(id) {
|
|
245
|
+
return addExtension2(removeExtension2(id), ".wxss");
|
|
246
|
+
}
|
|
247
|
+
function getRealPath(res) {
|
|
248
|
+
if (res.query.wxss) {
|
|
249
|
+
return addExtension2(removeExtension2(res.filename), ".wxss");
|
|
250
|
+
}
|
|
251
|
+
return res.filename;
|
|
252
|
+
}
|
|
253
|
+
function vitePluginWeapp() {
|
|
254
|
+
function getInputOption(entries) {
|
|
255
|
+
return entries.reduce((acc, cur) => {
|
|
256
|
+
acc[relative(cur)] = cur;
|
|
257
|
+
return acc;
|
|
258
|
+
}, {});
|
|
259
|
+
}
|
|
260
|
+
const stylesIds = /* @__PURE__ */ new Set();
|
|
261
|
+
let configResolved;
|
|
262
|
+
let entriesSet = /* @__PURE__ */ new Set();
|
|
263
|
+
function relative(p) {
|
|
264
|
+
return path4.relative(configResolved.root, p);
|
|
265
|
+
}
|
|
266
|
+
const cacheInstance = createPluginCache(/* @__PURE__ */ Object.create(null));
|
|
267
|
+
return [
|
|
268
|
+
{
|
|
269
|
+
name: "weapp-vite:pre",
|
|
270
|
+
enforce: "pre",
|
|
271
|
+
// config->configResolved->|watching|options->buildStart
|
|
272
|
+
config(config, env) {
|
|
273
|
+
debug?.(config, env);
|
|
274
|
+
},
|
|
275
|
+
async options(options) {
|
|
276
|
+
const entries = await getEntries({
|
|
277
|
+
root: configResolved.root,
|
|
278
|
+
outDir: configResolved.build.outDir,
|
|
279
|
+
srcRoot: configResolved.weapp?.srcRoot
|
|
280
|
+
});
|
|
281
|
+
if (entries) {
|
|
282
|
+
const paths = [entries.app, ...entries.pages, ...entries.components].map((x) => {
|
|
283
|
+
return x.path;
|
|
284
|
+
});
|
|
285
|
+
const input = getInputOption(paths);
|
|
286
|
+
entriesSet = new Set(paths);
|
|
287
|
+
options.input = input;
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
configResolved(config) {
|
|
291
|
+
debug?.(config);
|
|
292
|
+
configResolved = config;
|
|
293
|
+
},
|
|
294
|
+
resolveId(source) {
|
|
295
|
+
if (/\.wxss$/.test(source)) {
|
|
296
|
+
return source.replace(/\.wxss$/, ".css?wxss");
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
load(id) {
|
|
300
|
+
if (entriesSet.has(id)) {
|
|
301
|
+
const base = removeExtension2(id);
|
|
302
|
+
const ms = new MagicString(fs3.readFileSync(id, "utf8"));
|
|
303
|
+
for (const ext of supportedCssExtensions) {
|
|
304
|
+
const mayBeCssPath = addExtension2(base, ext);
|
|
305
|
+
if (fs3.existsSync(mayBeCssPath)) {
|
|
306
|
+
this.addWatchFile(mayBeCssPath);
|
|
307
|
+
ms.prepend(`import '${mayBeCssPath}'
|
|
308
|
+
`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
this.addWatchFile(id);
|
|
312
|
+
return {
|
|
313
|
+
code: ms.toString()
|
|
314
|
+
};
|
|
315
|
+
} else if (isCSSRequest(id)) {
|
|
316
|
+
stylesIds.add(id);
|
|
317
|
+
return {
|
|
318
|
+
code: ""
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
async buildEnd() {
|
|
323
|
+
const styles = {};
|
|
324
|
+
for (const stylesId of stylesIds) {
|
|
325
|
+
const parsed = parseRequest(stylesId);
|
|
326
|
+
const css = await fs3.readFile(getRealPath(parsed), "utf8");
|
|
327
|
+
const res = await preprocessCSS(css, stylesId, configResolved);
|
|
328
|
+
const fileName = relative(normalizeCssPath(stylesId));
|
|
329
|
+
if (styles[fileName]) {
|
|
330
|
+
styles[fileName] += res.code;
|
|
331
|
+
} else {
|
|
332
|
+
styles[fileName] = res.code;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
for (const style of Object.entries(styles)) {
|
|
336
|
+
this.emitFile({
|
|
337
|
+
type: "asset",
|
|
338
|
+
fileName: style[0],
|
|
339
|
+
source: style[1]
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
const files = await fg(
|
|
343
|
+
// 假如去 join root 就是返回 absolute
|
|
344
|
+
["**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}"],
|
|
345
|
+
{
|
|
346
|
+
cwd: configResolved.root,
|
|
347
|
+
ignore: [
|
|
348
|
+
...defaultExcluded,
|
|
349
|
+
`${configResolved.build.outDir}/**`,
|
|
350
|
+
"project.config.json",
|
|
351
|
+
"project.private.config.json",
|
|
352
|
+
"package.json"
|
|
353
|
+
],
|
|
354
|
+
absolute: false
|
|
355
|
+
}
|
|
356
|
+
);
|
|
357
|
+
for (const file of files) {
|
|
358
|
+
const filepath = path4.resolve(configResolved.root, file);
|
|
359
|
+
this.addWatchFile(filepath);
|
|
360
|
+
this.emitFile({
|
|
361
|
+
type: "asset",
|
|
362
|
+
fileName: file,
|
|
363
|
+
source: await fs3.readFile(filepath, "utf8")
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
// generateBundle(_options, _bundle) {
|
|
368
|
+
// const files = this.getWatchFiles()
|
|
369
|
+
// console.log(files)
|
|
370
|
+
// },
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
name: "weapp-vite"
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
name: "weapp-vite:post",
|
|
377
|
+
enforce: "post"
|
|
378
|
+
}
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// src/build.ts
|
|
383
|
+
function getDefaultConfig() {
|
|
384
|
+
return {
|
|
385
|
+
build: {
|
|
386
|
+
rollupOptions: {
|
|
387
|
+
output: {
|
|
388
|
+
format: "cjs",
|
|
389
|
+
entryFileNames: (chunkInfo) => {
|
|
390
|
+
if (chunkInfo.name.endsWith(".ts")) {
|
|
391
|
+
return addExtension3(removeExtension3(chunkInfo.name), ".js");
|
|
392
|
+
}
|
|
393
|
+
return chunkInfo.name;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
assetsDir: ".",
|
|
398
|
+
commonjsOptions: {
|
|
399
|
+
transformMixedEsModules: true,
|
|
400
|
+
include: void 0
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
plugins: [
|
|
404
|
+
vitePluginWeapp()
|
|
405
|
+
]
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
async function runDev(options) {
|
|
409
|
+
process2.env.NODE_ENV = "development";
|
|
410
|
+
const watcher = await build(
|
|
411
|
+
defu2(
|
|
412
|
+
options,
|
|
413
|
+
getDefaultConfig(),
|
|
414
|
+
{
|
|
415
|
+
mode: "development",
|
|
416
|
+
build: {
|
|
417
|
+
watch: {},
|
|
418
|
+
minify: false
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
)
|
|
422
|
+
);
|
|
423
|
+
return watcher;
|
|
424
|
+
}
|
|
425
|
+
async function runProd(options) {
|
|
426
|
+
const output = await build(
|
|
427
|
+
defu2(
|
|
428
|
+
options,
|
|
429
|
+
getDefaultConfig(),
|
|
430
|
+
{
|
|
431
|
+
mode: "production"
|
|
432
|
+
}
|
|
433
|
+
)
|
|
434
|
+
);
|
|
435
|
+
return output;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export {
|
|
439
|
+
getDefaultConfig,
|
|
440
|
+
runDev,
|
|
441
|
+
runProd
|
|
442
|
+
};
|