@4399ywkf/core 5.0.7 → 5.0.8

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.
@@ -11,6 +11,107 @@ import { existsSync } from "fs";
11
11
  import { resolve, extname } from "path";
12
12
  import { pathToFileURL } from "url";
13
13
  import deepmerge from "deepmerge";
14
+
15
+ // src/config/schema.ts
16
+ var defaultConfig = {
17
+ appName: "app",
18
+ appCName: "\u5E94\u7528",
19
+ dev: {
20
+ port: 3e3,
21
+ host: "localhost",
22
+ proxy: {},
23
+ https: false
24
+ },
25
+ output: {
26
+ path: "dist",
27
+ publicPath: "/",
28
+ clean: true
29
+ },
30
+ html: {
31
+ title: "\u5E94\u7528",
32
+ template: "public/index.html",
33
+ favicon: "public/favicon.ico",
34
+ mountRoot: "root"
35
+ },
36
+ style: {
37
+ cssModules: true,
38
+ less: { enabled: true, lessOptions: { javascriptEnabled: true } },
39
+ sass: { enabled: true, sassOptions: {} },
40
+ tailwindcss: true
41
+ },
42
+ router: {
43
+ basename: "/",
44
+ conventional: false,
45
+ pagesDir: "src/pages",
46
+ exclude: [
47
+ /\/components?\//,
48
+ /\/models\//,
49
+ /\/utils?\//,
50
+ /^_/,
51
+ /\.d\.ts$/,
52
+ /\.(test|spec|e2e)\.(ts|tsx|js|jsx)$/
53
+ ]
54
+ },
55
+ microFrontend: {
56
+ enabled: false,
57
+ name: "app",
58
+ framework: "qiankun"
59
+ },
60
+ performance: {
61
+ rsdoctor: false,
62
+ splitChunks: true,
63
+ dropConsole: false
64
+ },
65
+ tools: {},
66
+ env: {
67
+ publicEnvFile: "config/env/.env.public",
68
+ envDir: "config/env"
69
+ },
70
+ alias: {},
71
+ plugins: []
72
+ };
73
+
74
+ // src/config/loader.ts
75
+ var CONFIG_FILES = [
76
+ "ywkf.config.ts",
77
+ "ywkf.config.mts",
78
+ "ywkf.config.js",
79
+ "ywkf.config.mjs"
80
+ ];
81
+ function findConfigFile(cwd) {
82
+ for (const file of CONFIG_FILES) {
83
+ const configPath = resolve(cwd, file);
84
+ if (existsSync(configPath)) {
85
+ return configPath;
86
+ }
87
+ }
88
+ return null;
89
+ }
90
+ async function loadTsConfig(configPath) {
91
+ const jiti = (await import("jiti")).default;
92
+ const loader = jiti(configPath, {
93
+ interopDefault: true
94
+ });
95
+ const config = loader(configPath);
96
+ return config.default || config;
97
+ }
98
+ async function loadJsConfig(configPath) {
99
+ const fileUrl = pathToFileURL(configPath).href;
100
+ const module = await import(fileUrl);
101
+ return module.default || module;
102
+ }
103
+ async function loadConfigFile(configPath) {
104
+ const ext = extname(configPath);
105
+ if (ext === ".ts" || ext === ".mts") {
106
+ return loadTsConfig(configPath);
107
+ }
108
+ return loadJsConfig(configPath);
109
+ }
110
+ function mergeConfig(userConfig, baseConfig = defaultConfig) {
111
+ return deepmerge(baseConfig, userConfig, {
112
+ arrayMerge: (_, sourceArray) => sourceArray
113
+ });
114
+ }
14
115
  function createPathResolver(cwd) {
15
116
  return {
16
117
  resolveApp: (relativePath) => resolve(cwd, relativePath),
@@ -355,7 +456,7 @@ function generateEntry(config, injections = {}) {
355
456
  startupBody = [
356
457
  `(async () => {`,
357
458
  ...topLevel.map((l) => ` ${l}`),
358
- ` runApp();`,
459
+ ` await runApp();`,
359
460
  `})();`
360
461
  ].join("\n");
361
462
  } else {
@@ -869,6 +970,16 @@ var YwkfGeneratorPlugin = class {
869
970
  );
870
971
  this.initialized = true;
871
972
  }
973
+ resetState() {
974
+ this.initialized = false;
975
+ this.pluginHooks = [];
976
+ this.generator = null;
977
+ }
978
+ async regenerate() {
979
+ if (this.generator) {
980
+ await this.generator.generate();
981
+ }
982
+ }
872
983
  apply(compiler) {
873
984
  const pluginName = "YwkfGeneratorPlugin";
874
985
  compiler.hooks.beforeCompile.tapAsync(pluginName, async (params, callback) => {
@@ -885,6 +996,7 @@ var YwkfGeneratorPlugin = class {
885
996
  });
886
997
  if (this.options.isDev && !this.isWatching) {
887
998
  this.watchPages();
999
+ this.watchConfig();
888
1000
  }
889
1001
  }
890
1002
  /**
@@ -920,6 +1032,43 @@ var YwkfGeneratorPlugin = class {
920
1032
  watcher.close();
921
1033
  });
922
1034
  }
1035
+ /**
1036
+ * 监听配置文件变化,重新加载配置并重新生成 .ywkf 文件
1037
+ */
1038
+ watchConfig() {
1039
+ const { cwd } = this.options;
1040
+ const configPath = findConfigFile(cwd);
1041
+ if (!configPath) {
1042
+ return;
1043
+ }
1044
+ let debounceTimer = null;
1045
+ const watcher = watch(configPath, async () => {
1046
+ if (debounceTimer) {
1047
+ clearTimeout(debounceTimer);
1048
+ }
1049
+ debounceTimer = setTimeout(async () => {
1050
+ try {
1051
+ console.log(`
1052
+ [ywkf] \u68C0\u6D4B\u5230\u914D\u7F6E\u6587\u4EF6\u53D8\u5316\uFF0C\u91CD\u65B0\u751F\u6210 .ywkf \u76EE\u5F55...
1053
+ `);
1054
+ const userConfig = await loadConfigFile(configPath);
1055
+ const newConfig = mergeConfig(userConfig);
1056
+ this.options.config = newConfig;
1057
+ this.options.pluginConfigs = newConfig.plugins;
1058
+ this.resetState();
1059
+ await this.initialize();
1060
+ await this.regenerate();
1061
+ console.log(` [ywkf] .ywkf \u76EE\u5F55\u5DF2\u91CD\u65B0\u751F\u6210\u3002\u90E8\u5206\u914D\u7F6E\u53D8\u66F4\uFF08\u5982\u7AEF\u53E3\u3001\u4EE3\u7406\uFF09\u9700\u91CD\u542F dev server \u751F\u6548\u3002
1062
+ `);
1063
+ } catch (error) {
1064
+ console.error(` [ywkf] \u914D\u7F6E\u91CD\u8F7D\u5931\u8D25:`, error);
1065
+ }
1066
+ }, 500);
1067
+ });
1068
+ process.on("exit", () => {
1069
+ watcher.close();
1070
+ });
1071
+ }
923
1072
  };
924
1073
 
925
1074
  // src/rspack/base.ts