weapp-vite 1.2.4 → 1.3.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.
@@ -3,18 +3,20 @@ import logger from "@weapp-core/logger";
3
3
  var logger_default = logger;
4
4
 
5
5
  // package.json
6
- var version = "1.2.4";
6
+ var version = "1.3.0";
7
7
 
8
8
  // src/constants.ts
9
9
  var VERSION = version;
10
- var jsExtensions = ["js", "ts"];
10
+ var jsExtensions = ["ts", "js"];
11
+ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
12
+ var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
11
13
 
12
14
  // src/context.ts
13
15
  import { createRequire } from "node:module";
14
- import process2 from "node:process";
15
- import { addExtension as addExtension3, defu as defu3, removeExtension as removeExtension3 } from "@weapp-core/shared";
16
+ import process from "node:process";
17
+ import { addExtension as addExtension3, defu as defu2, isObject as isObject2, removeExtension as removeExtension3 } from "@weapp-core/shared";
16
18
  import { watch } from "chokidar";
17
- import fs4 from "fs-extra";
19
+ import fs6 from "fs-extra";
18
20
  import path5 from "pathe";
19
21
  import { build as tsupBuild } from "tsup";
20
22
  import { build, loadConfigFromFile } from "vite";
@@ -34,7 +36,7 @@ function getWeappWatchOptions() {
34
36
  // src/plugins/index.ts
35
37
  import { addExtension as addExtension2, removeExtension as removeExtension2 } from "@weapp-core/shared";
36
38
  import fg from "fast-glob";
37
- import fs3 from "fs-extra";
39
+ import fs5 from "fs-extra";
38
40
  import MagicString from "magic-string";
39
41
  import path4 from "pathe";
40
42
  import { isCSSRequest } from "vite";
@@ -48,24 +50,35 @@ function createDebugger(namespace) {
48
50
  }
49
51
  }
50
52
 
51
- // src/entry.ts
52
- import process from "node:process";
53
- import { defu as defu2 } from "@weapp-core/shared";
54
- import { fdir as Fdir } from "fdir";
55
- import mm from "micromatch";
53
+ // src/utils/index.ts
54
+ import fs4 from "fs-extra";
56
55
  import path3 from "pathe";
57
56
 
58
- // src/utils/projectConfig.ts
57
+ // src/utils/json.ts
58
+ import { parse as parseJson } from "comment-json";
59
59
  import fs from "fs-extra";
60
+ function parseCommentJson(json) {
61
+ return parseJson(json, void 0, true);
62
+ }
63
+ async function readCommentJson(filepath) {
64
+ try {
65
+ return parseCommentJson(await fs.readFile(filepath, "utf8"));
66
+ } catch {
67
+ logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
68
+ }
69
+ }
70
+
71
+ // src/utils/projectConfig.ts
72
+ import fs2 from "fs-extra";
60
73
  import path from "pathe";
