weapp-vite 1.6.6 → 1.6.7

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/README.md CHANGED
@@ -35,3 +35,5 @@
35
35
  ## License
36
36
 
37
37
  [MIT](./LICENSE)
38
+
39
+ <!-- "//------":""esbuild": "^0.21.3",", -->
package/client.d.ts CHANGED
@@ -1 +1,18 @@
1
1
  /// <reference types="vite/client" />
2
+
3
+ type MP_PLATFORM = 'weapp'
4
+
5
+ interface ImportMetaEnv {
6
+ MP_PLATFORM: MP_PLATFORM // | 'swan' | 'alipay' | 'tt' | 'qq' | 'jd' | 'h5'
7
+ }
8
+ // MP_PLATFORM
9
+ // weapp / swan / alipay / tt / qq / jd / h5
10
+ declare module 'process' {
11
+ global {
12
+ namespace NodeJS {
13
+ interface ProcessEnv extends Dict<string> {
14
+ MP_PLATFORM: MP_PLATFORM
15
+ }
16
+ }
17
+ }
18
+ }
@@ -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.6.6";
6
+ var version = "1.6.7";
7
7
 
8
8
  // src/constants.ts
9
9
  var VERSION = version;
@@ -14,7 +14,7 @@ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
14
14
  // src/context.ts
15
15
  import { createRequire } from "node:module";
16
16
  import process from "node:process";
17
- import { addExtension, defu, get as get2, isObject as isObject2, objectHash, removeExtension } from "@weapp-core/shared";
17
+ import { addExtension, defu, get as get2, isObject as isObject3, objectHash, removeExtension } from "@weapp-core/shared";
18
18
  import fs5 from "fs-extra";
19
19
  import path5 from "pathe";
20
20
  import { build, loadConfigFromFile } from "vite";
