weapp-vite 1.1.0 → 1.1.1

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.
@@ -0,0 +1,8 @@
1
+ // src/config.ts
2
+ function defineConfig(config) {
3
+ return config;
4
+ }
5
+
6
+ export {
7
+ defineConfig
8
+ };
@@ -1,19 +1,51 @@
1
- // src/build.ts
1
+ // src/context.ts
2
2
  import process2 from "node:process";
3
- import { defu as defu3 } from "@weapp-core/shared";
4
- import path6 from "pathe";
3
+ import path5 from "pathe";
4
+ import { build, loadConfigFromFile } from "vite";
5
5
  import { watch } from "chokidar";
6
- import { build } from "vite";
6
+ import { addExtension as addExtension3, defu as defu3, removeExtension as removeExtension3 } from "@weapp-core/shared";
7
+ import tsconfigPaths from "vite-tsconfig-paths";
8
+ import fs4 from "fs-extra";
7
9
 
8
- // src/symbols.ts
9
- var RootRollupSymbol = Symbol("root-rollup");
10
- var DevSymbol = Symbol("dev");
10
+ // src/utils/projectConfig.ts
11
+ import path from "pathe";
12
+ import fs from "fs-extra";
13
+ function getProjectConfig(root, options) {
14
+ const baseJsonPath = path.resolve(root, "project.config.json");
15
+ const privateJsonPath = path.resolve(root, "project.private.config.json");
16
+ let baseJson = {};
17
+ let privateJson = {};
18
+ if (fs.existsSync(baseJsonPath)) {
19
+ try {
20
+ baseJson = fs.readJsonSync(baseJsonPath) || {};
21
+ } catch {
22
+ throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
23
+ }
24
+ } else {
25
+ throw new Error(`\u5728 ${root} \u76EE\u5F55\u4E0B\u627E\u4E0D\u5230 project.config.json`);
26
+ }
27
+ if (!options?.ignorePrivate) {
28
+ if (fs.existsSync(privateJsonPath)) {
29
+ try {
30
+ privateJson = fs.readJsonSync(privateJsonPath) || {};
31
+ } catch {
32
+ throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.private.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
33
+ }
34
+ }
35
+ }
36
+ return Object.assign({}, privateJson, baseJson);
37
+ }
11
38
 
12
39
  // src/defaults.ts
13
- import { addExtension as addExtension3, removeExtension as removeExtension3 } from "@weapp-core/shared";
14
- import { readPackageJSON } from "pkg-types";
15
- import path5 from "pathe";
16
- import tsconfigPaths from "vite-tsconfig-paths";
40
+ var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
41
+ function getWeappWatchOptions() {
42
+ return {
43
+ paths: ["**/*.{wxml,json,wxs}", "**/*.{png,jpg,jpeg,gif,svg,webp}", ".env", ".env.*"],
44
+ ignored: [
45
+ ...defaultExcluded
46
+ ]
47
+ };
48
+ }
17
49
 
18
50
  // src/plugins/index.ts
19
51
  import path4 from "pathe";
@@ -24,8 +56,8 @@ import { isCSSRequest, preprocessCSS } from "vite";
24
56
  import fg from "fast-glob";
25
57
 
26
58
  // src/utils/scan.ts
27
- import path from "pathe";
28
- import fs from "fs-extra";
59
+ import path2 from "pathe";
60
+ import fs2 from "fs-extra";
29
61
  import { addExtension, defu, isObject, removeExtension } from "@weapp-core/shared";
30
62
  function parseJsonUseComponents(json) {
31
63
  const deps = [];
@@ -44,12 +76,12 @@ function searchAppEntry(options) {
44
76
  formatPath: (x) => x
45
77
  });
46
78
  const extensions = ["js", "ts"];
