@weapp-core/init 6.0.9 → 6.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -58,6 +58,12 @@ interface PackageJsonData extends JsonObject {
58
58
  devDependencies?: Record<string, string>;
59
59
  }
60
60
  interface TsConfigData extends JsonObject {}
61
+ /**
62
+ * @description init 过程中推断出的项目布局信息。
63
+ */
64
+ interface ProjectLayout {
65
+ srcRoot: string;
66
+ }
61
67
  /**
62
68
  * @description init 过程中单个文件的上下文结构
63
69
  */
@@ -70,6 +76,7 @@ interface ContextDocument<T> {
70
76
  * @description init 过程的上下文容器
71
77
  */
72
78
  interface Context {
79
+ projectLayout: ProjectLayout;
73
80
  projectConfig: ContextDocument<ProjectConfig>;
74
81
  packageJson: ContextDocument<PackageJsonData>;
75
82
  viteConfig: ContextDocument<string>;
@@ -90,9 +97,72 @@ declare function initTsJsonFiles(options: SharedUpdateOptions): Promise<{
90
97
  }[];
91
98
  files: never[];
92
99
  };
93
- tsconfigApp: null;
94
- tsconfigServer: null;
95
- tsconfigNode: null;
100
+ tsconfigApp: {
101
+ extends: string;
102
+ compilerOptions: {
103
+ tsBuildInfoFile: string;
104
+ target: string;
105
+ lib: string[];
106
+ jsx: string;
107
+ module: string;
108
+ moduleResolution: string;
109
+ moduleDetection: string;
110
+ paths: {
111
+ '@/*': string[];
112
+ };
113
+ resolveJsonModule: boolean;
114
+ types: string[];
115
+ allowImportingTsExtensions: boolean;
116
+ allowJs: boolean;
117
+ allowSyntheticDefaultImports: boolean;
118
+ esModuleInterop: boolean;
119
+ isolatedModules: boolean;
120
+ strict: boolean;
121
+ noFallthroughCasesInSwitch: boolean;
122
+ noUnusedLocals: boolean;
123
+ noUnusedParameters: boolean;
124
+ noEmit: boolean;
125
+ verbatimModuleSyntax: boolean;
126
+ noUncheckedSideEffectImports: boolean;
127
+ erasableSyntaxOnly: boolean;
128
+ skipLibCheck: boolean;
129
+ };
130
+ include: string[];
131
+ };
132
+ tsconfigServer: {
133
+ extends: string;
134
+ compilerOptions: {
135
+ tsBuildInfoFile: string;
136
+ target: string;
137
+ lib: string[];
138
+ types: string[];
139
+ };
140
+ files: never[];
141
+ };
142
+ tsconfigNode: {
143
+ extends: string;
144
+ compilerOptions: {
145
+ tsBuildInfoFile: string;
146
+ target: string;
147
+ lib: string[];
148
+ module: string;
149
+ moduleResolution: string;
150
+ moduleDetection: string;
151
+ types: string[];
152
+ allowImportingTsExtensions: boolean;
153
+ resolveJsonModule: boolean;
154
+ verbatimModuleSyntax: boolean;
155
+ strict: boolean;
156
+ noFallthroughCasesInSwitch: boolean;
157
+ noUnusedLocals: boolean;
158
+ noUnusedParameters: boolean;
159
+ noEmit: boolean;
160
+ noUncheckedSideEffectImports: boolean;
161
+ erasableSyntaxOnly: boolean;
162
+ skipLibCheck: boolean;
163
+ };
164
+ include: string[];
165
+ };
96
166
  }>;
97
167
  //#endregion
98
168
  //#region src/packageJson.d.ts
package/dist/index.js CHANGED
@@ -433,7 +433,7 @@ const _path = {
433
433
  };
434
434
  //#endregion
435
435
  //#region ../../node_modules/.pnpm/pathe@2.0.3/node_modules/pathe/dist/index.mjs
