@weapp-core/init 6.0.8 → 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
  /**
@@ -523,6 +672,8 @@ function getDefaultTsDts() {
523
672
  * @description 文件读取错误
524
673
  */
525
674
  var FsReadError = class extends Error {
675
+ filepath;
676
+ cause;
526
677
  constructor(filepath, cause) {
527
678
  super(`Failed to read ${filepath}`);
528
679
  this.filepath = filepath;
@@ -534,6 +685,8 @@ var FsReadError = class extends Error {
534
685
  * @description 文件写入错误
535
686
  */
536
687
  var FsWriteError = class extends Error {
688
+ filepath;
689
+ cause;
537
690
  constructor(filepath, cause) {
538
691
  super(`Failed to write ${filepath}`);
539
692
  this.filepath = filepath;
@@ -597,12 +750,13 @@ function resolveOutputPath(root, dest, fallback) {
597
750
  /**
598
751
  * @description 生成默认的 vite.config.ts 模板
599
752
  */
600
- function getDefaultViteConfig() {
753
+ function getDefaultViteConfig(options = {}) {
754
+ const { srcRoot = "src" } = options;
601
755
  return `import { defineConfig } from 'weapp-vite/config'
602
756
 
603
757
  export default defineConfig({
604
758
  weapp: {
605
- // weapp-vite options
759
+ ${srcRoot === "src" ? " // weapp-vite options" : ` srcRoot: '${srcRoot}',`}
606
760
  },
607
761
  })
608
762
  `;
@@ -614,7 +768,7 @@ async function initViteConfigFile(options) {
614
768
  const packageType = ctx.packageJson.value?.type ?? "module";
615
769
  const targetFilename = ctx.viteConfig.name = packageType === "module" ? "vite.config.ts" : "vite.config.mts";
616
770
  const outputPath = resolveOutputPath(root, dest, ctx.viteConfig.path = posix.resolve(root, targetFilename));
617
- const code = getDefaultViteConfig();
771
+ const code = getDefaultViteConfig({ srcRoot: ctx.projectLayout.srcRoot });
618
772
  ctx.viteConfig.value = code;
619
773
  if (write) {
620
774
  await writeFile(outputPath, code);
@@ -645,25 +799,59 @@ async function initTsJsonFiles(options) {
645
799
  ctx.tsconfigNode.name = ".weapp-vite/tsconfig.node.json";
646
800
  ctx.tsconfigNode.path = posix.resolve(root, ctx.tsconfigNode.name);
647
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();
648
810
  ctx.tsconfig.value = tsconfig;
649
- ctx.tsconfigApp.value = null;
650
- ctx.tsconfigServer.value = null;
651
- ctx.tsconfigNode.value = null;
811
+ ctx.tsconfigApp.value = tsconfigApp;
812
+ ctx.tsconfigServer.value = tsconfigServer;
813
+ ctx.tsconfigNode.value = tsconfigNode;
652
814
  if (write) {
653
815
  const tsconfigOutputPath = resolveOutputPath(root, dest, tsJsonFilePath);
654
816
  await writeJsonFile(tsconfigOutputPath, tsconfig);
655
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)} 成功!`);
656
844
  }
657
845
  return {
658
846
  tsconfig,
659
- tsconfigApp: null,
660
- tsconfigServer: null,
661
- tsconfigNode: null
847
+ tsconfigApp,
848
+ tsconfigServer,
849
+ tsconfigNode
662
850
  };
663
851
  }
664
852
  //#endregion
665
853
  //#region ../../packages/weapp-vite/package.json
666
- var version = "6.15.8";
854
+ var version = "6.16.41";
667
855
  //#endregion
668
856
  //#region src/npm.ts
669
857
  /**
@@ -711,7 +899,7 @@ async function latestVersion(packageName, prefix = "^", fetch = getLatestVersion
711
899
  const FALLBACK_DEP_VERSIONS = {
712
900
  "miniprogram-api-typings": "^4.1.0",
713
901
  "typescript": "^5.9.2",
714
- "weapp-tailwindcss": "^4.3.3"
902
+ "weapp-tailwindcss": "^5.0.4"
715
903
  };
716
904
  /**
717
905
  * @description 创建默认 package.json 模板
@@ -755,12 +943,13 @@ async function createOrUpdatePackageJson(options) {
755
943
  packageJson = createDefaultPackageJson();
756
944
  logger.info(`✨ 没有找到 ${packageJsonFilename} 文件,正在创建默认 package.json ...`);
757
945
  }
758
- set(packageJson, "scripts.dev", `${command} dev`);
759
- set(packageJson, "scripts.dev:open", `${command} dev -o`);
760
- 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`);
761
950
  if (command === "weapp-vite") {
762
- set(packageJson, "scripts.open", `${command} open`);
763
- set(packageJson, "scripts.g", `${command} generate`);
951
+ set(packageJson, "scripts.open", `${scriptCommand} open`);
952
+ set(packageJson, "scripts.g", `${scriptCommand} generate`);
764
953
  set(packageJson, "devDependencies.weapp-vite", `^${version}`);
765
954
  await Promise.all([upsertDependencyVersion(packageJson, "devDependencies.miniprogram-api-typings", "miniprogram-api-typings", { skipNetwork: !write }), upsertDependencyVersion(packageJson, "devDependencies.typescript", "typescript", { skipNetwork: !write })]);
766
955
  }
@@ -866,6 +1055,37 @@ async function createOrUpdateProjectConfig(options) {
866
1055
  }
867
1056
  }
868
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
869
1089
  //#region src/gitignore.ts
870
1090
  const DEFAULT_GITIGNORE = `# dependencies
871
1091
  node_modules
@@ -922,17 +1142,17 @@ function ensureTrailingNewline(value) {
922
1142
  function mergeGitignore(existing) {
923
1143
  const normalizedExisting = normalizeLineEndings(existing ?? "");
924
1144
  const merged = [...normalizedExisting.length ? normalizedExisting.split("\n") : []];
925
- while (merged.length > 0 && merged.at(-1) === "") merged.pop();
1145
+ while (merged.length > 0 && merged[merged.length - 1] === "") merged.pop();
926
1146
  const seen = new Set(merged);
927
1147
  let appendedNonBlank = false;
928
1148
  for (const line of DEFAULT_GITIGNORE.split("\n")) {
929
1149
  if (line.length === 0) {
930
- if (merged.length === 0 || merged.at(-1) === "") continue;
1150
+ if (merged.length === 0 || merged[merged.length - 1] === "") continue;
931
1151
  merged.push("");
932
1152
  continue;
933
1153
  }
934
1154
  if (seen.has(line)) continue;
935
- if (!appendedNonBlank && merged.length > 0 && merged.at(-1) !== "") merged.push("");
1155
+ if (!appendedNonBlank && merged.length > 0 && merged[merged.length - 1] !== "") merged.push("");
936
1156
  merged.push(line);
937
1157
  seen.add(line);
938
1158
  appendedNonBlank = true;
@@ -959,7 +1179,8 @@ async function updateGitIgnore(options) {
959
1179
  */
960
1180
  async function initConfig(options) {
961
1181
  const { root = process$1.cwd(), command } = options;
962
- await createOrUpdateProjectConfig({ root });
1182
+ const projectConfig = await createOrUpdateProjectConfig({ root });
1183
+ ctx.projectLayout.srcRoot = await resolveProjectSrcRoot(root, projectConfig);
963
1184
  await createOrUpdatePackageJson({
964
1185
  root,
965
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.8",
4
+ "version": "6.0.10",
5
5
  "description": "@weapp-core/init",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -39,8 +39,8 @@
39
39
  "node": "^20.19.0 || >=22.12.0"
40
40
  },
41
41
  "dependencies": {
42
- "@weapp-core/logger": "^3.1.1",
43
- "@weapp-core/shared": "^3.0.4"
42
+ "@weapp-core/logger": "3.1.1",
43
+ "@weapp-core/shared": "3.0.4"
44
44
  },
45
45
  "scripts": {
46
46
  "dev": "tsdown -w --sourcemap",