61
74
  function getProjectConfig(root, options) {
62
75
  const baseJsonPath = path.resolve(root, "project.config.json");
63
76
  const privateJsonPath = path.resolve(root, "project.private.config.json");
64
77
  let baseJson = {};
65
78
  let privateJson = {};
66
- if (fs.existsSync(baseJsonPath)) {
79
+ if (fs2.existsSync(baseJsonPath)) {
67
80
  try {
68
- baseJson = fs.readJsonSync(baseJsonPath) || {};
81
+ baseJson = fs2.readJsonSync(baseJsonPath) || {};
69
82
  } catch {
70
83
  throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
71
84
  }
@@ -73,9 +86,9 @@ function getProjectConfig(root, options) {
73
86
  throw new Error(`\u5728 ${root} \u76EE\u5F55\u4E0B\u627E\u4E0D\u5230 project.config.json`);
74
87
  }
75
88
  if (!options?.ignorePrivate) {
76
- if (fs.existsSync(privateJsonPath)) {
89
+ if (fs2.existsSync(privateJsonPath)) {
77
90
  try {
78
- privateJson = fs.readJsonSync(privateJsonPath) || {};
91
+ privateJson = fs2.readJsonSync(privateJsonPath) || {};
79
92
  } catch {
80
93
  throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.private.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
81
94
  }
@@ -86,212 +99,29 @@ function getProjectConfig(root, options) {
86
99
 
87
100
  // src/utils/scan.ts
88
101
  import { addExtension, defu, isObject, removeExtension } from "@weapp-core/shared";
89
- import fs2 from "fs-extra";
102
+ import fs3 from "fs-extra";
90
103
  import path2 from "pathe";
91
- function parseJsonUseComponents(json) {
92
- const deps = [];
93
- if (isObject(json.usingComponents)) {
94
- deps.push(...Object.values(json.usingComponents).map((x) => {
95
- return {
96
- path: x,
97
- type: "component"
98
- };
99
- }));
100
- }
101
- return deps;
102
- }
103
- function searchAppEntry(options) {
104
- const { formatPath, root } = defu(options, {
105
- formatPath: (x) => x
106
- });
107
- const appJsonPath = path2.resolve(root, "app.json");
108
- if (fs2.existsSync(appJsonPath)) {
109
- for (const ext of jsExtensions) {
110
- const entryPath = path2.resolve(root, addExtension("app", `.${ext}`));
111
- if (fs2.existsSync(entryPath)) {
112
- const appJson = fs2.readJSONSync(appJsonPath, { throws: false });
113
- const deps = [];
114
- if (appJson) {
115
- if (Array.isArray(appJson.pages)) {
116
- deps.push(...appJson.pages.map((x) => {
117
- return {
118
- path: x,
119
- type: "page"
120
- };
121
- }));
122
- }
123
- if (isObject(appJson.usingComponents)) {
124
- deps.push(...parseJsonUseComponents(appJson));
125
- }
126
- if (Array.isArray(appJson.subPackages)) {
127
- deps.push(...appJson.subPackages.map((x) => {
128
- return {
129
- type: "subPackage",
130
- ...x
131
- };
132
- }));
133
- }
134
- }
135
- return {
136
- jsonPath: formatPath(appJsonPath),
137
- json: appJson,
138
- path: formatPath(entryPath),
139
- deps,
140
- type: "app"
141
- };
142
- }
143
- }
104
+
105
+ // src/utils/index.ts
106
+ function changeFileExtension(filePath, extension) {
107
+ if (typeof filePath !== "string") {
108
+ throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`);
144
109
  }
145
- }
146
- function searchPageEntry(wxmlPath) {
147
- if (fs2.existsSync(wxmlPath)) {
148
- const base = removeExtension(wxmlPath);
149
- for (const ext of jsExtensions) {
150
- const entryPath = addExtension(base, `.${ext}`);
151
- if (fs2.existsSync(entryPath)) {
152
- return entryPath;
153
- }
154
- }
110
+ if (typeof extension !== "string") {
111
+ throw new TypeError(`Expected \`extension\` to be a string, got \`${typeof extension}\`.`);
155
112
  }
156
- }
157
- function getWxmlEntry(wxmlPath, formatPath) {
158
- const pageEntry = searchPageEntry(wxmlPath);
159
- if (pageEntry) {
160
- const jsonPath = addExtension(removeExtension(wxmlPath), ".json");
161
- const finalPath = formatPath(pageEntry);
162
- if (fs2.existsSync(jsonPath)) {
163
- const json = fs2.readJsonSync(jsonPath, { throws: false });
164
- if (json && json.component) {
165
- return {
166
- deps: parseJsonUseComponents(json),
167
- path: finalPath,
168
- type: "component",
169
- json,
170
- jsonPath: formatPath(jsonPath)
171
- };
172
- } else {
173
- return {
174
- deps: parseJsonUseComponents(json),
175
- path: finalPath,
176
- type: "page",
177
- json,
178
- jsonPath: formatPath(jsonPath)
179
- };
180
- }
181
- }
182
- return {
183
- deps: [],
184
- path: finalPath,
185
- type: "page"
186
- // json: undefined,
187
- // jsonPath: undefined,
188
- };
113
+ if (filePath === "") {
114
+ return "";
189
115
  }
116
+ extension = extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
117
+ const basename = path3.basename(filePath, path3.extname(filePath));
118
+ return path3.join(path3.dirname(filePath), basename + extension);
190
119
  }
191
-
192
- // src/utils/index.ts
193
- var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
194
- var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
195
-
196
- // src/entry.ts
197
- function createFilter(include, exclude, options) {
198
- const opts = defu2(options, {
199
- ignore: exclude
200
- });
201
- return function(id) {
202
- if (typeof id !== "string") {
203
- return false;
204
- }
205
- if (/\0/.test(id)) {
206
- return false;
207
- }
208
- return mm.isMatch(id, include, opts);
209
- };
210
- }
211
- async function getEntries(options) {
212
- const { root = process.cwd(), outDir = "dist", relative, srcRoot = "", subPackage } = options;
213
- function formatPath(to) {
214
- if (relative) {
215
- return path3.relative(root, to);
216
- }
217
- return path3.normalize(to);
218
- }
219
- if (subPackage) {
220
- const subPackageRoot = subPackage.root ?? "";
221
- const filter = createFilter(
222
- [path3.join(root, srcRoot, subPackageRoot, "**/*")],
223
- [
224
- ...defaultExcluded
225
- ],
226
- { cwd: root }
227
- );
228
- const pageEntries = [];
229
- const componentEntries = [];
230
- const subPackageEntries = [];
231
- if (subPackage.entry) {
232
- subPackageEntries.push({
233
- deps: [],
234
- path: path3.join(root, subPackageRoot, subPackage.entry),
235
- type: "subPackageEntry"
236
- });
237
- }
238
- const files = await new Fdir().withFullPaths().filter(filter).crawl(path3.join(root, subPackageRoot)).withPromise();
239
- for (const file of files) {
240
- if (/\.wxml$/.test(file)) {
241
- const entry = getWxmlEntry(file, formatPath);
242
- if (entry) {
243
- if (entry.type === "component") {
244
- componentEntries.push(entry);
245
- } else if (entry.type === "page") {
246
- pageEntries.push(entry);
247
- }
248
- }
249
- }
250
- }
251
- return {
252
- pages: pageEntries,
253
- components: componentEntries,
254
- subPackageEntries
255
- };
256
- } else {
257
- const appEntry = searchAppEntry({
258
- root: path3.join(root, srcRoot),
259
- formatPath
260
- });
261
- if (appEntry) {
262
- const subPackageDeps = appEntry.deps.filter((x) => x.type === "subPackage");
263
- const filter = createFilter(
264
- [path3.join(root, srcRoot, "**/*")],
265
- [
266
- ...defaultExcluded,
267
- path3.join(root, `${outDir}/**`),
268
- ...subPackageDeps.map((x) => {
269
- return path3.join(root, `${x.root}/**`);
270
- })
271
- ],
272
- { cwd: root }
273
- );
274
- const pageEntries = [];
275
- const componentEntries = [];
276
- const files = await new Fdir().withFullPaths().filter(filter).crawl(path3.join(root, srcRoot)).withPromise();
277
- for (const file of files) {
278
- if (/\.wxml$/.test(file)) {
279
- const entry = getWxmlEntry(file, formatPath);
280
- if (entry) {
281
- if (entry.type === "component") {
282
- componentEntries.push(entry);
283
- } else if (entry.type === "page") {
284
- pageEntries.push(entry);
285
- }
286
- }
287
- }
288
- }
289
- return {
290
- app: appEntry,
291
- pages: pageEntries,
292
- components: componentEntries,
293
- subPackages: subPackageDeps
294
- };
120
+ async function findJsEntry(filepath) {
121
+ for (const ext of jsExtensions) {
122
+ const p = changeFileExtension(filepath, ext);
123
+ if (await fs4.exists(p)) {
124
+ return p;
295
125
  }
296
126
  }
297
127
  }
@@ -311,9 +141,6 @@ function parseRequest(id) {
311
141
 
312
142
  // src/plugins/index.ts
313
143
  var debug = createDebugger("weapp-vite:plugin");
314
- function normalizeCssPath(id) {
315
- return addExtension2(removeExtension2(id), ".wxss");
316
- }
317
144
  function isJsOrTs(name) {
318
145
  if (typeof name === "string") {
319
146
  return jsExtensions.some((x) => name.endsWith(`.${x}`));
@@ -322,13 +149,12 @@ function isJsOrTs(name) {
322
149
  }
323
150
  function getRealPath(res) {
324
151
  if (res.query.wxss) {
325
- return addExtension2(removeExtension2(res.filename), ".wxss");
152
+ return changeFileExtension(res.filename, "wxss");
326
153
  }
327
154
  return res.filename;
328
155
  }
329
156
  function vitePluginWeapp(ctx) {
330
157
  let configResolved;
331
- let entriesSet = /* @__PURE__ */ new Set();
332
158
  function relative(p) {
333
159
  return path4.relative(configResolved.root, p);
334
160
  }
@@ -338,7 +164,6 @@ function vitePluginWeapp(ctx) {
338
164
  return acc;
339
165
  }, {});
340
166
  }
341
- let appEntry;
342
167
  return [
343
168
  {
344
169
  name: "weapp-vite:pre",
@@ -353,68 +178,26 @@ function vitePluginWeapp(ctx) {
353
178
  configResolved = config;
354
179
  },
355
180
  async options(options) {
356
- const { root, build: build2, weapp } = configResolved;
357
- const entries = await getEntries({
358
- root,
359
- outDir: build2.outDir,
360
- srcRoot: weapp?.srcRoot,
361
- subPackage: weapp?.subPackage
362
- });
363
- if (entries) {
364
- const paths = [];
365
- if (entries.app) {
366
- paths.push(entries.app.path);
367
- appEntry = entries.app;
368
- }
369
- paths.push(...[...entries.pages, ...entries.components].map((x) => {
370
- return x.path;
371
- }));
372
- if (entries.subPackageEntries) {
373
- paths.push(...entries.subPackageEntries.map((x) => {
374
- return x.path;
375
- }));
376
- }
377
- const input = getInputOption(paths);
378
- entriesSet = new Set(paths);
379
- options.input = input;
380
- if (weapp?.type === "app" && Array.isArray(entries.subPackages) && entries.subPackages.length) {
381
- for (const subPackage of entries.subPackages) {
382
- if (subPackage.root && !ctx.subPackageContextMap.has(subPackage.root)) {
383
- ctx.forkSubPackage(subPackage).build();
384
- }
385
- }
386
- }
387
- } else {
388
- throw new Error(`\u5728 ${path4.join(root, ctx.srcRoot ?? "")} \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 `);
389
- }
181
+ await ctx.scanAppEntry();
182
+ const input = getInputOption([...ctx.entriesSet]);
183
+ options.input = input;
390
184
  },
391
185
  async buildStart() {
392
- const { root, build: build2, weapp } = configResolved;
393
- let cwd = root;
186
+ const { root, build: build2 } = configResolved;
187
+ const cwd = root;
394
188
  const ignore = [
395
189
  ...defaultExcluded
396
190
  ];
397
- const isSubPackage = weapp?.type === "subPackage";
398
- if (isSubPackage) {
399
- cwd = path4.join(root, ctx.subPackage.root);
400
- } else {
401
- const ignoreSubPackage = appEntry ? appEntry.deps.filter(
402
- (x) => x.type === "subPackage"
403
- ).map((x) => {
404
- return `${x.root}/**`;
405
- }) : [];
406
- ignore.push(
407
- ...[
408
- `${build2.outDir}/**`,
409
- ...ignoreSubPackage,
410
- "project.config.json",
411
- "project.private.config.json",
412
- "package.json",
413
- "tsconfig.json",
414
- "tsconfig.node.json"
415
- ]
416
- );
417
- }
191
+ ignore.push(
192
+ ...[
193
+ `${build2.outDir}/**`,
194
+ "project.config.json",
195
+ "project.private.config.json",
196
+ "package.json",
197
+ "tsconfig.json",
198
+ "tsconfig.node.json"
199
+ ]
200
+ );
418
201
  const files = await fg(
419
202
  // 假如去 join root 就是返回 absolute
420
203
  [path4.join(ctx.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
@@ -424,9 +207,7 @@ function vitePluginWeapp(ctx) {
424
207
  absolute: false
425
208
  }
426
209
  );
427
- const relFiles = files.map((x) => {
428
- return isSubPackage ? path4.join(weapp?.subPackage?.root ?? "", x) : x;
429
- });
210
+ const relFiles = files;
430
211
  for (const file of relFiles) {
431
212
  const filepath = path4.resolve(ctx.cwd, file);
432
213
  this.addWatchFile(filepath);
@@ -434,7 +215,7 @@ function vitePluginWeapp(ctx) {
434
215
  this.emitFile({
435
216
  type: "asset",
436
217
  fileName: ctx.relativeSrcRoot(file),
437
- source: isMedia ? await fs3.readFile(filepath) : await fs3.readFile(filepath, "utf8")
218
+ source: isMedia ? await fs5.readFile(filepath) : await fs5.readFile(filepath, "utf8")
438
219
  });
439
220
  }
440
221
  },
@@ -444,26 +225,25 @@ function vitePluginWeapp(ctx) {
444
225
  }
445
226
  },
446
227
  async load(id) {
447
- if (entriesSet.has(id)) {
228
+ if (ctx.entriesSet.has(id)) {
448
229
  const base = removeExtension2(id);
449
- const ms = new MagicString(fs3.readFileSync(id, "utf8"));
230
+ const ms = new MagicString(fs5.readFileSync(id, "utf8"));
450
231
  for (const ext of supportedCssExtensions) {
451
232
  const mayBeCssPath = addExtension2(base, ext);
452
- if (fs3.existsSync(mayBeCssPath)) {
233
+ if (fs5.existsSync(mayBeCssPath)) {
453
234
  this.addWatchFile(mayBeCssPath);
454
235
  ms.prepend(`import '${mayBeCssPath}'
455
236
  `);
456
237
  }
457
238
  }
458
- this.addWatchFile(id);
459
239
  return {
460
240
  code: ms.toString()
461
241
  };
462
242
  } else if (isCSSRequest(id)) {
463
243
  const parsed = parseRequest(id);
464
244
  const realPath = getRealPath(parsed);
465
- if (await fs3.exists(realPath)) {
466
- const css = await fs3.readFile(realPath, "utf8");
245
+ if (await fs5.exists(realPath)) {
246
+ const css = await fs5.readFile(realPath, "utf8");
467
247
  return {
468
248
  code: css
469
249
  };
@@ -479,7 +259,9 @@ function vitePluginWeapp(ctx) {
479
259
  for (const bundleKey of bundleKeys) {
480
260
  const asset = bundle[bundleKey];
481
261
  if (bundleKey.endsWith(".css") && asset.type === "asset" && typeof asset.originalFileName === "string" && isJsOrTs(asset.originalFileName)) {
482
- const newFileName = ctx.relativeSrcRoot(normalizeCssPath(asset.originalFileName));
262
+ const newFileName = ctx.relativeSrcRoot(
263
+ changeFileExtension(asset.originalFileName, "wxss")
264
+ );
483
265
  this.emitFile({
484
266
  type: "asset",
485
267
  fileName: newFileName,
@@ -517,9 +299,12 @@ var CompilerContext = class _CompilerContext {
517
299
  subPackageContextMap;
518
300
  type;
519
301
  parent;
302
+ entriesSet;
303
+ entries;
304
+ appEntry;
520
305
  constructor(options) {
521
- const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, subPackage, type } = defu3(options, {
522
- cwd: process2.cwd(),
306
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, subPackage, type } = defu2(options, {
307
+ cwd: process.cwd(),
523
308
  isDev: false,
524
309
  projectConfig: {},
525
310
  type: "app",
@@ -537,9 +322,11 @@ var CompilerContext = class _CompilerContext {
537
322
  this.watcherMap = /* @__PURE__ */ new Map();
538
323
  this.subPackageContextMap = /* @__PURE__ */ new Map();
539
324
  this.type = type;
325
+ this.entriesSet = /* @__PURE__ */ new Set();
326
+ this.entries = [];
540
327
  }
541
328
  get srcRoot() {
542
- return this.inlineConfig?.weapp?.srcRoot;
329
+ return this.inlineConfig?.weapp?.srcRoot ?? "";
543
330
  }
544
331
  relativeSrcRoot(p) {
545
332
  if (this.srcRoot) {
@@ -575,10 +362,10 @@ var CompilerContext = class _CompilerContext {
575
362
  return rollupWatcher;
576
363
  }
577
364
  async runDev() {
578
- if (process2.env.NODE_ENV === void 0) {
579
- process2.env.NODE_ENV = "development";
365
+ if (process.env.NODE_ENV === void 0) {
366
+ process.env.NODE_ENV = "development";
580
367
  }
581
- const inlineConfig = defu3(
368
+ const inlineConfig = defu2(
582
369
  this.inlineConfig,
583
370
  {
584
371
  root: this.cwd,
@@ -617,7 +404,7 @@ var CompilerContext = class _CompilerContext {
617
404
  subPackage: this.subPackage
618
405
  }
619
406
  });
620
- const { paths, ...opts } = defu3(
407
+ const { paths, ...opts } = defu2(
621
408
  subPackageInlineConfig.weapp?.watch,
622
409
  {
623
410
  cwd: path5.join(this.cwd, subPackageInlineConfig.weapp.srcRoot ?? "", this.subPackage.root)
@@ -628,7 +415,7 @@ var CompilerContext = class _CompilerContext {
628
415
  this.watcherMap.set(this.subPackage.root, watcher);
629
416
  return watcher;
630
417
  } else if (this.type === "app") {
631
- const { paths, ...opts } = defu3(
418
+ const { paths, ...opts } = defu2(
632
419
  inlineConfig.weapp?.watch,
633
420
  {
634
421
  ignored: [
@@ -644,7 +431,7 @@ var CompilerContext = class _CompilerContext {
644
431
  }
645
432
  }
646
433
  async runProd() {
647
- const inlineConfig = defu3(
434
+ const inlineConfig = defu2(
648
435
  this.inlineConfig,
649
436
  {
650
437
  root: this.cwd,
@@ -686,8 +473,8 @@ var CompilerContext = class _CompilerContext {
686
473
  this.projectConfig = projectConfig;
687
474
  const packageJsonPath = path5.resolve(this.cwd, "package.json");
688
475
  const external = [];
689
- if (await fs4.exists(packageJsonPath)) {
690
- const localPackageJson = await fs4.readJson(packageJsonPath, {
476
+ if (await fs6.exists(packageJsonPath)) {
477
+ const localPackageJson = await fs6.readJson(packageJsonPath, {
691
478
  throws: false
692
479
  }) || {};
693
480
  this.packageJson = localPackageJson;
@@ -699,7 +486,7 @@ var CompilerContext = class _CompilerContext {
699
486
  command: this.isDev ? "serve" : "build",
700
487
  mode: this.mode
701
488
  }, void 0, this.cwd);
702
- this.inlineConfig = defu3({
489
+ this.inlineConfig = defu2({
703
490
  mode: this.mode,
704
491
  build: {
705
492
  rollupOptions: {
@@ -738,7 +525,7 @@ var CompilerContext = class _CompilerContext {
738
525
  // miniprogram
739
526
  // 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
740
527
  async buildNpm(options) {
741
- const { sourcemap } = defu3(options, { sourcemap: true });
528
+ const { sourcemap } = defu2(options, { sourcemap: true });
742
529
  let packNpmRelationList = [];
743
530
  if (this.projectConfig.setting?.packNpmManually && Array.isArray(this.projectConfig.setting.packNpmRelationList)) {
744
531
  packNpmRelationList = this.projectConfig.setting.packNpmRelationList;
@@ -752,8 +539,8 @@ var CompilerContext = class _CompilerContext {
752
539
  }
753
540
  for (const relation of packNpmRelationList) {
754
541
  const packageJsonPath = path5.resolve(this.cwd, relation.packageJsonPath);
755
- if (await fs4.exists(packageJsonPath)) {
756
- const pkgJson = await fs4.readJson(packageJsonPath);
542
+ if (await fs6.exists(packageJsonPath)) {
543
+ const pkgJson = await fs6.readJson(packageJsonPath);
757
544
  const outDir = path5.resolve(this.cwd, relation.miniprogramNpmDistDir, "miniprogram_npm");
758
545
  if (pkgJson.dependencies) {
759
546
  const dependencies = Object.keys(pkgJson.dependencies);
@@ -763,7 +550,7 @@ var CompilerContext = class _CompilerContext {
763
550
  const targetJson = require2(id);
764
551
  if ("miniprogram" in targetJson && targetJson.miniprogram) {
765
552
  const targetJsonPath = require2.resolve(id);
766
- await fs4.copy(
553
+ await fs6.copy(
767
554
  path5.resolve(
768
555
  path5.dirname(targetJsonPath),
769
556
  targetJson.miniprogram
@@ -794,6 +581,91 @@ var CompilerContext = class _CompilerContext {
794
581
  }
795
582
  }
796
583
  }
584
+ async usingComponentsHandler(usingComponents, dirname) {
585
+ if (usingComponents) {
586
+ for (const componentUrl of Object.values(usingComponents)) {
587
+ if (/plugin:\/\//.test(componentUrl)) {
588
+ continue;
589
+ }
590
+ const tokens = componentUrl.split("/");
591
+ if (tokens[0] && isObject2(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
592
+ continue;
593
+ } else if (tokens[0] === "") {
594
+ await this.scanComponentEntry(componentUrl.substring(1), path5.resolve(this.cwd, this.srcRoot));
595
+ } else {
596
+ await this.scanComponentEntry(componentUrl, dirname);
597
+ }
598
+ }
599
+ }
600
+ }
601
+ async scanAppEntry() {
602
+ const appDirname = path5.resolve(this.cwd, this.srcRoot);
603
+ const appConfigFile = path5.resolve(appDirname, "app.json");
604
+ const appEntry = await findJsEntry(appConfigFile);
605
+ if (appEntry && await fs6.exists(appConfigFile)) {
606
+ const config = await readCommentJson(appConfigFile);
607
+ if (isObject2(config)) {
608
+ this.entriesSet.add(appEntry);
609
+ this.appEntry = {
610
+ path: appEntry,
611
+ json: config,
612
+ jsonPath: appConfigFile
613
+ };
614
+ this.entries.push(this.appEntry);
615
+ const { pages, usingComponents, subpackages = [], subPackages = [] } = config;
616
+ const subs = [...subpackages, ...subPackages];
617
+ await this.usingComponentsHandler(usingComponents, appDirname);
618
+ if (Array.isArray(pages)) {
619
+ for (const page of pages) {
620
+ await this.scanComponentEntry(page, appDirname);
621
+ }
622
+ }
623
+ for (const sub of subs) {
624
+ if (Array.isArray(sub.pages)) {
625
+ for (const page of sub.pages) {
626
+ await this.scanComponentEntry(path5.join(sub.root, page), appDirname);
627
+ }
628
+ }
629
+ if (sub.entry) {
630
+ await this.scanComponentEntry(path5.join(sub.root, sub.entry), appDirname);
631
+ }
632
+ }
633
+ }
634
+ } else {
635
+ 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 `);
636
+ }
637
+ }
638
+ // usingComponents
639
+ // subpackages / subPackages
640
+ // pages
641
+ // https://developers.weixin.qq.com/miniprogram/dev/framework/structure.html
642
+ // 页面可以没有 JSON
643
+ async scanComponentEntry(componentEntry, dirname) {
644
+ const entry = path5.resolve(dirname, componentEntry);
645
+ const jsEntry = await findJsEntry(entry);
646
+ if (jsEntry) {
647
+ this.entriesSet.add(jsEntry);
648
+ }
649
+ const configFile = changeFileExtension(entry, "json");
650
+ if (await fs6.exists(configFile)) {
651
+ const config = await readCommentJson(configFile);
652
+ if (jsEntry) {
653
+ this.entries.push({
654
+ path: jsEntry,
655
+ json: config,
656
+ jsonPath: configFile
657
+ });
658
+ }
659
+ if (isObject2(config)) {
660
+ const { usingComponents } = config;
661
+ await this.usingComponentsHandler(usingComponents, path5.dirname(configFile));
662
+ }
663
+ } else if (jsEntry) {
664
+ this.entries.push({
665
+ path: jsEntry
666
+ });
667
+ }
668
+ }
797
669
  };
798
670
 
799
671
  export {