@@ -31,8 +31,18 @@ function createDebugger(namespace) {
31
31
 
32
32
  // src/defaults.ts
33
33
  var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
34
+ function getOutputExtensions(_platform) {
35
+ return {
36
+ js: "js",
37
+ json: "json",
38
+ wxml: "wxml",
39
+ wxss: "wxss",
40
+ wxs: "wxs"
41
+ };
42
+ }
34
43
 
35
44
  // src/plugins/index.ts
45
+ import { isObject as isObject2 } from "@weapp-core/shared";
36
46
  import { fdir as Fdir } from "fdir";
37
47
  import fs4 from "fs-extra";
38
48
  import MagicString from "magic-string";
@@ -88,30 +98,37 @@ function resolveGlobs(globs, subPackageMeta) {
88
98
  }
89
99
 
90
100
  // src/utils/json.ts
91
- import { pathToFileURL } from "node:url";
92
101
  import { get, isObject, set } from "@weapp-core/shared";
102
+ import { bundleRequire } from "bundle-require";
93
103
  import { parse as parseJson, stringify } from "comment-json";
94
104
  import fs2 from "fs-extra";
95
105
  import path2 from "pathe";
96
- import { tsImport } from "tsx/esm/api";
97
106
  function parseCommentJson(json) {
98
107
  return parseJson(json, void 0, true);
99
108
  }
100
109
  function jsonFileRemoveJsExtension(fileName) {
101
110
  return fileName.replace(/\.[jt]s$/, "");
102
111
  }
103
- async function readCommentJson(filepath) {
104
- try {
105
- if (/\.json\.[jt]s$/.test(filepath)) {
106
- const loaded = await tsImport(pathToFileURL(filepath).href, import.meta.url);
107
- return loaded.default;
108
- } else {
109
- return parseCommentJson(await fs2.readFile(filepath, "utf8"));
112
+ function createReadCommentJson(ctx) {
113
+ return async function readCommentJson(filepath) {
114
+ try {
115
+ if (/\.json\.[jt]s$/.test(filepath)) {
116
+ const { mod } = await bundleRequire({
117
+ filepath,
118
+ cwd: ctx?.cwd,
119
+ esbuildOptions: {
120
+ define: ctx?.define
121
+ }
122
+ });
123
+ return typeof mod.default === "function" ? await mod.default(ctx) : mod.default;
124
+ } else {
125
+ return parseCommentJson(await fs2.readFile(filepath, "utf8"));
126
+ }
127
+ } catch (error) {
128
+ logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
129
+ logger_default.error(error);
110
130
  }
111
- } catch (error) {
112
- logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
113
- logger_default.error(error);
114
- }
131
+ };
115
132
  }
116
133
  function stringifyJson(value, replacer) {
117
134
  return stringify(value, replacer, 2);
@@ -270,6 +287,11 @@ function vitePluginWeapp(ctx, subPackageMeta) {
270
287
  // },
271
288
  configResolved(config) {
272
289
  configResolved = config;
290
+ if (isObject2(configResolved.env)) {
291
+ for (const [key, value] of Object.entries(configResolved.env)) {
292
+ ctx.setDefineEnv(key, value);
293
+ }
294
+ }
273
295
  },
274
296
  async options(options) {
275
297
  if (subPackageMeta) {
@@ -331,7 +353,7 @@ function vitePluginWeapp(ctx, subPackageMeta) {
331
353
  if (isHtml) {
332
354
  this.emitFile({
333
355
  type: "asset",
334
- fileName: changeFileExtension(fileName, "wxml"),
356
+ fileName: changeFileExtension(fileName, ctx.outputExtensions.wxml),
335
357
  source
336
358
  });
337
359
  } else if (isWxml) {
@@ -446,7 +468,7 @@ function vitePluginWeapp(ctx, subPackageMeta) {
446
468
  for (const originalFileName of asset.originalFileNames) {
447
469
  if (isJsOrTs(originalFileName)) {
448
470
  const newFileName = ctx.relativeSrcRoot(
449
- changeFileExtension(originalFileName, "wxss")
471
+ changeFileExtension(originalFileName, ctx.outputExtensions.wxss)
450
472
  );
451
473
  this.emitFile({
452
474
  type: "asset",
@@ -509,13 +531,21 @@ var CompilerContext = class {
509
531
  appEntry;
510
532
  subPackageMeta;
511
533
  aliasEntries;
534
+ platform;
535
+ outputExtensions;
536
+ readCommentJson;
537
+ /**
538
+ * esbuild 定义的环境变量
539
+ */
540
+ defineEnv;
512
541
  constructor(options) {
513
- const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson } = defu(options, {
542
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, platform } = defu(options, {
514
543
  cwd: process.cwd(),
515
544
  isDev: false,
516
545
  projectConfig: {},
517
546
  inlineConfig: {},
518
- packageJson: {}
547
+ packageJson: {},
548
+ platform: "weapp"
519
549
  });
520
550
  this.cwd = cwd;
521
551
  this.inlineConfig = inlineConfig;
@@ -528,6 +558,29 @@ var CompilerContext = class {
528
558
  this.entriesSet = /* @__PURE__ */ new Set();
529
559
  this.entries = [];
530
560
  this.aliasEntries = [];
561
+ this.platform = platform;
562
+ this.outputExtensions = getOutputExtensions(platform);
563
+ this.readCommentJson = createReadCommentJson(this);
564
+ this.defineEnv = {};
565
+ }
566
+ // https://github.com/vitejs/vite/blob/192d555f88bba7576e8a40cc027e8a11e006079c/packages/vite/src/node/plugins/define.ts#L41
567
+ /**
568
+ * 插件真正计算出来的 define options
569
+ */
570
+ get define() {
571
+ const env = {
572
+ MP_PLATFORM: this.platform,
573
+ ...this.defineEnv
574
+ };
575
+ const define = {};
576
+ for (const [key, value] of Object.entries(env)) {
577
+ define[`import.meta.env.${key}`] = JSON.stringify(value);
578
+ }
579
+ define[`import.meta.env`] = JSON.stringify(env);
580
+ return define;
581
+ }
582
+ setDefineEnv(key, value) {
583
+ this.defineEnv[key] = value;
531
584
  }
532
585
  get srcRoot() {
533
586
  return this.inlineConfig?.weapp?.srcRoot ?? "";
@@ -583,6 +636,8 @@ var CompilerContext = class {
583
636
  root: this.cwd,
584
637
  mode: "development",
585
638
  plugins: [vitePluginWeapp(this, subPackageMeta)],
639
+ // https://github.com/vitejs/vite/blob/a0336bd5197bb4427251be4c975e30fb596c658f/packages/vite/src/node/config.ts#L1117
640
+ define: this.define,
586
641
  build: {
587
642
  watch: {
588
643
  exclude: [
@@ -606,6 +661,7 @@ var CompilerContext = class {
606
661
  root: this.cwd,
607
662
  plugins: [vitePluginWeapp(this, subPackageMeta)],
608
663
  mode: "production",
664
+ define: this.define,
609
665
  build: {
610
666
  emptyOutDir: false
611
667
  }
@@ -712,7 +768,7 @@ var CompilerContext = class {
712
768
  }
713
769
  async checkDependenciesCacheOutdate() {
714
770
  const json = await this.readDependenciesCache();
715
- if (isObject2(json)) {
771
+ if (isObject3(json)) {
716
772
  return this.dependenciesCacheHash !== json["/"];
717
773
  }
718
774
  return true;
@@ -788,7 +844,7 @@ var CompilerContext = class {
788
844
  let finalOptions;
789
845
  if (resolvedOptions === void 0) {
790
846
  finalOptions = mergedOptions;
791
- } else if (isObject2(resolvedOptions)) {
847
+ } else if (isObject3(resolvedOptions)) {
792
848
  finalOptions = resolvedOptions;
793
849
  }
794
850
  finalOptions && await tsupBuild(finalOptions);
@@ -810,7 +866,7 @@ var CompilerContext = class {
810
866
  continue;
811
867
  }
812
868
  const tokens = componentUrl.split("/");
813
- if (tokens[0] && isObject2(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
869
+ if (tokens[0] && isObject3(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
814
870
  continue;
815
871
  } else if (tokens[0] === "") {
816
872
  await this.scanComponentEntry(componentUrl.substring(1), path5.resolve(this.cwd, this.srcRoot));
@@ -834,8 +890,8 @@ var CompilerContext = class {
834
890
  const appConfigFile = await findJsonEntry(appBasename);
835
891
  const appEntryPath = await findJsEntry(appBasename);
836
892
  if (appEntryPath && appConfigFile) {
837
- const config = await readCommentJson(appConfigFile);
838
- if (isObject2(config)) {
893
+ const config = await this.readCommentJson(appConfigFile);
894
+ if (isObject3(config)) {
839
895
  if (this.entriesSet.has(appEntryPath)) {
840
896
  return;
841
897
  }
@@ -853,14 +909,14 @@ var CompilerContext = class {
853
909
  const sitemapJsonPath = await findJsonEntry(path5.resolve(appDirname, sitemapLocation));
854
910
  if (sitemapJsonPath) {
855
911
  appEntry.sitemapJsonPath = sitemapJsonPath;
856
- appEntry.sitemapJson = await readCommentJson(sitemapJsonPath);
912
+ appEntry.sitemapJson = await this.readCommentJson(sitemapJsonPath);
857
913
  }
858
914
  }
859
915
  if (themeLocation) {
860
916
  const themeJsonPath = await findJsonEntry(path5.resolve(appDirname, themeLocation));
861
917
  if (themeJsonPath) {
862
918
  appEntry.themeJsonPath = themeJsonPath;
863
- appEntry.themeJson = await readCommentJson(themeJsonPath);
919
+ appEntry.themeJson = await this.readCommentJson(themeJsonPath);
864
920
  }
865
921
  }
866
922
  const subs = [...subpackages, ...subPackages];
@@ -926,7 +982,7 @@ var CompilerContext = class {
926
982
  }
927
983
  const configFile = await findJsonEntry(baseName);
928
984
  if (configFile) {
929
- const config = await readCommentJson(configFile);
985
+ const config = await this.readCommentJson(configFile);
930
986
  const jsonFragment = {
931
987
  json: config,
932
988
  jsonPath: configFile
@@ -935,7 +991,7 @@ var CompilerContext = class {
935
991
  partialEntry.json = jsonFragment.json;
936
992
  partialEntry.jsonPath = jsonFragment.jsonPath;
937
993
  }
938
- if (isObject2(config)) {
994
+ if (isObject3(config)) {
939
995
  await this.usingComponentsHandler(jsonFragment, path5.dirname(configFile));
940
996
  }
941
997
  }
package/dist/cli.cjs CHANGED
@@ -34,7 +34,7 @@ var import_vite3 = require("vite");
34
34
  var import_weapp_ide_cli = require("weapp-ide-cli");
35
35
 
36
36
  // package.json
37
- var version = "1.6.6";
37
+ var version = "1.6.7";
38
38
 
39
39
  // src/constants.ts
40
40
  var VERSION = version;
@@ -45,7 +45,7 @@ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
45
45
  // src/context.ts
46
46
  var import_node_module = require("module");
47
47
  var import_node_process = __toESM(require("process"), 1);
48
- var import_shared2 = require("@weapp-core/shared");
48
+ var import_shared3 = require("@weapp-core/shared");
49
49
  var import_fs_extra5 = __toESM(require("fs-extra"), 1);
50
50
  var import_pathe5 = __toESM(require("pathe"), 1);
51
51
  var import_vite2 = require("vite");
@@ -62,12 +62,22 @@ function createDebugger(namespace) {
62
62
 
63
63
  // src/defaults.ts
64
64
  var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
65
+ function getOutputExtensions(_platform) {
66
+ return {
67
+ js: "js",
68
+ json: "json",
69
+ wxml: "wxml",
70
+ wxss: "wxss",
71
+ wxs: "wxs"
72
+ };
73
+ }
65
74
 
66
75
  // src/logger.ts
67
76
  var import_logger = __toESM(require("@weapp-core/logger"), 1);
68
77
  var logger_default = import_logger.default;
69
78
 
70
79
  // src/plugins/index.ts
80
+ var import_shared2 = require("@weapp-core/shared");
71
81
  var import_fdir = require("fdir");
72
82
  var import_fs_extra4 = __toESM(require("fs-extra"), 1);
73
83
  var import_magic_string = __toESM(require("magic-string"), 1);
@@ -123,30 +133,37 @@ function resolveGlobs(globs, subPackageMeta) {
123
133
  }
124
134
 
125
135
  // src/utils/json.ts
126
- var import_node_url = require("url");
127
136
  var import_shared = require("@weapp-core/shared");
137
+ var import_bundle_require = require("bundle-require");
128
138
  var import_comment_json = require("comment-json");
129
139
  var import_fs_extra2 = __toESM(require("fs-extra"), 1);
130
140
  var import_pathe2 = __toESM(require("pathe"), 1);
131
- var import_api = require("tsx/esm/api");
132
141
  function parseCommentJson(json) {
133
142
  return (0, import_comment_json.parse)(json, void 0, true);
134
143
  }
135
144
  function jsonFileRemoveJsExtension(fileName) {
136
145
  return fileName.replace(/\.[jt]s$/, "");
137
146
  }
138
- async function readCommentJson(filepath) {
139
- try {
140
- if (/\.json\.[jt]s$/.test(filepath)) {
141
- const loaded = await (0, import_api.tsImport)((0, import_node_url.pathToFileURL)(filepath).href, importMetaUrl);
142
- return loaded.default;
143
- } else {
144
- return parseCommentJson(await import_fs_extra2.default.readFile(filepath, "utf8"));
147
+ function createReadCommentJson(ctx) {
148
+ return async function readCommentJson(filepath) {
149
+ try {
150
+ if (/\.json\.[jt]s$/.test(filepath)) {
151
+ const { mod } = await (0, import_bundle_require.bundleRequire)({
152
+ filepath,
153
+ cwd: ctx?.cwd,
154
+ esbuildOptions: {
155
+ define: ctx?.define
156
+ }
157
+ });
158
+ return typeof mod.default === "function" ? await mod.default(ctx) : mod.default;
159
+ } else {
160
+ return parseCommentJson(await import_fs_extra2.default.readFile(filepath, "utf8"));
161
+ }
162
+ } catch (error) {
163
+ logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
164
+ logger_default.error(error);
145
165
  }
146
- } catch (error) {
147
- logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
148
- logger_default.error(error);
149
- }
166
+ };
150
167
  }
151
168
  function stringifyJson(value, replacer) {
152
169
  return (0, import_comment_json.stringify)(value, replacer, 2);
@@ -305,6 +322,11 @@ function vitePluginWeapp(ctx, subPackageMeta) {
305
322
  // },
306
323
  configResolved(config) {
307
324
  configResolved = config;
325
+ if ((0, import_shared2.isObject)(configResolved.env)) {
326
+ for (const [key, value] of Object.entries(configResolved.env)) {
327
+ ctx.setDefineEnv(key, value);
328
+ }
329
+ }
308
330
  },
309
331
  async options(options) {
310
332
  if (subPackageMeta) {
@@ -366,7 +388,7 @@ function vitePluginWeapp(ctx, subPackageMeta) {
366
388
  if (isHtml) {
367
389
  this.emitFile({
368
390
  type: "asset",
369
- fileName: changeFileExtension(fileName, "wxml"),
391
+ fileName: changeFileExtension(fileName, ctx.outputExtensions.wxml),
370
392
  source
371
393
  });
372
394
  } else if (isWxml) {
@@ -481,7 +503,7 @@ function vitePluginWeapp(ctx, subPackageMeta) {
481
503
  for (const originalFileName of asset.originalFileNames) {
482
504
  if (isJsOrTs(originalFileName)) {
483
505
  const newFileName = ctx.relativeSrcRoot(
484
- changeFileExtension(originalFileName, "wxss")
506
+ changeFileExtension(originalFileName, ctx.outputExtensions.wxss)
485
507
  );
486
508
  this.emitFile({
487
509
  type: "asset",
@@ -544,13 +566,21 @@ var CompilerContext = class {
544
566
  appEntry;
545
567
  subPackageMeta;
546
568
  aliasEntries;
569
+ platform;
570
+ outputExtensions;
571
+ readCommentJson;
572
+ /**
573
+ * esbuild 定义的环境变量
574
+ */
575
+ defineEnv;
547
576
  constructor(options) {
548
- const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson } = (0, import_shared2.defu)(options, {
577
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, platform } = (0, import_shared3.defu)(options, {
549
578
  cwd: import_node_process.default.cwd(),
550
579
  isDev: false,
551
580
  projectConfig: {},
552
581
  inlineConfig: {},
553
- packageJson: {}
582
+ packageJson: {},
583
+ platform: "weapp"
554
584
  });
555
585
  this.cwd = cwd;
556
586
  this.inlineConfig = inlineConfig;
@@ -563,6 +593,29 @@ var CompilerContext = class {
563
593
  this.entriesSet = /* @__PURE__ */ new Set();
564
594
  this.entries = [];
565
595
  this.aliasEntries = [];
596
+ this.platform = platform;
597
+ this.outputExtensions = getOutputExtensions(platform);
598
+ this.readCommentJson = createReadCommentJson(this);
599
+ this.defineEnv = {};
600
+ }
601
+ // https://github.com/vitejs/vite/blob/192d555f88bba7576e8a40cc027e8a11e006079c/packages/vite/src/node/plugins/define.ts#L41
602
+ /**
603
+ * 插件真正计算出来的 define options
604
+ */
605
+ get define() {
606
+ const env = {
607
+ MP_PLATFORM: this.platform,
608
+ ...this.defineEnv
609
+ };
610
+ const define = {};
611
+ for (const [key, value] of Object.entries(env)) {
612
+ define[`import.meta.env.${key}`] = JSON.stringify(value);
613
+ }
614
+ define[`import.meta.env`] = JSON.stringify(env);
615
+ return define;
616
+ }
617
+ setDefineEnv(key, value) {
618
+ this.defineEnv[key] = value;
566
619
  }
567
620
  get srcRoot() {
568
621
  return this.inlineConfig?.weapp?.srcRoot ?? "";
@@ -611,13 +664,15 @@ var CompilerContext = class {
611
664
  }
612
665
  getConfig(subPackageMeta, ...configs) {
613
666
  if (this.isDev) {
614
- return (0, import_shared2.defu)(
667
+ return (0, import_shared3.defu)(
615
668
  this.inlineConfig,
616
669
  ...configs,
617
670
  {
618
671
  root: this.cwd,
619
672
  mode: "development",
620
673
  plugins: [vitePluginWeapp(this, subPackageMeta)],
674
+ // https://github.com/vitejs/vite/blob/a0336bd5197bb4427251be4c975e30fb596c658f/packages/vite/src/node/config.ts#L1117
675
+ define: this.define,
621
676
  build: {
622
677
  watch: {
623
678
  exclude: [
@@ -634,13 +689,14 @@ var CompilerContext = class {
634
689
  }
635
690
  );
636
691
  } else {
637
- const inlineConfig = (0, import_shared2.defu)(
692
+ const inlineConfig = (0, import_shared3.defu)(
638
693
  this.inlineConfig,
639
694
  ...configs,
640
695
  {
641
696
  root: this.cwd,
642
697
  plugins: [vitePluginWeapp(this, subPackageMeta)],
643
698
  mode: "production",
699
+ define: this.define,
644
700
  build: {
645
701
  emptyOutDir: false
646
702
  }
@@ -695,7 +751,7 @@ var CompilerContext = class {
695
751
  command: this.isDev ? "serve" : "build",
696
752
  mode: this.mode
697
753
  }, void 0, this.cwd);
698
- this.inlineConfig = (0, import_shared2.defu)({
754
+ this.inlineConfig = (0, import_shared3.defu)({
699
755
  configFile: false
700
756
  }, loaded?.config, {
701
757
  build: {
@@ -706,11 +762,11 @@ var CompilerContext = class {
706
762
  entryFileNames: (chunkInfo) => {
707
763
  const name = this.relativeSrcRoot(chunkInfo.name);
708
764
  if (name.endsWith(".ts")) {
709
- const baseFileName = (0, import_shared2.removeExtension)(name);
765
+ const baseFileName = (0, import_shared3.removeExtension)(name);
710
766
  if (baseFileName.endsWith(".wxs")) {
711
767
  return baseFileName;
712
768
  }
713
- return (0, import_shared2.addExtension)(baseFileName, ".js");
769
+ return (0, import_shared3.addExtension)(baseFileName, ".js");
714
770
  }
715
771
  return name;
716
772
  }
@@ -733,7 +789,7 @@ var CompilerContext = class {
733
789
  return import_pathe5.default.resolve(this.cwd, "node_modules/weapp-vite/.cache/npm.json");
734
790
  }
735
791
  get dependenciesCacheHash() {
736
- return (0, import_shared2.objectHash)(this.packageJson.dependencies ?? {});
792
+ return (0, import_shared3.objectHash)(this.packageJson.dependencies ?? {});
737
793
  }
738
794
  writeDependenciesCache() {
739
795
  return import_fs_extra5.default.outputJSON(this.dependenciesCacheFilePath, {
@@ -747,7 +803,7 @@ var CompilerContext = class {
747
803
  }
748
804
  async checkDependenciesCacheOutdate() {
749
805
  const json = await this.readDependenciesCache();
750
- if ((0, import_shared2.isObject)(json)) {
806
+ if ((0, import_shared3.isObject)(json)) {
751
807
  return this.dependenciesCacheHash !== json["/"];
752
808
  }
753
809
  return true;
@@ -802,7 +858,7 @@ var CompilerContext = class {
802
858
  logger_default.success(`${dep} \u4F9D\u8D56\u672A\u53D1\u751F\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5904\u7406!`);
803
859
  continue;
804
860
  }
805
- const mergedOptions = (0, import_shared2.defu)(options, {
861
+ const mergedOptions = (0, import_shared3.defu)(options, {
806
862
  entry: {
807
863
  index: require2.resolve(dep)
808
864
  },
@@ -823,7 +879,7 @@ var CompilerContext = class {
823
879
  let finalOptions;
824
880
  if (resolvedOptions === void 0) {
825
881
  finalOptions = mergedOptions;
826
- } else if ((0, import_shared2.isObject)(resolvedOptions)) {
882
+ } else if ((0, import_shared3.isObject)(resolvedOptions)) {
827
883
  finalOptions = resolvedOptions;
828
884
  }
829
885
  finalOptions && await tsupBuild(finalOptions);
@@ -845,7 +901,7 @@ var CompilerContext = class {
845
901
  continue;
846
902
  }
847
903
  const tokens = componentUrl.split("/");
848
- if (tokens[0] && (0, import_shared2.isObject)(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
904
+ if (tokens[0] && (0, import_shared3.isObject)(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
849
905
  continue;
850
906
  } else if (tokens[0] === "") {
851
907
  await this.scanComponentEntry(componentUrl.substring(1), import_pathe5.default.resolve(this.cwd, this.srcRoot));
@@ -869,8 +925,8 @@ var CompilerContext = class {
869
925
  const appConfigFile = await findJsonEntry(appBasename);
870
926
  const appEntryPath = await findJsEntry(appBasename);
871
927
  if (appEntryPath && appConfigFile) {
872
- const config = await readCommentJson(appConfigFile);
873
- if ((0, import_shared2.isObject)(config)) {
928
+ const config = await this.readCommentJson(appConfigFile);
929
+ if ((0, import_shared3.isObject)(config)) {
874
930
  if (this.entriesSet.has(appEntryPath)) {
875
931
  return;
876
932
  }
@@ -888,14 +944,14 @@ var CompilerContext = class {
888
944
  const sitemapJsonPath = await findJsonEntry(import_pathe5.default.resolve(appDirname, sitemapLocation));
889
945
  if (sitemapJsonPath) {
890
946
  appEntry.sitemapJsonPath = sitemapJsonPath;
891
- appEntry.sitemapJson = await readCommentJson(sitemapJsonPath);
947
+ appEntry.sitemapJson = await this.readCommentJson(sitemapJsonPath);
892
948
  }
893
949
  }
894
950
  if (themeLocation) {
895
951
  const themeJsonPath = await findJsonEntry(import_pathe5.default.resolve(appDirname, themeLocation));
896
952
  if (themeJsonPath) {
897
953
  appEntry.themeJsonPath = themeJsonPath;
898
- appEntry.themeJson = await readCommentJson(themeJsonPath);
954
+ appEntry.themeJson = await this.readCommentJson(themeJsonPath);
899
955
  }
900
956
  }
901
957
  const subs = [...subpackages, ...subPackages];
@@ -933,7 +989,7 @@ var CompilerContext = class {
933
989
  }
934
990
  }
935
991
  }
936
- if ((0, import_shared2.get)(appEntry, "json.tabBar.custom")) {
992
+ if ((0, import_shared3.get)(appEntry, "json.tabBar.custom")) {
937
993
  await this.scanComponentEntry("custom-tab-bar/index", appDirname);
938
994
  }
939
995
  debug2?.("scanAppEntry end");
@@ -950,7 +1006,7 @@ var CompilerContext = class {
950
1006
  // 页面可以没有 JSON
951
1007
  async scanComponentEntry(componentEntry, dirname) {
952
1008
  debug2?.("scanComponentEntry start", componentEntry);
953
- const baseName = (0, import_shared2.removeExtension)(import_pathe5.default.resolve(dirname, componentEntry));
1009
+ const baseName = (0, import_shared3.removeExtension)(import_pathe5.default.resolve(dirname, componentEntry));
954
1010
  const jsEntry = await findJsEntry(baseName);
955
1011
  const partialEntry = {
956
1012
  path: jsEntry
@@ -961,7 +1017,7 @@ var CompilerContext = class {
961
1017
  }
962
1018
  const configFile = await findJsonEntry(baseName);
963
1019
  if (configFile) {
964
- const config = await readCommentJson(configFile);
1020
+ const config = await this.readCommentJson(configFile);
965
1021
  const jsonFragment = {
966
1022
  json: config,
967
1023
  jsonPath: configFile
@@ -970,7 +1026,7 @@ var CompilerContext = class {
970
1026
  partialEntry.json = jsonFragment.json;
971
1027
  partialEntry.jsonPath = jsonFragment.jsonPath;
972
1028
  }
973
- if ((0, import_shared2.isObject)(config)) {
1029
+ if ((0, import_shared3.isObject)(config)) {
974
1030
  await this.usingComponentsHandler(jsonFragment, import_pathe5.default.dirname(configFile));
975
1031
  }
976
1032
  }
@@ -1028,7 +1084,7 @@ async function createCompilerContext(options) {
1028
1084
  // src/schematics.ts
1029
1085
  var import_node_process2 = __toESM(require("process"), 1);
1030
1086
  var import_schematics = require("@weapp-core/schematics");
1031
- var import_shared3 = require("@weapp-core/shared");
1087
+ var import_shared4 = require("@weapp-core/shared");
1032
1088
  var import_fs_extra6 = __toESM(require("fs-extra"), 1);
1033
1089
  var import_pathe6 = __toESM(require("pathe"), 1);
1034
1090
  function composePath(outDir, filename) {
@@ -1044,7 +1100,7 @@ function resolveExtension(extension) {
1044
1100
  return extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
1045
1101
  }
1046
1102
  async function generate(options) {
1047
- let { fileName, outDir, extensions, type, cwd } = (0, import_shared3.defu)(options, {
1103
+ let { fileName, outDir, extensions, type, cwd } = (0, import_shared4.defu)(options, {
1048
1104
  // fileName: 'index',
1049
1105
  type: "component",
1050
1106
  extensions: {
package/dist/cli.mjs CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  VERSION,
3
3
  createCompilerContext,
4
4
  logger_default
5
- } from "./chunk-MF2E2WZT.mjs";
5
+ } from "./chunk-5PUUYFZM.mjs";
6
6
  import "./chunk-444MQSSG.mjs";
7
7
 
8
8
  // src/cli.ts
@@ -17,6 +17,7 @@ interface AliasOptions {
17
17
  [find: string]: string;
18
18
  };
19
19
  }
20
+ type MpPlatform = 'weapp';
20
21
  interface SubPackage {
21
22
  pages: string[];
22
23
  root: string;
@@ -75,6 +76,10 @@ interface WeappViteConfig {
75
76
  * 默认情况下包括大部分的图片资源格式
76
77
  */
77
78
  copy?: CopyOptions;
79
+ /**
80
+ * 编译目标平台
81
+ */
82
+ platform?: MpPlatform;
78
83
  }
79
84
  interface BaseEntry {
80
85
  path: string;
@@ -108,6 +113,7 @@ interface CompilerContextOptions {
108
113
  projectConfig?: ProjectConfig;
109
114
  mode?: string;
110
115
  packageJson?: PackageJson;
116
+ platform?: MpPlatform;
111
117
  }
112
118
  interface SubPackageMetaValue {
113
119
  entriesSet: Set<string>;
@@ -125,4 +131,4 @@ declare function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>;
125
131
  declare function defineConfig(config: UserConfigFnObject): UserConfigFnObject;
126
132
  declare function defineConfig(config: UserConfigExport): UserConfigExport;
127
133
 
128
- export { type AppEntry as A, type CompilerContextOptions as C, type Entry as E, type ProjectConfig as P, type ResolvedAlias as R, type SubPackageMetaValue as S, defineConfig as d };
134
+ export { type AppEntry as A, type CompilerContextOptions as C, type Entry as E, type MpPlatform as M, type ProjectConfig as P, type ResolvedAlias as R, type SubPackageMetaValue as S, defineConfig as d };
@@ -17,6 +17,7 @@ interface AliasOptions {
17
17
  [find: string]: string;
18
18
  };
19
19
  }
20
+ type MpPlatform = 'weapp';
20
21
  interface SubPackage {
21
22
  pages: string[];
22
23
  root: string;
@@ -75,6 +76,10 @@ interface WeappViteConfig {
75
76
  * 默认情况下包括大部分的图片资源格式
76
77
  */
77
78
  copy?: CopyOptions;
79
+ /**
80
+ * 编译目标平台
81
+ */
82
+ platform?: MpPlatform;
78
83
  }
79
84
  interface BaseEntry {
80
85
  path: string;
@@ -108,6 +113,7 @@ interface CompilerContextOptions {
108
113
  projectConfig?: ProjectConfig;
109
114
  mode?: string;
110
115
  packageJson?: PackageJson;
116
+ platform?: MpPlatform;
111
117
  }
112
118
  interface SubPackageMetaValue {
113
119
  entriesSet: Set<string>;
@@ -125,4 +131,4 @@ declare function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>;
125
131
  declare function defineConfig(config: UserConfigFnObject): UserConfigFnObject;
126
132
  declare function defineConfig(config: UserConfigExport): UserConfigExport;
127
133
 
128
- export { type AppEntry as A, type CompilerContextOptions as C, type Entry as E, type ProjectConfig as P, type ResolvedAlias as R, type SubPackageMetaValue as S, defineConfig as d };
134
+ export { type AppEntry as A, type CompilerContextOptions as C, type Entry as E, type MpPlatform as M, type ProjectConfig as P, type ResolvedAlias as R, type SubPackageMetaValue as S, defineConfig as d };
package/dist/config.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { UserConfig, UserConfigExport, UserConfigFnObject } from 'vite';
2
- export { d as defineConfig } from './config-BeGrsxaI.cjs';
2
+ export { d as defineConfig } from './config-BeFRKLxC.cjs';
3
3
  import '@weapp-core/schematics';
4
4
  import 'pkg-types';
5
5
  import 'tsup';
package/dist/config.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { UserConfig, UserConfigExport, UserConfigFnObject } from 'vite';
2
- export { d as defineConfig } from './config-BeGrsxaI.js';
2
+ export { d as defineConfig } from './config-BeFRKLxC.js';
3
3
  import '@weapp-core/schematics';
4
4
  import 'pkg-types';
5
5
  import 'tsup';
package/dist/index.cjs CHANGED
@@ -42,7 +42,7 @@ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
42
42
  // src/context.ts
43
43
  var import_node_module = require("module");
44
44
  var import_node_process = __toESM(require("process"), 1);
45
- var import_shared2 = require("@weapp-core/shared");
45
+ var import_shared3 = require("@weapp-core/shared");
46
46
  var import_fs_extra5 = __toESM(require("fs-extra"), 1);
47
47
  var import_pathe5 = __toESM(require("pathe"), 1);
48
48
  var import_vite2 = require("vite");
@@ -59,12 +59,22 @@ function createDebugger(namespace) {
59
59
 
60
60
  // src/defaults.ts
61
61
  var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
62
+ function getOutputExtensions(_platform) {
63
+ return {
64
+ js: "js",
65
+ json: "json",
66
+ wxml: "wxml",
67
+ wxss: "wxss",
68
+ wxs: "wxs"
69
+ };
70
+ }
62
71
 
63
72
  // src/logger.ts
64
73
  var import_logger = __toESM(require("@weapp-core/logger"), 1);
65
74
  var logger_default = import_logger.default;
66
75
 
67
76
  // src/plugins/index.ts
77
+ var import_shared2 = require("@weapp-core/shared");
68
78
  var import_fdir = require("fdir");
69
79
  var import_fs_extra4 = __toESM(require("fs-extra"), 1);
70
80
  var import_magic_string = __toESM(require("magic-string"), 1);
@@ -125,30 +135,37 @@ function resolveGlobs(globs, subPackageMeta) {
125
135
  }
126
136
 
127
137
  // src/utils/json.ts
128
- var import_node_url = require("url");
129
138
  var import_shared = require("@weapp-core/shared");
139
+ var import_bundle_require = require("bundle-require");
130
140
  var import_comment_json = require("comment-json");
131
141
  var import_fs_extra2 = __toESM(require("fs-extra"), 1);
132
142
  var import_pathe2 = __toESM(require("pathe"), 1);
133
- var import_api = require("tsx/esm/api");
134
143
  function parseCommentJson(json) {
135
144
  return (0, import_comment_json.parse)(json, void 0, true);
136
145
  }
137
146
  function jsonFileRemoveJsExtension(fileName) {
138
147
  return fileName.replace(/\.[jt]s$/, "");
139
148
  }
140
- async function readCommentJson(filepath) {
141
- try {
142
- if (/\.json\.[jt]s$/.test(filepath)) {
143
- const loaded = await (0, import_api.tsImport)((0, import_node_url.pathToFileURL)(filepath).href, importMetaUrl);
144
- return loaded.default;
145
- } else {
146
- return parseCommentJson(await import_fs_extra2.default.readFile(filepath, "utf8"));
149
+ function createReadCommentJson(ctx) {
150
+ return async function readCommentJson(filepath) {
151
+ try {
152
+ if (/\.json\.[jt]s$/.test(filepath)) {
153
+ const { mod } = await (0, import_bundle_require.bundleRequire)({
154
+ filepath,
155
+ cwd: ctx?.cwd,
156
+ esbuildOptions: {
157
+ define: ctx?.define
158
+ }
159
+ });
160
+ return typeof mod.default === "function" ? await mod.default(ctx) : mod.default;
161
+ } else {
162
+ return parseCommentJson(await import_fs_extra2.default.readFile(filepath, "utf8"));
163
+ }
164
+ } catch (error) {
165
+ logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
166
+ logger_default.error(error);
147
167
  }
148
- } catch (error) {
149
- logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
150
- logger_default.error(error);
151
- }
168
+ };
152
169
  }
153
170
  function stringifyJson(value, replacer) {
154
171
  return (0, import_comment_json.stringify)(value, replacer, 2);
@@ -307,6 +324,11 @@ function vitePluginWeapp(ctx, subPackageMeta) {
307
324
  // },
308
325
  configResolved(config) {
309
326
  configResolved = config;
327
+ if ((0, import_shared2.isObject)(configResolved.env)) {
328
+ for (const [key, value] of Object.entries(configResolved.env)) {
329
+ ctx.setDefineEnv(key, value);
330
+ }
331
+ }
310
332
  },
311
333
  async options(options) {
312
334
  if (subPackageMeta) {
@@ -368,7 +390,7 @@ function vitePluginWeapp(ctx, subPackageMeta) {
368
390
  if (isHtml) {
369
391
  this.emitFile({
370
392
  type: "asset",
371
- fileName: changeFileExtension(fileName, "wxml"),
393
+ fileName: changeFileExtension(fileName, ctx.outputExtensions.wxml),
372
394
  source
373
395
  });
374
396
  } else if (isWxml) {
@@ -483,7 +505,7 @@ function vitePluginWeapp(ctx, subPackageMeta) {
483
505
  for (const originalFileName of asset.originalFileNames) {
484
506
  if (isJsOrTs(originalFileName)) {
485
507
  const newFileName = ctx.relativeSrcRoot(
486
- changeFileExtension(originalFileName, "wxss")
508
+ changeFileExtension(originalFileName, ctx.outputExtensions.wxss)
487
509
  );
488
510
  this.emitFile({
489
511
  type: "asset",
@@ -546,13 +568,21 @@ var CompilerContext = class {
546
568
  appEntry;
547
569
  subPackageMeta;
548
570
  aliasEntries;
571
+ platform;
572
+ outputExtensions;
573
+ readCommentJson;
574
+ /**
575
+ * esbuild 定义的环境变量
576
+ */
577
+ defineEnv;
549
578
  constructor(options) {
550
- const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson } = (0, import_shared2.defu)(options, {
579
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, platform } = (0, import_shared3.defu)(options, {
551
580
  cwd: import_node_process.default.cwd(),
552
581
  isDev: false,
553
582
  projectConfig: {},
554
583
  inlineConfig: {},
555
- packageJson: {}
584
+ packageJson: {},
585
+ platform: "weapp"
556
586
  });
557
587
  this.cwd = cwd;
558
588
  this.inlineConfig = inlineConfig;
@@ -565,6 +595,29 @@ var CompilerContext = class {
565
595
  this.entriesSet = /* @__PURE__ */ new Set();
566
596
  this.entries = [];
567
597
  this.aliasEntries = [];
598
+ this.platform = platform;
599
+ this.outputExtensions = getOutputExtensions(platform);
600
+ this.readCommentJson = createReadCommentJson(this);
601
+ this.defineEnv = {};
602
+ }
603
+ // https://github.com/vitejs/vite/blob/192d555f88bba7576e8a40cc027e8a11e006079c/packages/vite/src/node/plugins/define.ts#L41
604
+ /**
605
+ * 插件真正计算出来的 define options
606
+ */
607
+ get define() {
608
+ const env = {
609
+ MP_PLATFORM: this.platform,
610
+ ...this.defineEnv
611
+ };
612
+ const define = {};
613
+ for (const [key, value] of Object.entries(env)) {
614
+ define[`import.meta.env.${key}`] = JSON.stringify(value);
615
+ }
616
+ define[`import.meta.env`] = JSON.stringify(env);
617
+ return define;
618
+ }
619
+ setDefineEnv(key, value) {
620
+ this.defineEnv[key] = value;
568
621
  }
569
622
  get srcRoot() {
570
623
  return this.inlineConfig?.weapp?.srcRoot ?? "";
@@ -613,13 +666,15 @@ var CompilerContext = class {
613
666
  }
614
667
  getConfig(subPackageMeta, ...configs) {
615
668
  if (this.isDev) {
616
- return (0, import_shared2.defu)(
669
+ return (0, import_shared3.defu)(
617
670
  this.inlineConfig,
618
671
  ...configs,
619
672
  {
620
673
  root: this.cwd,
621
674
  mode: "development",
622
675
  plugins: [vitePluginWeapp(this, subPackageMeta)],
676
+ // https://github.com/vitejs/vite/blob/a0336bd5197bb4427251be4c975e30fb596c658f/packages/vite/src/node/config.ts#L1117
677
+ define: this.define,
623
678
  build: {
624
679
  watch: {
625
680
  exclude: [
@@ -636,13 +691,14 @@ var CompilerContext = class {
636
691
  }
637
692
  );
638
693
  } else {
639
- const inlineConfig = (0, import_shared2.defu)(
694
+ const inlineConfig = (0, import_shared3.defu)(
640
695
  this.inlineConfig,
641
696
  ...configs,
642
697
  {
643
698
  root: this.cwd,
644
699
  plugins: [vitePluginWeapp(this, subPackageMeta)],
645
700
  mode: "production",
701
+ define: this.define,
646
702
  build: {
647
703
  emptyOutDir: false
648
704
  }
@@ -697,7 +753,7 @@ var CompilerContext = class {
697
753
  command: this.isDev ? "serve" : "build",
698
754
  mode: this.mode
699
755
  }, void 0, this.cwd);
700
- this.inlineConfig = (0, import_shared2.defu)({
756
+ this.inlineConfig = (0, import_shared3.defu)({
701
757
  configFile: false
702
758
  }, loaded?.config, {
703
759
  build: {
@@ -708,11 +764,11 @@ var CompilerContext = class {
708
764
  entryFileNames: (chunkInfo) => {
709
765
  const name = this.relativeSrcRoot(chunkInfo.name);
710
766
  if (name.endsWith(".ts")) {
711
- const baseFileName = (0, import_shared2.removeExtension)(name);
767
+ const baseFileName = (0, import_shared3.removeExtension)(name);
712
768
  if (baseFileName.endsWith(".wxs")) {
713
769
  return baseFileName;
714
770
  }
715
- return (0, import_shared2.addExtension)(baseFileName, ".js");
771
+ return (0, import_shared3.addExtension)(baseFileName, ".js");
716
772
  }
717
773
  return name;
718
774
  }
@@ -735,7 +791,7 @@ var CompilerContext = class {
735
791
  return import_pathe5.default.resolve(this.cwd, "node_modules/weapp-vite/.cache/npm.json");
736
792
  }
737
793
  get dependenciesCacheHash() {
738
- return (0, import_shared2.objectHash)(this.packageJson.dependencies ?? {});
794
+ return (0, import_shared3.objectHash)(this.packageJson.dependencies ?? {});
739
795
  }
740
796
  writeDependenciesCache() {
741
797
  return import_fs_extra5.default.outputJSON(this.dependenciesCacheFilePath, {
@@ -749,7 +805,7 @@ var CompilerContext = class {
749
805
  }
750
806
  async checkDependenciesCacheOutdate() {
751
807
  const json = await this.readDependenciesCache();
752
- if ((0, import_shared2.isObject)(json)) {
808
+ if ((0, import_shared3.isObject)(json)) {
753
809
  return this.dependenciesCacheHash !== json["/"];
754
810
  }
755
811
  return true;
@@ -804,7 +860,7 @@ var CompilerContext = class {
804
860
  logger_default.success(`${dep} \u4F9D\u8D56\u672A\u53D1\u751F\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5904\u7406!`);
805
861
  continue;
806
862
  }
807
- const mergedOptions = (0, import_shared2.defu)(options, {
863
+ const mergedOptions = (0, import_shared3.defu)(options, {
808
864
  entry: {
809
865
  index: require2.resolve(dep)
810
866
  },
@@ -825,7 +881,7 @@ var CompilerContext = class {
825
881
  let finalOptions;
826
882
  if (resolvedOptions === void 0) {
827
883
  finalOptions = mergedOptions;
828
- } else if ((0, import_shared2.isObject)(resolvedOptions)) {
884
+ } else if ((0, import_shared3.isObject)(resolvedOptions)) {
829
885
  finalOptions = resolvedOptions;
830
886
  }
831
887
  finalOptions && await tsupBuild(finalOptions);
@@ -847,7 +903,7 @@ var CompilerContext = class {
847
903
  continue;
848
904
  }
849
905
  const tokens = componentUrl.split("/");
850
- if (tokens[0] && (0, import_shared2.isObject)(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
906
+ if (tokens[0] && (0, import_shared3.isObject)(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
851
907
  continue;
852
908
  } else if (tokens[0] === "") {
853
909
  await this.scanComponentEntry(componentUrl.substring(1), import_pathe5.default.resolve(this.cwd, this.srcRoot));
@@ -871,8 +927,8 @@ var CompilerContext = class {
871
927
  const appConfigFile = await findJsonEntry(appBasename);
872
928
  const appEntryPath = await findJsEntry(appBasename);
873
929
  if (appEntryPath && appConfigFile) {
874
- const config = await readCommentJson(appConfigFile);
875
- if ((0, import_shared2.isObject)(config)) {
930
+ const config = await this.readCommentJson(appConfigFile);
931
+ if ((0, import_shared3.isObject)(config)) {
876
932
  if (this.entriesSet.has(appEntryPath)) {
877
933
  return;
878
934
  }
@@ -890,14 +946,14 @@ var CompilerContext = class {
890
946
  const sitemapJsonPath = await findJsonEntry(import_pathe5.default.resolve(appDirname, sitemapLocation));
891
947
  if (sitemapJsonPath) {
892
948
  appEntry.sitemapJsonPath = sitemapJsonPath;
893
- appEntry.sitemapJson = await readCommentJson(sitemapJsonPath);
949
+ appEntry.sitemapJson = await this.readCommentJson(sitemapJsonPath);
894
950
  }
895
951
  }
896
952
  if (themeLocation) {
897
953
  const themeJsonPath = await findJsonEntry(import_pathe5.default.resolve(appDirname, themeLocation));
898
954
  if (themeJsonPath) {
899
955
  appEntry.themeJsonPath = themeJsonPath;
900
- appEntry.themeJson = await readCommentJson(themeJsonPath);
956
+ appEntry.themeJson = await this.readCommentJson(themeJsonPath);
901
957
  }
902
958
  }
903
959
  const subs = [...subpackages, ...subPackages];
@@ -935,7 +991,7 @@ var CompilerContext = class {
935
991
  }
936
992
  }
937
993
  }
938
- if ((0, import_shared2.get)(appEntry, "json.tabBar.custom")) {
994
+ if ((0, import_shared3.get)(appEntry, "json.tabBar.custom")) {
939
995
  await this.scanComponentEntry("custom-tab-bar/index", appDirname);
940
996
  }
941
997
  debug2?.("scanAppEntry end");
@@ -952,7 +1008,7 @@ var CompilerContext = class {
952
1008
  // 页面可以没有 JSON
953
1009
  async scanComponentEntry(componentEntry, dirname) {
954
1010
  debug2?.("scanComponentEntry start", componentEntry);
955
- const baseName = (0, import_shared2.removeExtension)(import_pathe5.default.resolve(dirname, componentEntry));
1011
+ const baseName = (0, import_shared3.removeExtension)(import_pathe5.default.resolve(dirname, componentEntry));
956
1012
  const jsEntry = await findJsEntry(baseName);
957
1013
  const partialEntry = {
958
1014
  path: jsEntry
@@ -963,7 +1019,7 @@ var CompilerContext = class {
963
1019
  }
964
1020
  const configFile = await findJsonEntry(baseName);
965
1021
  if (configFile) {
966
- const config = await readCommentJson(configFile);
1022
+ const config = await this.readCommentJson(configFile);
967
1023
  const jsonFragment = {
968
1024
  json: config,
969
1025
  jsonPath: configFile
@@ -972,7 +1028,7 @@ var CompilerContext = class {
972
1028
  partialEntry.json = jsonFragment.json;
973
1029
  partialEntry.jsonPath = jsonFragment.jsonPath;
974
1030
  }
975
- if ((0, import_shared2.isObject)(config)) {
1031
+ if ((0, import_shared3.isObject)(config)) {
976
1032
  await this.usingComponentsHandler(jsonFragment, import_pathe5.default.dirname(configFile));
977
1033
  }
978
1034
  }
package/dist/index.d.cts CHANGED
@@ -1,11 +1,19 @@
1
1
  import { PackageJson } from 'pkg-types';
2
2
  import { RollupWatcher, RollupOutput } from 'rollup';
3
- import { P as ProjectConfig, E as Entry, A as AppEntry, S as SubPackageMetaValue, R as ResolvedAlias, C as CompilerContextOptions } from './config-BeGrsxaI.cjs';
3
+ import { P as ProjectConfig, E as Entry, A as AppEntry, S as SubPackageMetaValue, R as ResolvedAlias, M as MpPlatform, C as CompilerContextOptions } from './config-BeFRKLxC.cjs';
4
4
  import { InlineConfig } from 'vite';
5
5
  import { Options } from 'tsup';
6
6
  import '@weapp-core/schematics';
7
7
  import 'vite-tsconfig-paths';
8
8
 
9
+ interface OutputExtensions {
10
+ js: string;
11
+ json: string;
12
+ wxml: string;
13
+ wxss: string;
14
+ wxs?: string;
15
+ }
16
+
9
17
  declare class CompilerContext {
10
18
  /**
11
19
  * loadDefaultConfig 的时候会被重新赋予
@@ -22,7 +30,19 @@ declare class CompilerContext {
22
30
  appEntry?: AppEntry;
23
31
  subPackageMeta: Record<string, SubPackageMetaValue>;
24
32
  aliasEntries: ResolvedAlias[];
33
+ platform: MpPlatform;
34
+ outputExtensions: OutputExtensions;
35
+ readCommentJson: (filepath: string) => Promise<any>;
36
+ /**
37
+ * esbuild 定义的环境变量
38
+ */
39
+ defineEnv: Record<string, any>;
25
40
  constructor(options?: CompilerContextOptions);
41
+ /**
42
+ * 插件真正计算出来的 define options
43
+ */
44
+ get define(): Record<string, any>;
45
+ setDefineEnv(key: string, value: any): void;
26
46
  get srcRoot(): string;
27
47
  relativeSrcRoot(p: string): string;
28
48
  /**
package/dist/index.d.ts CHANGED
@@ -1,11 +1,19 @@
1
1
  import { PackageJson } from 'pkg-types';
2
2
  import { RollupWatcher, RollupOutput } from 'rollup';
3
- import { P as ProjectConfig, E as Entry, A as AppEntry, S as SubPackageMetaValue, R as ResolvedAlias, C as CompilerContextOptions } from './config-BeGrsxaI.js';
3
+ import { P as ProjectConfig, E as Entry, A as AppEntry, S as SubPackageMetaValue, R as ResolvedAlias, M as MpPlatform, C as CompilerContextOptions } from './config-BeFRKLxC.js';
4
4
  import { InlineConfig } from 'vite';
5
5
  import { Options } from 'tsup';
6
6
  import '@weapp-core/schematics';
7
7
  import 'vite-tsconfig-paths';
8
8
 
9
+ interface OutputExtensions {
10
+ js: string;
11
+ json: string;
12
+ wxml: string;
13
+ wxss: string;
14
+ wxs?: string;
15
+ }
16
+
9
17
  declare class CompilerContext {
10
18
  /**
11
19
  * loadDefaultConfig 的时候会被重新赋予
@@ -22,7 +30,19 @@ declare class CompilerContext {
22
30
  appEntry?: AppEntry;
23
31
  subPackageMeta: Record<string, SubPackageMetaValue>;
24
32
  aliasEntries: ResolvedAlias[];
33
+ platform: MpPlatform;
34
+ outputExtensions: OutputExtensions;
35
+ readCommentJson: (filepath: string) => Promise<any>;
36
+ /**
37
+ * esbuild 定义的环境变量
38
+ */
39
+ defineEnv: Record<string, any>;
25
40
  constructor(options?: CompilerContextOptions);
41
+ /**
42
+ * 插件真正计算出来的 define options
43
+ */
44
+ get define(): Record<string, any>;
45
+ setDefineEnv(key: string, value: any): void;
26
46
  get srcRoot(): string;
27
47
  relativeSrcRoot(p: string): string;
28
48
  /**
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CompilerContext,
3
3
  createCompilerContext
4
- } from "./chunk-MF2E2WZT.mjs";
4
+ } from "./chunk-5PUUYFZM.mjs";
5
5
  import "./chunk-444MQSSG.mjs";
6
6
  export {
7
7
  CompilerContext,
package/dist/json.cjs CHANGED
@@ -27,21 +27,21 @@ __export(json_exports, {
27
27
  defineThemeJson: () => defineThemeJson
28
28
  });
29
29
  module.exports = __toCommonJS(json_exports);
30
- function defineAppJson(config) {
30
+ var defineAppJson = (config) => {
31
31
  return config;
32
- }
33
- function definePageJson(config) {
32
+ };
33
+ var definePageJson = (config) => {
34
34
  return config;
35
- }
36
- function defineComponentJson(config) {
35
+ };
36
+ var defineComponentJson = (config) => {
37
37
  return config;
38
- }
39
- function defineSitemapJson(config) {
38
+ };
39
+ var defineSitemapJson = (config) => {
40
40
  return config;
41
- }
42
- function defineThemeJson(config) {
41
+ };
42
+ var defineThemeJson = (config) => {
43
43
  return config;
44
- }
44
+ };
45
45
  // Annotate the CommonJS export names for ESM import in node:
46
46
  0 && (module.exports = {
47
47
  defineAppJson,
package/dist/json.d.cts CHANGED
@@ -1,11 +1,22 @@
1
- import * as node_modules__weapp_core_schematics_src_type_auto from 'node_modules/@weapp-core/schematics/src/type.auto';
2
1
  import { App, Page, Component, Sitemap, Theme } from '@weapp-core/schematics';
3
2
  export { App, Component, Page, Sitemap, Theme } from '@weapp-core/schematics';
3
+ import { CompilerContext } from './index.cjs';
4
+ import 'pkg-types';
5
+ import 'rollup';
6
+ import './config-BeFRKLxC.cjs';
7
+ import 'vite';
8
+ import 'tsup';
9
+ import 'vite-tsconfig-paths';
4
10
 
5
- declare function defineAppJson(config: App): node_modules__weapp_core_schematics_src_type_auto.App;
6
- declare function definePageJson(config: Page): node_modules__weapp_core_schematics_src_type_auto.Page;
7
- declare function defineComponentJson(config: Component): node_modules__weapp_core_schematics_src_type_auto.Component;
8
- declare function defineSitemapJson(config: Sitemap): node_modules__weapp_core_schematics_src_type_auto.Sitemap;
9
- declare function defineThemeJson(config: Theme): node_modules__weapp_core_schematics_src_type_auto.Theme;
11
+ interface ReturnSelf<T> {
12
+ (config: T): T;
13
+ }
14
+ type ConfigFn<T> = T | ((ctx: CompilerContext) => T);
15
+ type DefineJsonFn<T> = ReturnSelf<ConfigFn<T>>;
16
+ declare const defineAppJson: DefineJsonFn<App>;
17
+ declare const definePageJson: DefineJsonFn<Page>;
18
+ declare const defineComponentJson: DefineJsonFn<Component>;
19
+ declare const defineSitemapJson: DefineJsonFn<Sitemap>;
20
+ declare const defineThemeJson: DefineJsonFn<Theme>;
10
21
 
11
22
  export { defineAppJson, defineComponentJson, definePageJson, defineSitemapJson, defineThemeJson };
package/dist/json.d.ts CHANGED
@@ -1,11 +1,22 @@
1
- import * as node_modules__weapp_core_schematics_src_type_auto from 'node_modules/@weapp-core/schematics/src/type.auto';
2
1
  import { App, Page, Component, Sitemap, Theme } from '@weapp-core/schematics';
3
2
  export { App, Component, Page, Sitemap, Theme } from '@weapp-core/schematics';
3
+ import { CompilerContext } from './index.js';
4
+ import 'pkg-types';
5
+ import 'rollup';
6
+ import './config-BeFRKLxC.js';
7
+ import 'vite';
8
+ import 'tsup';
9
+ import 'vite-tsconfig-paths';
4
10
 
5
- declare function defineAppJson(config: App): node_modules__weapp_core_schematics_src_type_auto.App;
6
- declare function definePageJson(config: Page): node_modules__weapp_core_schematics_src_type_auto.Page;
7
- declare function defineComponentJson(config: Component): node_modules__weapp_core_schematics_src_type_auto.Component;
8
- declare function defineSitemapJson(config: Sitemap): node_modules__weapp_core_schematics_src_type_auto.Sitemap;
9
- declare function defineThemeJson(config: Theme): node_modules__weapp_core_schematics_src_type_auto.Theme;
11
+ interface ReturnSelf<T> {
12
+ (config: T): T;
13
+ }
14
+ type ConfigFn<T> = T | ((ctx: CompilerContext) => T);
15
+ type DefineJsonFn<T> = ReturnSelf<ConfigFn<T>>;
16
+ declare const defineAppJson: DefineJsonFn<App>;
17
+ declare const definePageJson: DefineJsonFn<Page>;
18
+ declare const defineComponentJson: DefineJsonFn<Component>;
19
+ declare const defineSitemapJson: DefineJsonFn<Sitemap>;
20
+ declare const defineThemeJson: DefineJsonFn<Theme>;
10
21
 
11
22
  export { defineAppJson, defineComponentJson, definePageJson, defineSitemapJson, defineThemeJson };
package/dist/json.mjs CHANGED
@@ -1,19 +1,19 @@
1
1
  // src/json.ts
2
- function defineAppJson(config) {
2
+ var defineAppJson = (config) => {
3
3
  return config;
4
- }
5
- function definePageJson(config) {
4
+ };
5
+ var definePageJson = (config) => {
6
6
  return config;
7
- }
8
- function defineComponentJson(config) {
7
+ };
8
+ var defineComponentJson = (config) => {
9
9
  return config;
10
- }
11
- function defineSitemapJson(config) {
10
+ };
11
+ var defineSitemapJson = (config) => {
12
12
  return config;
13
- }
14
- function defineThemeJson(config) {
13
+ };
14
+ var defineThemeJson = (config) => {
15
15
  return config;
16
- }
16
+ };
17
17
  export {
18
18
  defineAppJson,
19
19
  defineComponentJson,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "weapp-vite",
3
3
  "type": "module",
4
- "version": "1.6.6",
4
+ "version": "1.6.7",
5
5
  "description": "weapp-vite 一个现代化的小程序打包工具",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -63,6 +63,7 @@
63
63
  "vite": ">=2.6.0"
64
64
  },
65
65
  "dependencies": {
66
+ "bundle-require": "^5.0.0",
66
67
  "cac": "^6.7.14",
67
68
  "comment-json": "^4.2.5",
68
69
  "debug": "^4.3.7",
@@ -72,13 +73,12 @@
72
73
  "pathe": "^1.1.2",
73
74
  "picomatch": "^4.0.2",
74
75
  "tsup": "^8.3.5",
75
- "tsx": "^4.19.2",
76
76
  "vite-tsconfig-paths": "^5.0.1",
77
77
  "@weapp-core/init": "^1.1.6",
78
- "@weapp-core/schematics": "^1.0.3",
79
78
  "@weapp-core/shared": "^1.0.3",
80
- "weapp-ide-cli": "^2.0.6",
81
- "@weapp-core/logger": "^1.0.1"
79
+ "@weapp-core/schematics": "^1.0.3",
80
+ "@weapp-core/logger": "^1.0.1",
81
+ "weapp-ide-cli": "^2.0.6"
82
82
  },
83
83
  "publishConfig": {
84
84
  "access": "public",