436
- const delimiter = globalThis.process?.platform === "win32" ? ";" : ":";
436
+ const delimiter = /* @__PURE__ */ (() => globalThis.process?.platform === "win32" ? ";" : ":")();
437
437
  const _platforms = {
438
438
  posix: void 0,
439
439
  win32: void 0
@@ -462,6 +462,7 @@ function createDocument() {
462
462
  */
463
463
  function createContext() {
464
464
  return {
465
+ projectLayout: { srcRoot: "src" },
465
466
  projectConfig: createDocument(),
466
467
  packageJson: createDocument(),
467
468
  viteConfig: createDocument(),
@@ -483,6 +484,7 @@ const ctx = createContext();
483
484
  */
484
485
  function resetContext() {
485
486
  const next = createContext();
487
+ Object.assign(ctx.projectLayout, next.projectLayout);
486
488
  Object.assign(ctx.projectConfig, next.projectConfig);
487
489
  Object.assign(ctx.packageJson, next.packageJson);
488
490
  Object.assign(ctx.viteConfig, next.viteConfig);
@@ -494,6 +496,33 @@ function resetContext() {
494
496
  }
495
497
  //#endregion
496
498
  //#region src/tsconfigJson.ts
499
+ function normalizeManagedSrcRoot(srcRoot) {
500
+ return srcRoot === "." ? ".." : `../${srcRoot}`;
501
+ }
502
+ function normalizeSrcRoot(srcRoot) {
503
+ if (!srcRoot) return "src";
504
+ if (srcRoot === ".") return ".";
505
+ return srcRoot.replaceAll("\\", "/").replace(/\/+$/u, "");
506
+ }
507
+ function createSrcIncludeGlobs(srcRoot) {
508
+ const prefix = `${normalizeManagedSrcRoot(srcRoot)}/**`;
509
+ return [
510
+ `${prefix}/*.ts`,
511
+ `${prefix}/*.tsx`,
512
+ `${prefix}/*.js`,
513
+ `${prefix}/*.jsx`,
514
+ `${prefix}/*.mts`,
515
+ `${prefix}/*.cts`,
516
+ `${prefix}/*.vue`,
517
+ `${prefix}/*.json`,
518
+ `${prefix}/*.d.ts`
519
+ ];
520
+ }
521
+ const extraIncludeGlobs = [
522
+ "../types/**/*.d.ts",
523
+ "../env.d.ts",
524
+ "./**/*.d.ts"
525
+ ];
497
526
  /**
498
527
  * @description 生成默认 tsconfig.json
499
528
  */
@@ -508,6 +537,126 @@ function getDefaultTsconfigJson() {
508
537
  files: []
509
538
  };
510
539
  }
540
+ /**
541
+ * @description 生成默认 tsconfig.app.json
542
+ */
543
+ function getDefaultTsconfigAppJson(options = {}) {
544
+ const srcRoot = normalizeSrcRoot(options.srcRoot);
545
+ return {
546
+ extends: "./tsconfig.shared.json",
547
+ compilerOptions: {
548
+ tsBuildInfoFile: "../node_modules/.tmp/tsconfig.app.tsbuildinfo",
549
+ target: "ES2023",
550
+ lib: ["ES2023", "DOM"],
551
+ jsx: "preserve",
552
+ module: "ESNext",
553
+ moduleResolution: "bundler",
554
+ moduleDetection: "force",
555
+ paths: { "@/*": [`${normalizeManagedSrcRoot(srcRoot)}/*`] },
556
+ resolveJsonModule: true,
557
+ types: ["miniprogram-api-typings"],
558
+ allowImportingTsExtensions: true,
559
+ allowJs: true,
560
+ allowSyntheticDefaultImports: true,
561
+ esModuleInterop: true,
562
+ isolatedModules: true,
563
+ strict: true,
564
+ noFallthroughCasesInSwitch: true,
565
+ noUnusedLocals: true,
566
+ noUnusedParameters: true,
567
+ noEmit: true,
568
+ verbatimModuleSyntax: true,
569
+ noUncheckedSideEffectImports: true,
570
+ erasableSyntaxOnly: true,
571
+ skipLibCheck: true
572
+ },
573
+ include: [...createSrcIncludeGlobs(srcRoot), ...extraIncludeGlobs]
574
+ };
575
+ }
576
+ /**
577
+ * @description 生成默认 tsconfig.shared.json
578
+ */
579
+ function getDefaultTsconfigSharedJson() {
580
+ return {
581
+ compilerOptions: {
582
+ target: "ES2023",
583
+ module: "ESNext",
584
+ moduleResolution: "bundler",
585
+ moduleDetection: "force",
586
+ resolveJsonModule: true,
587
+ allowImportingTsExtensions: true,
588
+ strict: true,
589
+ noFallthroughCasesInSwitch: true,
590
+ noUnusedLocals: true,
591
+ noUnusedParameters: true,
592
+ noEmit: true,
593
+ verbatimModuleSyntax: true,
594
+ noUncheckedSideEffectImports: true,
595
+ erasableSyntaxOnly: true,
596
+ skipLibCheck: true
597
+ },
598
+ files: ["./tsconfig.shared.empty.d.ts"]
599
+ };
600
+ }
601
+ /**
602
+ * @description 生成默认 tsconfig.node.json
603
+ */
604
+ function getDefaultTsconfigNodeJson(include = []) {
605
+ return {
606
+ extends: "./tsconfig.shared.json",
607
+ compilerOptions: {
608
+ tsBuildInfoFile: "../node_modules/.tmp/tsconfig.node.tsbuildinfo",
609
+ target: "ES2023",
610
+ lib: ["ES2023"],
611
+ module: "ESNext",
612
+ moduleResolution: "bundler",
613
+ moduleDetection: "force",
614
+ types: ["node"],
615
+ allowImportingTsExtensions: true,
616
+ resolveJsonModule: true,
617
+ verbatimModuleSyntax: true,
618
+ strict: true,
619
+ noFallthroughCasesInSwitch: true,
620
+ noUnusedLocals: true,
621
+ noUnusedParameters: true,
622
+ noEmit: true,
623
+ noUncheckedSideEffectImports: true,
624
+ erasableSyntaxOnly: true,
625
+ skipLibCheck: true
626
+ },
627
+ include: [...new Set([...[
628
+ "../vite.config.ts",
629
+ "../vite.config.*.ts",
630
+ "../vite.config.mts",
631
+ "../vite.config.*.mts",
632
+ "../weapp-vite.config.ts",
633
+ "../weapp-vite.config.*.ts",
634
+ "../weapp-vite.config.mts",
635
+ "../weapp-vite.config.*.mts",
636
+ "../*.config.ts",
637
+ "../*.config.mts",
638
+ "../config/**/*.ts",
639
+ "../config/**/*.mts",
640
+ "../scripts/**/*.ts",
641
+ "../scripts/**/*.mts"
642
+ ], ...include])]
643
+ };
644
+ }
645
+ /**
646
+ * @description 生成默认 tsconfig.server.json
647
+ */
648
+ function getDefaultTsconfigServerJson() {
649
+ return {
650
+ extends: "./tsconfig.shared.json",
651
+ compilerOptions: {
652
+ tsBuildInfoFile: "../node_modules/.tmp/tsconfig.server.tsbuildinfo",
653
+ target: "ES2023",
654
+ lib: ["ES2023"],
655
+ types: ["node"]
656
+ },
657
+ files: []
658
+ };
659
+ }
511
660
  //#endregion
512
661
  //#region src/tsDts.ts
513
662
  /**
@@ -601,12 +750,13 @@ function resolveOutputPath(root, dest, fallback) {
601
750
  /**
602
751
  * @description 生成默认的 vite.config.ts 模板
603
752
  */
604
- function getDefaultViteConfig() {
753
+ function getDefaultViteConfig(options = {}) {
754
+ const { srcRoot = "src" } = options;
605
755
  return `import { defineConfig } from 'weapp-vite/config'
606
756
 
607
757
  export default defineConfig({
608
758
  weapp: {
609
- // weapp-vite options
759
+ ${srcRoot === "src" ? " // weapp-vite options" : ` srcRoot: '${srcRoot}',`}
610
760
  },
611
761
  })
612
762
  `;
@@ -618,7 +768,7 @@ async function initViteConfigFile(options) {
618
768
  const packageType = ctx.packageJson.value?.type ?? "module";
619
769
  const targetFilename = ctx.viteConfig.name = packageType === "module" ? "vite.config.ts" : "vite.config.mts";
620
770
  const outputPath = resolveOutputPath(root, dest, ctx.viteConfig.path = posix.resolve(root, targetFilename));
621
- const code = getDefaultViteConfig();
771
+ const code = getDefaultViteConfig({ srcRoot: ctx.projectLayout.srcRoot });
622
772
  ctx.viteConfig.value = code;
623
773
  if (write) {
624
774
  await writeFile(outputPath, code);
@@ -649,25 +799,59 @@ async function initTsJsonFiles(options) {
649
799
  ctx.tsconfigNode.name = ".weapp-vite/tsconfig.node.json";
650
800
  ctx.tsconfigNode.path = posix.resolve(root, ctx.tsconfigNode.name);
651
801
  const tsconfig = getDefaultTsconfigJson();
802
+ const tsconfigSharedName = ".weapp-vite/tsconfig.shared.json";
803
+ const tsconfigSharedEmptyName = ".weapp-vite/tsconfig.shared.empty.d.ts";
804
+ const tsconfigSharedPath = posix.resolve(root, tsconfigSharedName);
805
+ const tsconfigSharedEmptyPath = posix.resolve(root, tsconfigSharedEmptyName);
806
+ const tsconfigApp = getDefaultTsconfigAppJson({ srcRoot: ctx.projectLayout.srcRoot });
807
+ const tsconfigNode = getDefaultTsconfigNodeJson();
808
+ const tsconfigServer = getDefaultTsconfigServerJson();
809
+ const tsconfigShared = getDefaultTsconfigSharedJson();
652
810
  ctx.tsconfig.value = tsconfig;
653
- ctx.tsconfigApp.value = null;
654
- ctx.tsconfigServer.value = null;
655
- ctx.tsconfigNode.value = null;
811
+ ctx.tsconfigApp.value = tsconfigApp;
812
+ ctx.tsconfigServer.value = tsconfigServer;
813
+ ctx.tsconfigNode.value = tsconfigNode;
656
814
  if (write) {
657
815
  const tsconfigOutputPath = resolveOutputPath(root, dest, tsJsonFilePath);
658
816
  await writeJsonFile(tsconfigOutputPath, tsconfig);
659
817
  logger.log(`✨ 写入 ${posix.relative(root, tsconfigOutputPath)} 成功!`);
818
+ const managedFiles = [
819
+ {
820
+ filePath: ctx.tsconfigApp.path,
821
+ value: tsconfigApp
822
+ },
823
+ {
824
+ filePath: ctx.tsconfigNode.path,
825
+ value: tsconfigNode
826
+ },
827
+ {
828
+ filePath: ctx.tsconfigServer.path,
829
+ value: tsconfigServer
830
+ },
831
+ {
832
+ filePath: tsconfigSharedPath,
833
+ value: tsconfigShared
834
+ }
835
+ ];
836
+ for (const file of managedFiles) {
837
+ const outputPath = resolveOutputPath(root, dest, file.filePath);
838
+ await writeJsonFile(outputPath, file.value);
839
+ logger.log(`✨ 写入 ${posix.relative(root, outputPath)} 成功!`);
840
+ }
841
+ const sharedEmptyOutputPath = resolveOutputPath(root, dest, tsconfigSharedEmptyPath);
842
+ await writeFile(sharedEmptyOutputPath, "export {}\n");
843
+ logger.log(`✨ 写入 ${posix.relative(root, sharedEmptyOutputPath)} 成功!`);
660
844
  }
661
845
  return {
662
846
  tsconfig,
663
- tsconfigApp: null,
664
- tsconfigServer: null,
665
- tsconfigNode: null
847
+ tsconfigApp,
848
+ tsconfigServer,
849
+ tsconfigNode
666
850
  };
667
851
  }
668
852
  //#endregion
669
853
  //#region ../../packages/weapp-vite/package.json
670
- var version = "6.16.18";
854
+ var version = "6.16.41";
671
855
  //#endregion
672
856
  //#region src/npm.ts
673
857
  /**
@@ -715,7 +899,7 @@ async function latestVersion(packageName, prefix = "^", fetch = getLatestVersion
715
899
  const FALLBACK_DEP_VERSIONS = {
716
900
  "miniprogram-api-typings": "^4.1.0",
717
901
  "typescript": "^5.9.2",
718
- "weapp-tailwindcss": "^4.3.3"
902
+ "weapp-tailwindcss": "^5.0.4"
719
903
  };
720
904
  /**
721
905
  * @description 创建默认 package.json 模板
@@ -759,12 +943,13 @@ async function createOrUpdatePackageJson(options) {
759
943
  packageJson = createDefaultPackageJson();
760
944
  logger.info(`✨ 没有找到 ${packageJsonFilename} 文件,正在创建默认 package.json ...`);
761
945
  }
762
- set(packageJson, "scripts.dev", `${command} dev`);
763
- set(packageJson, "scripts.dev:open", `${command} dev -o`);
764
- set(packageJson, "scripts.build", `${command} build`);
946
+ const scriptCommand = command === "weapp-vite" ? "wv" : command;
947
+ set(packageJson, "scripts.dev", `${scriptCommand} dev`);
948
+ set(packageJson, "scripts.dev:open", `${scriptCommand} dev -o`);
949
+ set(packageJson, "scripts.build", `${scriptCommand} build`);
765
950
  if (command === "weapp-vite") {
766
- set(packageJson, "scripts.open", `${command} open`);
767
- set(packageJson, "scripts.g", `${command} generate`);
951
+ set(packageJson, "scripts.open", `${scriptCommand} open`);
952
+ set(packageJson, "scripts.g", `${scriptCommand} generate`);
768
953
  set(packageJson, "devDependencies.weapp-vite", `^${version}`);
769
954
  await Promise.all([upsertDependencyVersion(packageJson, "devDependencies.miniprogram-api-typings", "miniprogram-api-typings", { skipNetwork: !write }), upsertDependencyVersion(packageJson, "devDependencies.typescript", "typescript", { skipNetwork: !write })]);
770
955
  }
@@ -870,6 +1055,37 @@ async function createOrUpdateProjectConfig(options) {
870
1055
  }
871
1056
  }
872
1057
  //#endregion
1058
+ //#region src/projectLayout.ts
1059
+ const TRAILING_SLASH_RE = /\/+$/u;
1060
+ const LEADING_DOT_SLASH_RE = /^\.\//u;
1061
+ function normalizeRelativeDir(value) {
1062
+ return value.replaceAll("\\", "/").replace(LEADING_DOT_SLASH_RE, "").replace(TRAILING_SLASH_RE, "") || ".";
1063
+ }
1064
+ function isSafeRelativeDir(value) {
1065
+ return typeof value === "string" && value.trim().length > 0 && !posix.isAbsolute(value) && !value.split(/[\\/]+/u).includes("..");
1066
+ }
1067
+ async function hasAppJson(root, srcRoot) {
1068
+ return await fs.pathExists(posix.resolve(root, srcRoot, "app.json"));
1069
+ }
1070
+ function pushCandidate(candidates, value) {
1071
+ if (!isSafeRelativeDir(value)) return;
1072
+ const normalized = normalizeRelativeDir(value);
1073
+ if (!candidates.includes(normalized)) candidates.push(normalized);
1074
+ }
1075
+ /**
1076
+ * @description 根据原生小程序项目配置推断源码根目录。
1077
+ */
1078
+ async function resolveProjectSrcRoot(root, projectConfig) {
1079
+ const candidates = [];
1080
+ pushCandidate(candidates, projectConfig?.srcMiniprogramRoot);
1081
+ pushCandidate(candidates, projectConfig?.miniprogramRoot);
1082
+ pushCandidate(candidates, ".");
1083
+ pushCandidate(candidates, "src");
1084
+ pushCandidate(candidates, "miniprogram");
1085
+ for (const candidate of candidates) if (await hasAppJson(root, candidate)) return candidate;
1086
+ return "src";
1087
+ }
1088
+ //#endregion
873
1089
  //#region src/gitignore.ts
874
1090
  const DEFAULT_GITIGNORE = `# dependencies
875
1091
  node_modules
@@ -963,7 +1179,8 @@ async function updateGitIgnore(options) {
963
1179
  */
964
1180
  async function initConfig(options) {
965
1181
  const { root = process$1.cwd(), command } = options;
966
- await createOrUpdateProjectConfig({ root });
1182
+ const projectConfig = await createOrUpdateProjectConfig({ root });
1183
+ ctx.projectLayout.srcRoot = await resolveProjectSrcRoot(root, projectConfig);
967
1184
  await createOrUpdatePackageJson({
968
1185
  root,
969
1186
  command
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-core/init",
3
3
  "type": "module",
4
- "version": "6.0.9",
4
+ "version": "6.0.10",
5
5
  "description": "@weapp-core/init",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",