weapp-vite 1.3.0 → 1.3.2

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,7 +3,7 @@ import logger from "@weapp-core/logger";
3
3
  var logger_default = logger;
4
4
 
5
5
  // package.json
6
- var version = "1.3.0";
6
+ var version = "1.3.2";
7
7
 
8
8
  // src/constants.ts
9
9
  var VERSION = version;
@@ -35,7 +35,7 @@ function getWeappWatchOptions() {
35
35
 
36
36
  // src/plugins/index.ts
37
37
  import { addExtension as addExtension2, removeExtension as removeExtension2 } from "@weapp-core/shared";
38
- import fg from "fast-glob";
38
+ import { fdir as Fdir } from "fdir";
39
39
  import fs5 from "fs-extra";
40
40
  import MagicString from "magic-string";
41
41
  import path4 from "pathe";
@@ -55,7 +55,7 @@ import fs4 from "fs-extra";
55
55
  import path3 from "pathe";
56
56
 
57
57
  // src/utils/json.ts
58
- import { parse as parseJson } from "comment-json";
58
+ import { parse as parseJson, stringify } from "comment-json";
59
59
  import fs from "fs-extra";
60
60
  function parseCommentJson(json) {
61
61
  return parseJson(json, void 0, true);
@@ -67,6 +67,12 @@ async function readCommentJson(filepath) {
67
67
  logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
68
68
  }
69
69
  }
70
+ function stringifyJson(value) {
71
+ return stringify(value, void 0, 2);
72
+ }
73
+ function resolveJson(value) {
74
+ return stringifyJson(value);
75
+ }
70
76
 
71
77
  // src/utils/projectConfig.ts
72
78
  import fs2 from "fs-extra";
@@ -103,6 +109,12 @@ import fs3 from "fs-extra";
103
109
  import path2 from "pathe";
104
110
 
105
111
  // src/utils/index.ts
112
+ function isJsOrTs(name) {
113
+ if (typeof name === "string") {
114
+ return jsExtensions.some((x) => name.endsWith(`.${x}`));
115
+ }
116
+ return false;
117
+ }
106
118
  function changeFileExtension(filePath, extension) {
107
119
  if (typeof filePath !== "string") {
108
120
  throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`);
@@ -138,21 +150,15 @@ function parseRequest(id) {
138
150
  query
139
151
  };
140
152
  }
141
-
142
- // src/plugins/index.ts
143
- var debug = createDebugger("weapp-vite:plugin");
144
- function isJsOrTs(name) {
145
- if (typeof name === "string") {
146
- return jsExtensions.some((x) => name.endsWith(`.${x}`));
147
- }
148
- return false;
149
- }
150
- function getRealPath(res) {
153
+ function getCssRealPath(res) {
151
154
  if (res.query.wxss) {
152
155
  return changeFileExtension(res.filename, "wxss");
153
156
  }
154
157
  return res.filename;
155
158
  }
159
+
160
+ // src/plugins/index.ts
161
+ var debug = createDebugger("weapp-vite:plugin");
156
162
  function vitePluginWeapp(ctx) {
157
163
  let configResolved;
158
164
  function relative(p) {
@@ -183,8 +189,7 @@ function vitePluginWeapp(ctx) {
183
189
  options.input = input;
184
190
  },
185
191
  async buildStart() {
186
- const { root, build: build2 } = configResolved;
187
- const cwd = root;
192
+ const { build: build2 } = configResolved;
188
193
  const ignore = [
189
194
  ...defaultExcluded
190
195
  ];
@@ -198,26 +203,36 @@ function vitePluginWeapp(ctx) {
198
203
  "tsconfig.node.json"
199
204
  ]
200
205
  );
201
- const files = await fg(
202
- // 假如去 join root 就是返回 absolute
203
- [path4.join(ctx.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
206
+ const relFiles = await new Fdir().withRelativePaths().globWithOptions(
207
+ [path4.join(ctx.srcRoot ?? "", "**/*.{wxml,wxs,png,jpg,jpeg,gif,svg,webp}")],
204
208
  {
205
- cwd,
206
- ignore,
207
- absolute: false
209
+ cwd: ctx.cwd,
210
+ ignore
208
211
  }
209
- );
210
- const relFiles = files;
212
+ ).crawl(ctx.cwd).withPromise();
211
213
  for (const file of relFiles) {
212
214
  const filepath = path4.resolve(ctx.cwd, file);
213
215
  this.addWatchFile(filepath);
214
- const isMedia = !/\.(?:wxml|json|wxs)$/.test(file);
216
+ const isMedia = !/\.(?:wxml|wxs)$/.test(file);
215
217
  this.emitFile({
216
218
  type: "asset",
217
219
  fileName: ctx.relativeSrcRoot(file),
218
220
  source: isMedia ? await fs5.readFile(filepath) : await fs5.readFile(filepath, "utf8")
219
221
  });
220
222
  }
223
+ for (const entry of ctx.entries) {
224
+ if (entry.jsonPath) {
225
+ this.addWatchFile(entry.jsonPath);
226
+ if (entry.json) {
227
+ const fileName = ctx.relativeSrcRoot(path4.relative(ctx.cwd, entry.jsonPath));
228
+ this.emitFile({
229
+ type: "asset",
230
+ fileName,
231
+ source: resolveJson(entry.json)
232
+ });
233
+ }
234
+ }
235
+ }
221
236
  },
222
237
  resolveId(source) {
223
238
  if (/\.wxss$/.test(source)) {
@@ -241,7 +256,7 @@ function vitePluginWeapp(ctx) {
241
256
  };
242
257
  } else if (isCSSRequest(id)) {
243
258
  const parsed = parseRequest(id);
244
- const realPath = getRealPath(parsed);
259
+ const realPath = getCssRealPath(parsed);
245
260
  if (await fs5.exists(realPath)) {
246
261
  const css = await fs5.readFile(realPath, "utf8");
247
262
  return {
@@ -287,7 +302,10 @@ function vitePluginWeapp(ctx) {
287
302
 
288
303
  // src/context.ts
289
304
  var require2 = createRequire(import.meta.url);
290
- var CompilerContext = class _CompilerContext {
305
+ var CompilerContext = class {
306
+ /**
307
+ * loadDefaultConfig 的时候会被重新赋予
308
+ */
291
309
  inlineConfig;
292
310
  cwd;
293
311
  isDev;
@@ -337,20 +355,6 @@ var CompilerContext = class _CompilerContext {
337
355
  get mpDistRoot() {
338
356
  return this.projectConfig.miniprogramRoot || this.projectConfig.srcMiniprogramRoot || "";
339
357
  }
340
- forkSubPackage(subPackage) {
341
- const ctx = new _CompilerContext({
342
- cwd: this.cwd,
343
- isDev: this.isDev,
344
- projectConfig: this.projectConfig,
345
- inlineConfig: this.inlineConfig,
346
- type: "subPackage",
347
- mode: this.mode,
348
- subPackage
349
- });
350
- this.subPackageContextMap.set(subPackage.root, ctx);
351
- ctx.parent = this;
352
- return ctx;
353
- }
354
358
  async internalDev(inlineConfig) {
355
359
  const rollupWatcher = await build(
356
360
  inlineConfig
@@ -393,6 +397,8 @@ var CompilerContext = class _CompilerContext {
393
397
  }).on("ready", async () => {
394
398
  await this.internalDev(inlineConfig2);
395
399
  isReady = true;
400
+ logger_default.success("\u5E94\u7528\u6784\u5EFA\u5B8C\u6210\uFF01");
401
+ logger_default.success("\u6267\u884C `npm run open` \u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u6216\u8005\u76F4\u63A5\u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u5BFC\u5165\u6839\u76EE\u5F55( `project.config.json` \u6240\u5728\u76EE\u5F55) \u67E5\u770B\u6548\u679C");
396
402
  });
397
403
  return watcher;
398
404
  };
@@ -442,6 +448,7 @@ var CompilerContext = class _CompilerContext {
442
448
  }
443
449
  }
444
450
  );
451
+ inlineConfig.logLevel = "info";
445
452
  if (this.type === "subPackage" && this.subPackage) {
446
453
  const subPackageInlineConfig = Object.assign({}, inlineConfig, {
447
454
  weapp: {
@@ -487,6 +494,8 @@ var CompilerContext = class _CompilerContext {
487
494
  mode: this.mode
488
495
  }, void 0, this.cwd);
489
496
  this.inlineConfig = defu2({
497
+ configFile: false
498
+ }, loaded?.config, {
490
499
  mode: this.mode,
491
500
  build: {
492
501
  rollupOptions: {
@@ -513,12 +522,11 @@ var CompilerContext = class _CompilerContext {
513
522
  include: void 0
514
523
  }
515
524
  },
516
- logLevel: "info",
517
525
  plugins: [
518
526
  tsconfigPaths()
519
527
  ],
520
- configFile: false
521
- }, loaded?.config, this.inlineConfig);
528
+ logLevel: "warn"
529
+ });
522
530
  }
523
531
  // https://cn.vitejs.dev/guide/build.html#library-mode
524
532
  // miniprogram_dist
@@ -572,6 +580,7 @@ var CompilerContext = class _CompilerContext {
572
580
  };
573
581
  },
574
582
  sourcemap
583
+ // clean: false,
575
584
  });
576
585
  }
577
586
  logger_default.success(`${dep} \u4F9D\u8D56\u5904\u7406\u5B8C\u6210!`);
@@ -598,7 +607,12 @@ var CompilerContext = class _CompilerContext {
598
607
  }
599
608
  }
600
609
  }
610
+ resetEntries() {
611
+ this.entriesSet.clear();
612
+ this.entries.length = 0;
613
+ }
601
614
  async scanAppEntry() {
615
+ this.resetEntries();
602
616
  const appDirname = path5.resolve(this.cwd, this.srcRoot);
603
617
  const appConfigFile = path5.resolve(appDirname, "app.json");
604
618
  const appEntry = await findJsEntry(appConfigFile);
@@ -667,9 +681,15 @@ var CompilerContext = class _CompilerContext {
667
681
  }
668
682
  }
669
683
  };
684
+ async function createCompilerContext(options) {
685
+ const ctx = new CompilerContext(options);
686
+ await ctx.loadDefaultConfig();
687
+ return ctx;
688
+ }
670
689
 
671
690
  export {
672
691
  logger_default,
673
692
  VERSION,
674
- CompilerContext
693
+ CompilerContext,
694
+ createCompilerContext
675
695
  };
package/dist/cli.cjs CHANGED
@@ -32,7 +32,7 @@ var import_cac = require("cac");
32
32
  var import_weapp_ide_cli = require("weapp-ide-cli");
33
33
 
34
34
  // package.json
35
- var version = "1.3.0";
35
+ var version = "1.3.2";
36
36
 
37
37
  // src/constants.ts
38
38
  var VERSION = version;
@@ -68,7 +68,7 @@ var logger_default = import_logger.default;
68
68
 
69
69
  // src/plugins/index.ts
70
70
  var import_shared2 = require("@weapp-core/shared");
71
- var import_fast_glob = __toESM(require("fast-glob"), 1);
71
+ var import_fdir = require("fdir");
72
72
  var import_fs_extra5 = __toESM(require("fs-extra"), 1);
73
73
  var import_magic_string = __toESM(require("magic-string"), 1);
74
74
  var import_pathe4 = __toESM(require("pathe"), 1);
@@ -100,6 +100,12 @@ async function readCommentJson(filepath) {
100
100
  logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
101
101
  }
102
102
  }
103
+ function stringifyJson(value) {
104
+ return (0, import_comment_json.stringify)(value, void 0, 2);
105
+ }
106
+ function resolveJson(value) {
107
+ return stringifyJson(value);
108
+ }
103
109
 
104
110
  // src/utils/projectConfig.ts
105
111
  var import_fs_extra2 = __toESM(require("fs-extra"), 1);
@@ -136,6 +142,12 @@ var import_fs_extra3 = __toESM(require("fs-extra"), 1);
136
142
  var import_pathe2 = __toESM(require("pathe"), 1);
137
143
 
138
144
  // src/utils/index.ts
145
+ function isJsOrTs(name) {
146
+ if (typeof name === "string") {
147
+ return jsExtensions.some((x) => name.endsWith(`.${x}`));
148
+ }
149
+ return false;
150
+ }
139
151
  function changeFileExtension(filePath, extension) {
140
152
  if (typeof filePath !== "string") {
141
153
  throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`);
@@ -171,21 +183,15 @@ function parseRequest(id) {
171
183
  query
172
184
  };
173
185
  }
174
-
175
- // src/plugins/index.ts
176
- var debug = createDebugger("weapp-vite:plugin");
177
- function isJsOrTs(name) {
178
- if (typeof name === "string") {
179
- return jsExtensions.some((x) => name.endsWith(`.${x}`));
180
- }
181
- return false;
182
- }
183
- function getRealPath(res) {
186
+ function getCssRealPath(res) {
184
187
  if (res.query.wxss) {
185
188
  return changeFileExtension(res.filename, "wxss");
186
189
  }
187
190
  return res.filename;
188
191
  }
192
+
193
+ // src/plugins/index.ts
194
+ var debug = createDebugger("weapp-vite:plugin");
189
195
  function vitePluginWeapp(ctx) {
190
196
  let configResolved;
191
197
  function relative(p) {
@@ -216,8 +222,7 @@ function vitePluginWeapp(ctx) {
216
222
  options.input = input;
217
223
  },
218
224
  async buildStart() {
219
- const { root, build: build2 } = configResolved;
220
- const cwd = root;
225
+ const { build: build2 } = configResolved;
221
226
  const ignore = [
222
227
  ...defaultExcluded
223
228
  ];
@@ -231,26 +236,36 @@ function vitePluginWeapp(ctx) {
231
236
  "tsconfig.node.json"
232
237
  ]
233
238
  );
234
- const files = await (0, import_fast_glob.default)(
235
- // 假如去 join root 就是返回 absolute
236
- [import_pathe4.default.join(ctx.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
239
+ const relFiles = await new import_fdir.fdir().withRelativePaths().globWithOptions(
240
+ [import_pathe4.default.join(ctx.srcRoot ?? "", "**/*.{wxml,wxs,png,jpg,jpeg,gif,svg,webp}")],
237
241
  {
238
- cwd,
239
- ignore,
240
- absolute: false
242
+ cwd: ctx.cwd,
243
+ ignore
241
244
  }
242
- );
243
- const relFiles = files;
245
+ ).crawl(ctx.cwd).withPromise();
244
246
  for (const file of relFiles) {
245
247
  const filepath = import_pathe4.default.resolve(ctx.cwd, file);
246
248
  this.addWatchFile(filepath);
247
- const isMedia = !/\.(?:wxml|json|wxs)$/.test(file);
249
+ const isMedia = !/\.(?:wxml|wxs)$/.test(file);
248
250
  this.emitFile({
249
251
  type: "asset",
250
252
  fileName: ctx.relativeSrcRoot(file),
251
253
  source: isMedia ? await import_fs_extra5.default.readFile(filepath) : await import_fs_extra5.default.readFile(filepath, "utf8")
252
254
  });
253
255
  }
256
+ for (const entry of ctx.entries) {
257
+ if (entry.jsonPath) {
258
+ this.addWatchFile(entry.jsonPath);
259
+ if (entry.json) {
260
+ const fileName = ctx.relativeSrcRoot(import_pathe4.default.relative(ctx.cwd, entry.jsonPath));
261
+ this.emitFile({
262
+ type: "asset",
263
+ fileName,
264
+ source: resolveJson(entry.json)
265
+ });
266
+ }
267
+ }
268
+ }
254
269
  },
255
270
  resolveId(source) {
256
271
  if (/\.wxss$/.test(source)) {
@@ -274,7 +289,7 @@ function vitePluginWeapp(ctx) {
274
289
  };
275
290
  } else if ((0, import_vite.isCSSRequest)(id)) {
276
291
  const parsed = parseRequest(id);
277
- const realPath = getRealPath(parsed);
292
+ const realPath = getCssRealPath(parsed);
278
293
  if (await import_fs_extra5.default.exists(realPath)) {
279
294
  const css = await import_fs_extra5.default.readFile(realPath, "utf8");
280
295
  return {
@@ -320,7 +335,10 @@ function vitePluginWeapp(ctx) {
320
335
 
321
336
  // src/context.ts
322
337
  var require2 = (0, import_node_module.createRequire)(importMetaUrl);
323
- var CompilerContext = class _CompilerContext {
338
+ var CompilerContext = class {
339
+ /**
340
+ * loadDefaultConfig 的时候会被重新赋予
341
+ */
324
342
  inlineConfig;
325
343
  cwd;
326
344
  isDev;
@@ -370,20 +388,6 @@ var CompilerContext = class _CompilerContext {
370
388
  get mpDistRoot() {
371
389
  return this.projectConfig.miniprogramRoot || this.projectConfig.srcMiniprogramRoot || "";
372
390
  }
373
- forkSubPackage(subPackage) {
374
- const ctx = new _CompilerContext({
375
- cwd: this.cwd,
376
- isDev: this.isDev,
377
- projectConfig: this.projectConfig,
378
- inlineConfig: this.inlineConfig,
379
- type: "subPackage",
380
- mode: this.mode,
381
- subPackage
382
- });
383
- this.subPackageContextMap.set(subPackage.root, ctx);
384
- ctx.parent = this;
385
- return ctx;
386
- }
387
391
  async internalDev(inlineConfig) {
388
392
  const rollupWatcher = await (0, import_vite2.build)(
389
393
  inlineConfig
@@ -426,6 +430,8 @@ var CompilerContext = class _CompilerContext {
426
430
  }).on("ready", async () => {
427
431
  await this.internalDev(inlineConfig2);
428
432
  isReady = true;
433
+ logger_default.success("\u5E94\u7528\u6784\u5EFA\u5B8C\u6210\uFF01");
434
+ logger_default.success("\u6267\u884C `npm run open` \u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u6216\u8005\u76F4\u63A5\u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u5BFC\u5165\u6839\u76EE\u5F55( `project.config.json` \u6240\u5728\u76EE\u5F55) \u67E5\u770B\u6548\u679C");
429
435
  });
430
436
  return watcher;
431
437
  };
@@ -475,6 +481,7 @@ var CompilerContext = class _CompilerContext {
475
481
  }
476
482
  }
477
483
  );
484
+ inlineConfig.logLevel = "info";
478
485
  if (this.type === "subPackage" && this.subPackage) {
479
486
  const subPackageInlineConfig = Object.assign({}, inlineConfig, {
480
487
  weapp: {
@@ -520,6 +527,8 @@ var CompilerContext = class _CompilerContext {
520
527
  mode: this.mode
521
528
  }, void 0, this.cwd);
522
529
  this.inlineConfig = (0, import_shared3.defu)({
530
+ configFile: false
531
+ }, loaded?.config, {
523
532
  mode: this.mode,
524
533
  build: {
525
534
  rollupOptions: {
@@ -546,12 +555,11 @@ var CompilerContext = class _CompilerContext {
546
555
  include: void 0
547
556
  }
548
557
  },
549
- logLevel: "info",
550
558
  plugins: [
551
559
  (0, import_vite_tsconfig_paths.default)()
552
560
  ],
553
- configFile: false
554
- }, loaded?.config, this.inlineConfig);
561
+ logLevel: "warn"
562
+ });
555
563
  }
556
564
  // https://cn.vitejs.dev/guide/build.html#library-mode
557
565
  // miniprogram_dist
@@ -605,6 +613,7 @@ var CompilerContext = class _CompilerContext {
605
613
  };
606
614
  },
607
615
  sourcemap
616
+ // clean: false,
608
617
  });
609
618
  }
610
619
  logger_default.success(`${dep} \u4F9D\u8D56\u5904\u7406\u5B8C\u6210!`);
@@ -631,7 +640,12 @@ var CompilerContext = class _CompilerContext {
631
640
  }
632
641
  }
633
642
  }
643
+ resetEntries() {
644
+ this.entriesSet.clear();
645
+ this.entries.length = 0;
646
+ }
634
647
  async scanAppEntry() {
648
+ this.resetEntries();
635
649
  const appDirname = import_pathe5.default.resolve(this.cwd, this.srcRoot);
636
650
  const appConfigFile = import_pathe5.default.resolve(appDirname, "app.json");
637
651
  const appEntry = await findJsEntry(appConfigFile);
@@ -700,6 +714,11 @@ var CompilerContext = class _CompilerContext {
700
714
  }
701
715
  }
702
716
  };
717
+ async function createCompilerContext(options) {
718
+ const ctx = new CompilerContext(options);
719
+ await ctx.loadDefaultConfig();
720
+ return ctx;
721
+ }
703
722
 
704
723
  // src/cli.ts
705
724
  var cli = (0, import_cac.cac)("weapp-vite");
@@ -721,12 +740,11 @@ cli.option("-c, --config <file>", `[string] use specified config file`).option("
721
740
  }).option("-l, --logLevel <level>", `[string] info | warn | error | silent`).option("--clearScreen", `[boolean] allow/disable clear screen when logging`).option("-d, --debug [feat]", `[string | boolean] show debug logs`).option("-f, --filter <filter>", `[string] filter debug logs`).option("-m, --mode <mode>", `[string] set env mode`);
722
741
  cli.command("[root]", "start dev server").alias("serve").alias("dev").option("--skipNpm", `[boolean] if skip npm build`).action(async (root, options) => {
723
742
  filterDuplicateOptions(options);
724
- const ctx = new CompilerContext({
743
+ const ctx = await createCompilerContext({
725
744
  cwd: root,
726
745
  mode: options.mode,
727
746
  isDev: true
728
747
  });
729
- await ctx.loadDefaultConfig();
730
748
  if (!options.skipNpm) {
731
749
  await ctx.buildNpm();
732
750
  }
@@ -743,11 +761,10 @@ cli.command("build [root]", "build for production").option("--target <target>",
743
761
  `[boolean] force empty outDir when it's outside of root`
744
762
  ).option("-w, --watch", `[boolean] rebuilds when modules have changed on disk`).option("--skipNpm", `[boolean] if skip npm build`).action(async (root, options) => {
745
763
  filterDuplicateOptions(options);
746
- const ctx = new CompilerContext({
764
+ const ctx = await createCompilerContext({
747
765
  cwd: root,
748
766
  mode: options.mode
749
767
  });
750
- await ctx.loadDefaultConfig();
751
768
  await ctx.runProd();
752
769
  if (!options.skipNpm) {
753
770
  await ctx.buildNpm({ sourcemap: false });
package/dist/cli.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
- CompilerContext,
3
2
  VERSION,
3
+ createCompilerContext,
4
4
  logger_default
5
- } from "./chunk-K4VNFHJL.mjs";
5
+ } from "./chunk-455ARQ2V.mjs";
6
6
  import "./chunk-444MQSSG.mjs";
7
7
 
8
8
  // src/cli.ts
@@ -28,12 +28,11 @@ cli.option("-c, --config <file>", `[string] use specified config file`).option("
28
28
  }).option("-l, --logLevel <level>", `[string] info | warn | error | silent`).option("--clearScreen", `[boolean] allow/disable clear screen when logging`).option("-d, --debug [feat]", `[string | boolean] show debug logs`).option("-f, --filter <filter>", `[string] filter debug logs`).option("-m, --mode <mode>", `[string] set env mode`);
29
29
  cli.command("[root]", "start dev server").alias("serve").alias("dev").option("--skipNpm", `[boolean] if skip npm build`).action(async (root, options) => {
30
30
  filterDuplicateOptions(options);
31
- const ctx = new CompilerContext({
31
+ const ctx = await createCompilerContext({
32
32
  cwd: root,
33
33
  mode: options.mode,
34
34
  isDev: true
35
35
  });
36
- await ctx.loadDefaultConfig();
37
36
  if (!options.skipNpm) {
38
37
  await ctx.buildNpm();
39
38
  }
@@ -50,11 +49,10 @@ cli.command("build [root]", "build for production").option("--target <target>",
50
49
  `[boolean] force empty outDir when it's outside of root`
51
50
  ).option("-w, --watch", `[boolean] rebuilds when modules have changed on disk`).option("--skipNpm", `[boolean] if skip npm build`).action(async (root, options) => {
52
51
  filterDuplicateOptions(options);
53
- const ctx = new CompilerContext({
52
+ const ctx = await createCompilerContext({
54
53
  cwd: root,
55
54
  mode: options.mode
56
55
  });
57
- await ctx.loadDefaultConfig();
58
56
  await ctx.runProd();
59
57
  if (!options.skipNpm) {
60
58
  await ctx.buildNpm({ sourcemap: false });
package/dist/index.cjs CHANGED
@@ -30,7 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
- CompilerContext: () => CompilerContext
33
+ CompilerContext: () => CompilerContext,
34
+ createCompilerContext: () => createCompilerContext
34
35
  });
35
36
  module.exports = __toCommonJS(src_exports);
36
37
 
@@ -66,7 +67,7 @@ var logger_default = import_logger.default;
66
67
 
67
68
  // src/plugins/index.ts
68
69
  var import_shared2 = require("@weapp-core/shared");
69
- var import_fast_glob = __toESM(require("fast-glob"), 1);
70
+ var import_fdir = require("fdir");
70
71
  var import_fs_extra5 = __toESM(require("fs-extra"), 1);
71
72
  var import_magic_string = __toESM(require("magic-string"), 1);
72
73
  var import_pathe4 = __toESM(require("pathe"), 1);
@@ -103,6 +104,12 @@ async function readCommentJson(filepath) {
103
104
  logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
104
105
  }
105
106
  }
107
+ function stringifyJson(value) {
108
+ return (0, import_comment_json.stringify)(value, void 0, 2);
109
+ }
110
+ function resolveJson(value) {
111
+ return stringifyJson(value);
112
+ }
106
113
 
107
114
  // src/utils/projectConfig.ts
108
115
  var import_fs_extra2 = __toESM(require("fs-extra"), 1);
@@ -139,6 +146,12 @@ var import_fs_extra3 = __toESM(require("fs-extra"), 1);
139
146
  var import_pathe2 = __toESM(require("pathe"), 1);
140
147
 
141
148
  // src/utils/index.ts
149
+ function isJsOrTs(name) {
150
+ if (typeof name === "string") {
151
+ return jsExtensions.some((x) => name.endsWith(`.${x}`));
152
+ }
153
+ return false;
154
+ }
142
155
  function changeFileExtension(filePath, extension) {
143
156
  if (typeof filePath !== "string") {
144
157
  throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`);
@@ -174,21 +187,15 @@ function parseRequest(id) {
174
187
  query
175
188
  };
176
189
  }
177
-
178
- // src/plugins/index.ts
179
- var debug = createDebugger("weapp-vite:plugin");
180
- function isJsOrTs(name) {
181
- if (typeof name === "string") {
182
- return jsExtensions.some((x) => name.endsWith(`.${x}`));
183
- }
184
- return false;
185
- }
186
- function getRealPath(res) {
190
+ function getCssRealPath(res) {
187
191
  if (res.query.wxss) {
188
192
  return changeFileExtension(res.filename, "wxss");
189
193
  }
190
194
  return res.filename;
191
195
  }
196
+
197
+ // src/plugins/index.ts
198
+ var debug = createDebugger("weapp-vite:plugin");
192
199
  function vitePluginWeapp(ctx) {
193
200
  let configResolved;
194
201
  function relative(p) {
@@ -219,8 +226,7 @@ function vitePluginWeapp(ctx) {
219
226
  options.input = input;
220
227
  },
221
228
  async buildStart() {
222
- const { root, build: build2 } = configResolved;
223
- const cwd = root;
229
+ const { build: build2 } = configResolved;
224
230
  const ignore = [
225
231
  ...defaultExcluded
226
232
  ];
@@ -234,26 +240,36 @@ function vitePluginWeapp(ctx) {
234
240
  "tsconfig.node.json"
235
241
  ]
236
242
  );
237
- const files = await (0, import_fast_glob.default)(
238
- // 假如去 join root 就是返回 absolute
239
- [import_pathe4.default.join(ctx.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
243
+ const relFiles = await new import_fdir.fdir().withRelativePaths().globWithOptions(
244
+ [import_pathe4.default.join(ctx.srcRoot ?? "", "**/*.{wxml,wxs,png,jpg,jpeg,gif,svg,webp}")],
240
245
  {
241
- cwd,
242
- ignore,
243
- absolute: false
246
+ cwd: ctx.cwd,
247
+ ignore
244
248
  }
245
- );
246
- const relFiles = files;
249
+ ).crawl(ctx.cwd).withPromise();
247
250
  for (const file of relFiles) {
248
251
  const filepath = import_pathe4.default.resolve(ctx.cwd, file);
249
252
  this.addWatchFile(filepath);
250
- const isMedia = !/\.(?:wxml|json|wxs)$/.test(file);
253
+ const isMedia = !/\.(?:wxml|wxs)$/.test(file);
251
254
  this.emitFile({
252
255
  type: "asset",
253
256
  fileName: ctx.relativeSrcRoot(file),
254
257
  source: isMedia ? await import_fs_extra5.default.readFile(filepath) : await import_fs_extra5.default.readFile(filepath, "utf8")
255
258
  });
256
259
  }
260
+ for (const entry of ctx.entries) {
261
+ if (entry.jsonPath) {
262
+ this.addWatchFile(entry.jsonPath);
263
+ if (entry.json) {
264
+ const fileName = ctx.relativeSrcRoot(import_pathe4.default.relative(ctx.cwd, entry.jsonPath));
265
+ this.emitFile({
266
+ type: "asset",
267
+ fileName,
268
+ source: resolveJson(entry.json)
269
+ });
270
+ }
271
+ }
272
+ }
257
273
  },
258
274
  resolveId(source) {
259
275
  if (/\.wxss$/.test(source)) {
@@ -277,7 +293,7 @@ function vitePluginWeapp(ctx) {
277
293
  };
278
294
  } else if ((0, import_vite.isCSSRequest)(id)) {
279
295
  const parsed = parseRequest(id);
280
- const realPath = getRealPath(parsed);
296
+ const realPath = getCssRealPath(parsed);
281
297
  if (await import_fs_extra5.default.exists(realPath)) {
282
298
  const css = await import_fs_extra5.default.readFile(realPath, "utf8");
283
299
  return {
@@ -323,7 +339,10 @@ function vitePluginWeapp(ctx) {
323
339
 
324
340
  // src/context.ts
325
341
  var require2 = (0, import_node_module.createRequire)(importMetaUrl);
326
- var CompilerContext = class _CompilerContext {
342
+ var CompilerContext = class {
343
+ /**
344
+ * loadDefaultConfig 的时候会被重新赋予
345
+ */
327
346
  inlineConfig;
328
347
  cwd;
329
348
  isDev;
@@ -373,20 +392,6 @@ var CompilerContext = class _CompilerContext {
373
392
  get mpDistRoot() {
374
393
  return this.projectConfig.miniprogramRoot || this.projectConfig.srcMiniprogramRoot || "";
375
394
  }
376
- forkSubPackage(subPackage) {
377
- const ctx = new _CompilerContext({
378
- cwd: this.cwd,
379
- isDev: this.isDev,
380
- projectConfig: this.projectConfig,
381
- inlineConfig: this.inlineConfig,
382
- type: "subPackage",
383
- mode: this.mode,
384
- subPackage
385
- });
386
- this.subPackageContextMap.set(subPackage.root, ctx);
387
- ctx.parent = this;
388
- return ctx;
389
- }
390
395
  async internalDev(inlineConfig) {
391
396
  const rollupWatcher = await (0, import_vite2.build)(
392
397
  inlineConfig
@@ -429,6 +434,8 @@ var CompilerContext = class _CompilerContext {
429
434
  }).on("ready", async () => {
430
435
  await this.internalDev(inlineConfig2);
431
436
  isReady = true;
437
+ logger_default.success("\u5E94\u7528\u6784\u5EFA\u5B8C\u6210\uFF01");
438
+ logger_default.success("\u6267\u884C `npm run open` \u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u6216\u8005\u76F4\u63A5\u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u5BFC\u5165\u6839\u76EE\u5F55( `project.config.json` \u6240\u5728\u76EE\u5F55) \u67E5\u770B\u6548\u679C");
432
439
  });
433
440
  return watcher;
434
441
  };
@@ -478,6 +485,7 @@ var CompilerContext = class _CompilerContext {
478
485
  }
479
486
  }
480
487
  );
488
+ inlineConfig.logLevel = "info";
481
489
  if (this.type === "subPackage" && this.subPackage) {
482
490
  const subPackageInlineConfig = Object.assign({}, inlineConfig, {
483
491
  weapp: {
@@ -523,6 +531,8 @@ var CompilerContext = class _CompilerContext {
523
531
  mode: this.mode
524
532
  }, void 0, this.cwd);
525
533
  this.inlineConfig = (0, import_shared3.defu)({
534
+ configFile: false
535
+ }, loaded?.config, {
526
536
  mode: this.mode,
527
537
  build: {
528
538
  rollupOptions: {
@@ -549,12 +559,11 @@ var CompilerContext = class _CompilerContext {
549
559
  include: void 0
550
560
  }
551
561
  },
552
- logLevel: "info",
553
562
  plugins: [
554
563
  (0, import_vite_tsconfig_paths.default)()
555
564
  ],
556
- configFile: false
557
- }, loaded?.config, this.inlineConfig);
565
+ logLevel: "warn"
566
+ });
558
567
  }
559
568
  // https://cn.vitejs.dev/guide/build.html#library-mode
560
569
  // miniprogram_dist
@@ -608,6 +617,7 @@ var CompilerContext = class _CompilerContext {
608
617
  };
609
618
  },
610
619
  sourcemap
620
+ // clean: false,
611
621
  });
612
622
  }
613
623
  logger_default.success(`${dep} \u4F9D\u8D56\u5904\u7406\u5B8C\u6210!`);
@@ -634,7 +644,12 @@ var CompilerContext = class _CompilerContext {
634
644
  }
635
645
  }
636
646
  }
647
+ resetEntries() {
648
+ this.entriesSet.clear();
649
+ this.entries.length = 0;
650
+ }
637
651
  async scanAppEntry() {
652
+ this.resetEntries();
638
653
  const appDirname = import_pathe5.default.resolve(this.cwd, this.srcRoot);
639
654
  const appConfigFile = import_pathe5.default.resolve(appDirname, "app.json");
640
655
  const appEntry = await findJsEntry(appConfigFile);
@@ -703,7 +718,13 @@ var CompilerContext = class _CompilerContext {
703
718
  }
704
719
  }
705
720
  };
721
+ async function createCompilerContext(options) {
722
+ const ctx = new CompilerContext(options);
723
+ await ctx.loadDefaultConfig();
724
+ return ctx;
725
+ }
706
726
  // Annotate the CommonJS export names for ESM import in node:
707
727
  0 && (module.exports = {
708
- CompilerContext
728
+ CompilerContext,
729
+ createCompilerContext
709
730
  });
package/dist/index.d.cts CHANGED
@@ -32,6 +32,9 @@ interface CompilerContextOptions {
32
32
  subPackage?: SubPackage;
33
33
  }
34
34
  declare class CompilerContext {
35
+ /**
36
+ * loadDefaultConfig 的时候会被重新赋予
37
+ */
35
38
  inlineConfig: InlineConfig;
36
39
  cwd: string;
37
40
  isDev: boolean;
@@ -50,7 +53,6 @@ declare class CompilerContext {
50
53
  get srcRoot(): string;
51
54
  relativeSrcRoot(p: string): string;
52
55
  get mpDistRoot(): string;
53
- forkSubPackage(subPackage: SubPackage): CompilerContext;
54
56
  private internalDev;
55
57
  runDev(): Promise<FSWatcher | undefined>;
56
58
  runProd(): Promise<RollupOutput | RollupOutput[] | undefined>;
@@ -60,8 +62,10 @@ declare class CompilerContext {
60
62
  sourcemap?: boolean;
61
63
  }): Promise<void>;
62
64
  private usingComponentsHandler;
65
+ resetEntries(): void;
63
66
  scanAppEntry(): Promise<void>;
64
67
  scanComponentEntry(componentEntry: string, dirname: string): Promise<void>;
65
68
  }
69
+ declare function createCompilerContext(options?: CompilerContextOptions): Promise<CompilerContext>;
66
70
 
67
- export { CompilerContext, type CompilerContextOptions, type Entry };
71
+ export { CompilerContext, type CompilerContextOptions, type Entry, createCompilerContext };
package/dist/index.d.ts CHANGED
@@ -32,6 +32,9 @@ interface CompilerContextOptions {
32
32
  subPackage?: SubPackage;
33
33
  }
34
34
  declare class CompilerContext {
35
+ /**
36
+ * loadDefaultConfig 的时候会被重新赋予
37
+ */
35
38
  inlineConfig: InlineConfig;
36
39
  cwd: string;
37
40
  isDev: boolean;
@@ -50,7 +53,6 @@ declare class CompilerContext {
50
53
  get srcRoot(): string;
51
54
  relativeSrcRoot(p: string): string;
52
55
  get mpDistRoot(): string;
53
- forkSubPackage(subPackage: SubPackage): CompilerContext;
54
56
  private internalDev;
55
57
  runDev(): Promise<FSWatcher | undefined>;
56
58
  runProd(): Promise<RollupOutput | RollupOutput[] | undefined>;
@@ -60,8 +62,10 @@ declare class CompilerContext {
60
62
  sourcemap?: boolean;
61
63
  }): Promise<void>;
62
64
  private usingComponentsHandler;
65
+ resetEntries(): void;
63
66
  scanAppEntry(): Promise<void>;
64
67
  scanComponentEntry(componentEntry: string, dirname: string): Promise<void>;
65
68
  }
69
+ declare function createCompilerContext(options?: CompilerContextOptions): Promise<CompilerContext>;
66
70
 
67
- export { CompilerContext, type CompilerContextOptions, type Entry };
71
+ export { CompilerContext, type CompilerContextOptions, type Entry, createCompilerContext };
package/dist/index.mjs CHANGED
@@ -1,7 +1,9 @@
1
1
  import {
2
- CompilerContext
3
- } from "./chunk-K4VNFHJL.mjs";
2
+ CompilerContext,
3
+ createCompilerContext
4
+ } from "./chunk-455ARQ2V.mjs";
4
5
  import "./chunk-444MQSSG.mjs";
5
6
  export {
6
- CompilerContext
7
+ CompilerContext,
8
+ createCompilerContext
7
9
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "weapp-vite",
3
3
  "type": "module",
4
- "version": "1.3.0",
4
+ "version": "1.3.2",
5
5
  "description": "weapp-vite 一个现代化的小程序打包工具",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -54,26 +54,25 @@
54
54
  "chokidar": "^3.6.0",
55
55
  "comment-json": "^4.2.5",
56
56
  "debug": "^4.3.7",
57
- "fast-glob": "^3.3.2",
58
57
  "fdir": "^6.4.0",
59
58
  "fs-extra": "^11.2.0",
60
- "htmlparser2": "^9.1.0",
61
59
  "magic-string": "^0.30.11",
62
- "micromatch": "^4.0.8",
63
60
  "pathe": "^1.1.2",
64
- "picocolors": "^1.1.0",
65
- "pkg-types": "^1.2.0",
61
+ "picomatch": "^4.0.2",
66
62
  "tsup": "^8.3.0",
67
63
  "vite-tsconfig-paths": "^5.0.1",
64
+ "@weapp-core/init": "^1.0.5",
68
65
  "@weapp-core/logger": "^1.0.1",
69
- "weapp-ide-cli": "^2.0.6",
70
66
  "@weapp-core/shared": "^1.0.1",
71
- "@weapp-core/init": "^1.0.5"
67
+ "weapp-ide-cli": "^2.0.6"
72
68
  },
73
69
  "publishConfig": {
74
70
  "access": "public",
75
71
  "registry": "https://registry.npmjs.org"
76
72
  },
73
+ "devDependencies": {
74
+ "htmlparser2": "^9.1.0"
75
+ },
77
76
  "scripts": {
78
77
  "dev": "tsup --watch --sourcemap",
79
78
  "build": "tsup",