47
- const appJsonPath = path.resolve(root, "app.json");
48
- if (fs.existsSync(appJsonPath)) {
79
+ const appJsonPath = path2.resolve(root, "app.json");
80
+ if (fs2.existsSync(appJsonPath)) {
49
81
  for (const ext of extensions) {
50
- const entryPath = path.resolve(root, addExtension("app", `.${ext}`));
51
- if (fs.existsSync(entryPath)) {
52
- const appJson = fs.readJSONSync(appJsonPath, { throws: false });
82
+ const entryPath = path2.resolve(root, addExtension("app", `.${ext}`));
83
+ if (fs2.existsSync(entryPath)) {
84
+ const appJson = fs2.readJSONSync(appJsonPath, { throws: false });
53
85
  const deps = [];
54
86
  if (appJson) {
55
87
  if (Array.isArray(appJson.pages)) {
@@ -84,12 +116,12 @@ function searchAppEntry(options) {
84
116
  }
85
117
  }
86
118
  function searchPageEntry(wxmlPath) {
87
- if (fs.existsSync(wxmlPath)) {
119
+ if (fs2.existsSync(wxmlPath)) {
88
120
  const extensions = ["js", "ts"];
89
121
  const base = removeExtension(wxmlPath);
90
122
  for (const ext of extensions) {
91
123
  const entryPath = addExtension(base, `.${ext}`);
92
- if (fs.existsSync(entryPath)) {
124
+ if (fs2.existsSync(entryPath)) {
93
125
  return entryPath;
94
126
  }
95
127
  }
@@ -100,8 +132,8 @@ function getWxmlEntry(wxmlPath, formatPath) {
100
132
  if (pageEntry) {
101
133
  const jsonPath = addExtension(removeExtension(wxmlPath), ".json");
102
134
  const finalPath = formatPath(pageEntry);
103
- if (fs.existsSync(jsonPath)) {
104
- const json = fs.readJsonSync(jsonPath, { throws: false });
135
+ if (fs2.existsSync(jsonPath)) {
136
+ const json = fs2.readJsonSync(jsonPath, { throws: false });
105
137
  if (json && json.component) {
106
138
  return {
107
139
  deps: parseJsonUseComponents(json),
@@ -130,35 +162,6 @@ function getWxmlEntry(wxmlPath, formatPath) {
130
162
  }
131
163
  }
132
164
 
133
- // src/utils/projectConfig.ts
134
- import path2 from "pathe";
135
- import fs2 from "fs-extra";
136
- function getProjectConfig(root, options) {
137
- const baseJsonPath = path2.resolve(root, "project.config.json");
138
- const privateJsonPath = path2.resolve(root, "project.private.config.json");
139
- let baseJson = {};
140
- let privateJson = {};
141
- if (fs2.existsSync(baseJsonPath)) {
142
- try {
143
- baseJson = fs2.readJsonSync(baseJsonPath) || {};
144
- } catch {
145
- throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
146
- }
147
- } else {
148
- throw new Error(`\u5728 ${root} \u76EE\u5F55\u4E0B\u627E\u4E0D\u5230 project.config.json`);
149
- }
150
- if (!options?.ignorePrivate) {
151
- if (fs2.existsSync(privateJsonPath)) {
152
- try {
153
- privateJson = fs2.readJsonSync(privateJsonPath) || {};
154
- } catch {
155
- throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.private.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
156
- }
157
- }
158
- }
159
- return Object.assign({}, privateJson, baseJson);
160
- }
161
-
162
165
  // src/utils/index.ts
163
166
  var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
164
167
  var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
@@ -339,9 +342,6 @@ function vitePluginWeapp(ctx) {
339
342
  // config->configResolved->|watching|options->buildStart
340
343
  config(config, env) {
341
344
  debug?.(config, env);
342
- if (config?.weapp?.srcRoot && !config?.weapp.subPackage && !ctx.srcRootRef.value) {
343
- ctx.srcRootRef.value = config.weapp.srcRoot;
344
- }
345
345
  },
346
346
  configResolved(config) {
347
347
  debug?.(config);
@@ -372,27 +372,15 @@ function vitePluginWeapp(ctx) {
372
372
  const input = getInputOption(paths);
373
373
  entriesSet = new Set(paths);
374
374
  options.input = input;
375
- if (Array.isArray(entries.subPackages) && entries.subPackages.length) {
375
+ if (weapp?.type === "app" && Array.isArray(entries.subPackages) && entries.subPackages.length) {
376
376
  for (const subPackage of entries.subPackages) {
377
- if (subPackage.root && subPackage.independent && !ctx.watcherCache.has(subPackage.root)) {
378
- if (ctx.isDev) {
379
- runDev(ctx, {
380
- weapp: {
381
- subPackage
382
- }
383
- });
384
- } else {
385
- runProd(ctx, {
386
- weapp: {
387
- subPackage
388
- }
389
- });
390
- }
377
+ if (subPackage.root && !ctx.subPackageContextMap.has(subPackage.root)) {
378
+ ctx.forkSubPackage(subPackage).build();
391
379
  }
392
380
  }
393
381
  }
394
382
  } else {
395
- throw new Error(`\u5728 ${path4.join(root, weapp?.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 `);
383
+ 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 `);
396
384
  }
397
385
  },
398
386
  async buildStart() {
@@ -401,12 +389,12 @@ function vitePluginWeapp(ctx) {
401
389
  const ignore = [
402
390
  ...defaultExcluded
403
391
  ];
404
- const isSubPackage = Boolean(!appEntry && weapp?.subPackage && weapp.subPackage.root);
392
+ const isSubPackage = weapp?.type === "subPackage";
405
393
  if (isSubPackage) {
406
- cwd = path4.join(root, weapp.subPackage.root);
394
+ cwd = path4.join(root, ctx.subPackage.root);
407
395
  } else {
408
396
  const ignoreSubPackage = appEntry ? appEntry.deps.filter(
409
- (x) => x.type === "subPackage" && x.independent
397
+ (x) => x.type === "subPackage"
410
398
  ).map((x) => {
411
399
  return `${x.root}/**`;
412
400
  }) : [];
@@ -424,7 +412,7 @@ function vitePluginWeapp(ctx) {
424
412
  }
425
413
  const files = await fg(
426
414
  // 假如去 join root 就是返回 absolute
427
- [path4.join(weapp?.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
415
+ [path4.join(ctx.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
428
416
  {
429
417
  cwd,
430
418
  ignore,
@@ -432,7 +420,7 @@ function vitePluginWeapp(ctx) {
432
420
  }
433
421
  );
434
422
  const relFiles = files.map((x) => {
435
- return isSubPackage ? path4.join(weapp.subPackage.root, x) : x;
423
+ return isSubPackage ? path4.join(weapp?.subPackage?.root ?? "", x) : x;
436
424
  });
437
425
  for (const file of relFiles) {
438
426
  const filepath = path4.resolve(ctx.cwd, file);
@@ -514,121 +502,226 @@ function vitePluginWeapp(ctx) {
514
502
  ];
515
503
  }
516
504
 
517
- // src/defaults.ts
518
- var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
519
- function getWeappWatchOptions() {
520
- return {
521
- paths: ["**/*.{wxml,json,wxs}", "**/*.{png,jpg,jpeg,gif,svg,webp}", ".env", ".env.*"],
522
- ignored: [
523
- ...defaultExcluded
524
- ]
525
- };
526
- }
527
- async function getDefaultViteConfig(ctx) {
528
- const localPackageJson = await readPackageJSON();
529
- const external = [];
530
- if (localPackageJson.dependencies) {
531
- external.push(...Object.keys(localPackageJson.dependencies));
505
+ // src/context.ts
506
+ var CompilerContext = class _CompilerContext {
507
+ inlineConfig;
508
+ cwd;
509
+ isDev;
510
+ projectConfig;
511
+ mode;
512
+ packageJson;
513
+ subPackage;
514
+ watcherMap;
515
+ subPackageContextMap;
516
+ type;
517
+ parent;
518
+ constructor(options) {
519
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, subPackage, type } = defu3(options, {
520
+ cwd: process2.cwd(),
521
+ isDev: false,
522
+ projectConfig: {},
523
+ type: "app",
524
+ inlineConfig: {},
525
+ mode: "",
526
+ packageJson: {}
527
+ });
528
+ this.cwd = cwd;
529
+ this.inlineConfig = inlineConfig;
530
+ this.isDev = isDev;
531
+ this.projectConfig = projectConfig;
532
+ this.mode = mode;
533
+ this.packageJson = packageJson;
534
+ this.subPackage = subPackage;
535
+ this.watcherMap = /* @__PURE__ */ new Map();
536
+ this.subPackageContextMap = /* @__PURE__ */ new Map();
537
+ this.type = type;
532
538
  }
533
- return {
534
- mode: ctx.options.mode,
535
- build: {
536
- rollupOptions: {
537
- output: {
538
- format: "cjs",
539
- strict: false,
540
- entryFileNames: (chunkInfo) => {
541
- const name = ctx.relativeSrcRoot(chunkInfo.name);
542
- if (name.endsWith(".ts")) {
543
- const baseFileName = removeExtension3(name);
544
- if (baseFileName.endsWith(".wxs")) {
545
- return path5.normalize(baseFileName);
546
- }
547
- return path5.normalize(addExtension3(baseFileName, ".js"));
548
- }
549
- return path5.normalize(name);
550
- }
551
- },
552
- external
553
- },
554
- assetsDir: ".",
555
- commonjsOptions: {
556
- transformMixedEsModules: true,
557
- include: void 0
558
- }
559
- },
560
- plugins: [
561
- vitePluginWeapp(ctx),
562
- tsconfigPaths()
563
- ]
564
- };
565
- }
566
-
567
- // src/build.ts
568
- async function runDev(ctx, options) {
569
- if (process2.env.NODE_ENV === void 0) {
570
- process2.env.NODE_ENV = "development";
539
+ get srcRoot() {
540
+ return this.inlineConfig?.weapp?.srcRoot;
571
541
  }
572
- const inlineConfig = defu3(
573
- options,
574
- await getDefaultViteConfig(ctx),
575
- {
576
- mode: "development",
577
- build: {
578
- watch: {},
579
- minify: false,
580
- emptyOutDir: false
581
- }
542
+ relativeSrcRoot(p) {
543
+ if (this.srcRoot) {
544
+ return path5.relative(this.srcRoot, p);
582
545
  }
583
- );
584
- async function innerDev() {
546
+ return p;
547
+ }
548
+ get mpDistRoot() {
549
+ return this.projectConfig.miniprogramRoot || this.projectConfig.srcMiniprogramRoot || "";
550
+ }
551
+ // get weappConfig() {
552
+ // return this.inlineConfig.weapp
553
+ // }
554
+ // get inlineSubPackageConfig() {
555
+ // return this.weappConfig?.subPackage
556
+ // }
557
+ forkSubPackage(subPackage) {
558
+ const ctx = new _CompilerContext({
559
+ cwd: this.cwd,
560
+ isDev: this.isDev,
561
+ projectConfig: this.projectConfig,
562
+ inlineConfig: this.inlineConfig,
563
+ type: "subPackage",
564
+ mode: this.mode,
565
+ subPackage
566
+ });
567
+ this.subPackageContextMap.set(subPackage.root, ctx);
568
+ ctx.parent = this;
569
+ return ctx;
570
+ }
571
+ async internalDev(inlineConfig) {
585
572
  const rollupWatcher = await build(
586
573
  inlineConfig
587
574
  );
588
- const key = options?.weapp?.subPackage?.root || RootRollupSymbol;
589
- const watcher = ctx.watcherCache.get(key);
575
+ const key = "rollup";
576
+ const watcher = this.watcherMap.get(key);
590
577
  watcher?.close();
591
- ctx.watcherCache.set(key, rollupWatcher);
578
+ this.watcherMap.set(key, rollupWatcher);
592
579
  return rollupWatcher;
593
580
  }
594
- if (options?.weapp?.subPackage) {
595
- return await innerDev();
596
- } else {
597
- const { paths, ...opts } = defu3(inlineConfig.weapp?.watch, {
598
- ignored: [
599
- path6.join(ctx.mpRoot, "**")
600
- ],
601
- cwd: ctx.cwd
602
- }, getWeappWatchOptions());
603
- const watcher = watch(paths, opts);
604
- let isReady = false;
605
- watcher.on("all", async (eventName) => {
606
- if (isReady && (eventName === "add" || eventName === "change" || eventName === "unlink")) {
607
- await innerDev();
581
+ async runDev() {
582
+ if (process2.env.NODE_ENV === void 0) {
583
+ process2.env.NODE_ENV = "development";
584
+ }
585
+ const inlineConfig = defu3(
586
+ this.inlineConfig,
587
+ {
588
+ root: this.cwd,
589
+ mode: "development",
590
+ plugins: [vitePluginWeapp(this)],
591
+ build: {
592
+ watch: {},
593
+ minify: false,
594
+ emptyOutDir: false
595
+ },
596
+ weapp: {
597
+ type: "app"
598
+ }
608
599
  }
609
- }).on("ready", async () => {
610
- await innerDev();
611
- isReady = true;
612
- });
613
- return watcher;
600
+ );
601
+ const getWatcher = (paths, opts, inlineConfig2) => {
602
+ const watcher = watch(paths, opts);
603
+ let isReady = false;
604
+ watcher.on("all", async (eventName) => {
605
+ if (isReady && (eventName === "add" || eventName === "change" || eventName === "unlink")) {
606
+ await this.internalDev(inlineConfig2);
607
+ }
608
+ }).on("ready", async () => {
609
+ await this.internalDev(inlineConfig2);
610
+ isReady = true;
611
+ });
612
+ return watcher;
613
+ };
614
+ if (this.type === "subPackage" && this.subPackage) {
615
+ const subPackageInlineConfig = Object.assign({}, inlineConfig, {
616
+ weapp: {
617
+ srcRoot: this.parent?.srcRoot,
618
+ type: this.type,
619
+ subPackage: this.subPackage
620
+ }
621
+ });
622
+ const { paths, ...opts } = defu3(
623
+ subPackageInlineConfig.weapp?.watch,
624
+ {
625
+ cwd: path5.join(this.cwd, subPackageInlineConfig.weapp.srcRoot ?? "", this.subPackage.root)
626
+ },
627
+ getWeappWatchOptions()
628
+ );
629
+ const watcher = getWatcher(paths, opts, subPackageInlineConfig);
630
+ this.watcherMap.set(this.subPackage.root, watcher);
631
+ return watcher;
632
+ } else if (this.type === "app") {
633
+ const { paths, ...opts } = defu3(
634
+ inlineConfig.weapp?.watch,
635
+ {
636
+ ignored: [
637
+ path5.join(this.mpDistRoot, "**")
638
+ ],
639
+ cwd: this.cwd
640
+ },
641
+ getWeappWatchOptions()
642
+ );
643
+ const watcher = getWatcher(paths, opts, inlineConfig);
644
+ this.watcherMap.set("/", watcher);
645
+ return watcher;
646
+ }
614
647
  }
615
- }
616
- async function runProd(ctx, options) {
617
- const inlineConfig = defu3(
618
- options,
619
- await getDefaultViteConfig(ctx),
620
- {
621
- mode: "production"
648
+ async runProd() {
649
+ const inlineConfig = defu3(
650
+ this.inlineConfig,
651
+ {
652
+ root: this.cwd,
653
+ plugins: [vitePluginWeapp(this)],
654
+ mode: "production",
655
+ weapp: {
656
+ type: "app"
657
+ }
658
+ }
659
+ );
660
+ const output = await build(
661
+ inlineConfig
662
+ );
663
+ return output;
664
+ }
665
+ build() {
666
+ if (this.isDev) {
667
+ return this.runDev();
668
+ } else {
669
+ return this.runProd();
622
670
  }
623
- );
624
- const output = await build(
625
- inlineConfig
626
- );
627
- return output;
628
- }
671
+ }
672
+ async loadDefaultConfig() {
673
+ const projectConfig = getProjectConfig(this.cwd);
674
+ this.projectConfig = projectConfig;
675
+ const packageJsonPath = path5.resolve(this.cwd, "package.json");
676
+ const external = [];
677
+ if (await fs4.exists(packageJsonPath)) {
678
+ const localPackageJson = await fs4.readJson(packageJsonPath, {
679
+ throws: false
680
+ }) || {};
681
+ this.packageJson = localPackageJson;
682
+ if (localPackageJson.dependencies) {
683
+ external.push(...Object.keys(localPackageJson.dependencies));
684
+ }
685
+ }
686
+ const loaded = await loadConfigFromFile({
687
+ command: this.isDev ? "serve" : "build",
688
+ mode: this.mode
689
+ }, void 0, this.cwd);
690
+ this.inlineConfig = defu3({
691
+ mode: this.mode,
692
+ build: {
693
+ rollupOptions: {
694
+ output: {
695
+ format: "cjs",
696
+ strict: false,
697
+ entryFileNames: (chunkInfo) => {
698
+ const name = this.relativeSrcRoot(chunkInfo.name);
699
+ if (name.endsWith(".ts")) {
700
+ const baseFileName = removeExtension3(name);
701
+ if (baseFileName.endsWith(".wxs")) {
702
+ return path5.normalize(baseFileName);
703
+ }
704
+ return path5.normalize(addExtension3(baseFileName, ".js"));
705
+ }
706
+ return path5.normalize(name);
707
+ }
708
+ },
709
+ external
710
+ },
711
+ assetsDir: ".",
712
+ commonjsOptions: {
713
+ transformMixedEsModules: true,
714
+ include: void 0
715
+ }
716
+ },
717
+ plugins: [
718
+ tsconfigPaths()
719
+ ],
720
+ configFile: false
721
+ }, loaded?.config, this.inlineConfig);
722
+ }
723
+ };
629
724
 
630
725
  export {
631
- getProjectConfig,
632
- runDev,
633
- runProd
726
+ CompilerContext
634
727
  };