@4399ywkf/core 5.1.1 → 5.1.3
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/cli/index.js +237 -149
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +7 -2
- package/dist/config/index.js +17 -10
- package/dist/config/index.js.map +1 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.js +247 -155
- package/dist/index.js.map +1 -1
- package/dist/router/index.js +7 -3
- package/dist/router/index.js.map +1 -1
- package/dist/rspack/index.js +6 -148
- package/dist/rspack/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -879,7 +879,7 @@ function createMockMiddleware(mockPath, options) {
|
|
|
879
879
|
scanMockFiles(mockPath);
|
|
880
880
|
return async (_req, _res, next) => {
|
|
881
881
|
if (delay > 0) {
|
|
882
|
-
await new Promise((
|
|
882
|
+
await new Promise((resolve5) => setTimeout(resolve5, delay));
|
|
883
883
|
}
|
|
884
884
|
next();
|
|
885
885
|
};
|
|
@@ -1833,11 +1833,11 @@ var zustandPlugin = createPlugin((options = {}) => ({
|
|
|
1833
1833
|
const hooks = {
|
|
1834
1834
|
modifyRspackConfig(rspackConfig) {
|
|
1835
1835
|
logger.info("Zustand \u5DF2\u542F\u7528");
|
|
1836
|
-
const
|
|
1837
|
-
const alias =
|
|
1836
|
+
const resolve5 = rspackConfig.resolve || {};
|
|
1837
|
+
const alias = resolve5.alias || {};
|
|
1838
1838
|
alias["@store"] = storePath;
|
|
1839
|
-
|
|
1840
|
-
rspackConfig.resolve =
|
|
1839
|
+
resolve5.alias = alias;
|
|
1840
|
+
rspackConfig.resolve = resolve5;
|
|
1841
1841
|
return rspackConfig;
|
|
1842
1842
|
},
|
|
1843
1843
|
generateFiles(_ctx) {
|
|
@@ -2127,7 +2127,7 @@ var defaultConfig = {
|
|
|
2127
2127
|
strictMode: true,
|
|
2128
2128
|
dev: {
|
|
2129
2129
|
port: 3e3,
|
|
2130
|
-
host: "
|
|
2130
|
+
host: "127.0.0.1",
|
|
2131
2131
|
proxy: {},
|
|
2132
2132
|
https: false
|
|
2133
2133
|
},
|
|
@@ -2191,32 +2191,39 @@ function findConfigFile(cwd) {
|
|
|
2191
2191
|
}
|
|
2192
2192
|
return null;
|
|
2193
2193
|
}
|
|
2194
|
-
async function loadTsConfig(configPath) {
|
|
2194
|
+
async function loadTsConfig(configPath, fresh = false) {
|
|
2195
2195
|
const jiti = (await import("jiti")).default;
|
|
2196
2196
|
const loader = jiti(configPath, {
|
|
2197
|
-
interopDefault: true
|
|
2197
|
+
interopDefault: true,
|
|
2198
|
+
// fresh(热重启)时必须禁用缓存:jiti 的 moduleCache 默认与 Node 原生 require 缓存集成,
|
|
2199
|
+
// 即使新建 jiti 实例,loader(configPath) 仍会命中 Node 全局缓存读回旧配置;
|
|
2200
|
+
// fsCache 为转译产物缓存,一并关闭以确保读到最新源码。
|
|
2201
|
+
...fresh ? { moduleCache: false, fsCache: false } : {}
|
|
2198
2202
|
});
|
|
2199
2203
|
const config = loader(configPath);
|
|
2200
2204
|
return config.default || config;
|
|
2201
2205
|
}
|
|
2202
|
-
async function loadJsConfig(configPath) {
|
|
2203
|
-
|
|
2206
|
+
async function loadJsConfig(configPath, fresh = false) {
|
|
2207
|
+
let fileUrl = pathToFileURL(configPath).href;
|
|
2208
|
+
if (fresh) {
|
|
2209
|
+
fileUrl += `?t=${Date.now()}`;
|
|
2210
|
+
}
|
|
2204
2211
|
const module = await import(fileUrl);
|
|
2205
2212
|
return module.default || module;
|
|
2206
2213
|
}
|
|
2207
|
-
async function loadConfigFile(configPath) {
|
|
2214
|
+
async function loadConfigFile(configPath, opts = {}) {
|
|
2208
2215
|
const ext = extname(configPath);
|
|
2209
2216
|
if (ext === ".ts" || ext === ".mts") {
|
|
2210
|
-
return loadTsConfig(configPath);
|
|
2217
|
+
return loadTsConfig(configPath, opts.fresh);
|
|
2211
2218
|
}
|
|
2212
|
-
return loadJsConfig(configPath);
|
|
2219
|
+
return loadJsConfig(configPath, opts.fresh);
|
|
2213
2220
|
}
|
|
2214
2221
|
function mergeConfig(userConfig, baseConfig = defaultConfig) {
|
|
2215
2222
|
return deepmerge(baseConfig, userConfig, {
|
|
2216
2223
|
arrayMerge: (_, sourceArray) => sourceArray
|
|
2217
2224
|
});
|
|
2218
2225
|
}
|
|
2219
|
-
async function resolveConfig(cwd) {
|
|
2226
|
+
async function resolveConfig(cwd, opts = {}) {
|
|
2220
2227
|
const configPath = findConfigFile(cwd);
|
|
2221
2228
|
if (!configPath) {
|
|
2222
2229
|
console.warn("\u26A0\uFE0F \u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6 (ywkf.config.ts)\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u914D\u7F6E");
|
|
@@ -2226,7 +2233,7 @@ async function resolveConfig(cwd) {
|
|
|
2226
2233
|
};
|
|
2227
2234
|
}
|
|
2228
2235
|
try {
|
|
2229
|
-
const userConfig = await loadConfigFile(configPath);
|
|
2236
|
+
const userConfig = await loadConfigFile(configPath, opts);
|
|
2230
2237
|
const normalized = normalizeConfig(userConfig);
|
|
2231
2238
|
const config = mergeConfig(normalized);
|
|
2232
2239
|
return { config, configPath };
|
|
@@ -3374,16 +3381,6 @@ var YwkfGeneratorPlugin = class {
|
|
|
3374
3381
|
);
|
|
3375
3382
|
this.initialized = true;
|
|
3376
3383
|
}
|
|
3377
|
-
resetState() {
|
|
3378
|
-
this.initialized = false;
|
|
3379
|
-
this.pluginHooks = [];
|
|
3380
|
-
this.generator = null;
|
|
3381
|
-
}
|
|
3382
|
-
async regenerate() {
|
|
3383
|
-
if (this.generator) {
|
|
3384
|
-
await this.generator.generate();
|
|
3385
|
-
}
|
|
3386
|
-
}
|
|
3387
3384
|
apply(compiler) {
|
|
3388
3385
|
const pluginName = "YwkfGeneratorPlugin";
|
|
3389
3386
|
compiler.hooks.beforeCompile.tapAsync(pluginName, async (_params, callback) => {
|
|
@@ -3399,14 +3396,13 @@ var YwkfGeneratorPlugin = class {
|
|
|
3399
3396
|
}
|
|
3400
3397
|
});
|
|
3401
3398
|
if (this.options.isDev && !this.isWatching) {
|
|
3402
|
-
this.watchPages();
|
|
3403
|
-
this.watchConfig();
|
|
3399
|
+
this.watchPages(compiler);
|
|
3404
3400
|
}
|
|
3405
3401
|
}
|
|
3406
3402
|
/**
|
|
3407
3403
|
* 监听页面目录变化
|
|
3408
3404
|
*/
|
|
3409
|
-
watchPages() {
|
|
3405
|
+
watchPages(compiler) {
|
|
3410
3406
|
const { config, cwd } = this.options;
|
|
3411
3407
|
const pagesDir = join11(cwd, config.router.pagesDir || "src/pages");
|
|
3412
3408
|
if (!existsSync11(pagesDir)) {
|
|
@@ -3428,47 +3424,11 @@ var YwkfGeneratorPlugin = class {
|
|
|
3428
3424
|
}
|
|
3429
3425
|
}, 500);
|
|
3430
3426
|
});
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
});
|
|
3434
|
-
}
|
|
3435
|
-
/**
|
|
3436
|
-
* 监听配置文件变化,重新加载配置并重新生成 .ywkf 文件
|
|
3437
|
-
*/
|
|
3438
|
-
watchConfig() {
|
|
3439
|
-
const { cwd } = this.options;
|
|
3440
|
-
const configPath = findConfigFile(cwd);
|
|
3441
|
-
if (!configPath) {
|
|
3442
|
-
return;
|
|
3443
|
-
}
|
|
3444
|
-
let debounceTimer = null;
|
|
3445
|
-
const watcher = watch(configPath, async () => {
|
|
3446
|
-
if (debounceTimer) {
|
|
3447
|
-
clearTimeout(debounceTimer);
|
|
3448
|
-
}
|
|
3449
|
-
debounceTimer = setTimeout(async () => {
|
|
3450
|
-
try {
|
|
3451
|
-
console.log(`
|
|
3452
|
-
[ywkf] \u68C0\u6D4B\u5230\u914D\u7F6E\u6587\u4EF6\u53D8\u5316\uFF0C\u91CD\u65B0\u751F\u6210 .ywkf \u76EE\u5F55...
|
|
3453
|
-
`);
|
|
3454
|
-
const userConfig = await loadConfigFile(configPath);
|
|
3455
|
-
const newConfig = mergeConfig(userConfig);
|
|
3456
|
-
this.options.config = newConfig;
|
|
3457
|
-
this.options.pluginConfigs = newConfig.plugins;
|
|
3458
|
-
this.resetState();
|
|
3459
|
-
await this.initialize();
|
|
3460
|
-
await this.regenerate();
|
|
3461
|
-
console.log(
|
|
3462
|
-
` [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
|
|
3463
|
-
`
|
|
3464
|
-
);
|
|
3465
|
-
} catch (error) {
|
|
3466
|
-
console.error(` [ywkf] \u914D\u7F6E\u91CD\u8F7D\u5931\u8D25:`, error);
|
|
3467
|
-
}
|
|
3468
|
-
}, 500);
|
|
3469
|
-
});
|
|
3470
|
-
process.on("exit", () => {
|
|
3427
|
+
compiler.hooks.shutdown.tap("YwkfGeneratorPlugin", () => {
|
|
3428
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
3471
3429
|
watcher.close();
|
|
3430
|
+
this.isWatching = false;
|
|
3431
|
+
this.hasGenerated = false;
|
|
3472
3432
|
});
|
|
3473
3433
|
}
|
|
3474
3434
|
};
|
|
@@ -4209,6 +4169,10 @@ function getNetworkAddress(port) {
|
|
|
4209
4169
|
}
|
|
4210
4170
|
return null;
|
|
4211
4171
|
}
|
|
4172
|
+
function displayHost(host) {
|
|
4173
|
+
if (host === "0.0.0.0" || host === "::") return "localhost";
|
|
4174
|
+
return host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
|
|
4175
|
+
}
|
|
4212
4176
|
var BAR_WIDTH = 30;
|
|
4213
4177
|
function renderBar(percent) {
|
|
4214
4178
|
const filled = Math.round(BAR_WIDTH * percent);
|
|
@@ -4310,7 +4274,7 @@ var DevPrinter = class {
|
|
|
4310
4274
|
* 打印服务器信息面板(dev 模式使用)
|
|
4311
4275
|
*/
|
|
4312
4276
|
printServerInfo() {
|
|
4313
|
-
const localUrl = `http
|
|
4277
|
+
const localUrl = `http://${displayHost(this._host)}:${this.port}/`;
|
|
4314
4278
|
const networkUrl = getNetworkAddress(this.port);
|
|
4315
4279
|
console.log(` ${chalk.bold(">")} Local: ${chalk.cyan(localUrl)}`);
|
|
4316
4280
|
if (networkUrl) {
|
|
@@ -4338,7 +4302,7 @@ function createProgressHandler(printer) {
|
|
|
4338
4302
|
printer.updateProgress(percent, message);
|
|
4339
4303
|
};
|
|
4340
4304
|
}
|
|
4341
|
-
function registerShortcuts(
|
|
4305
|
+
function registerShortcuts(getPrinter, opts) {
|
|
4342
4306
|
if (!process.stdin.isTTY) return;
|
|
4343
4307
|
process.stdin.setEncoding("utf-8");
|
|
4344
4308
|
process.stdin.setRawMode?.(false);
|
|
@@ -4350,10 +4314,10 @@ function registerShortcuts(printer, opts) {
|
|
|
4350
4314
|
buffer = "";
|
|
4351
4315
|
switch (cmd) {
|
|
4352
4316
|
case "h":
|
|
4353
|
-
|
|
4317
|
+
getPrinter().printShortcuts();
|
|
4354
4318
|
break;
|
|
4355
4319
|
case "o": {
|
|
4356
|
-
const url = `http
|
|
4320
|
+
const url = `http://${displayHost(opts.getHost())}:${opts.getPort()}/`;
|
|
4357
4321
|
import("child_process").then((cp) => {
|
|
4358
4322
|
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
4359
4323
|
cp.exec(`${command} ${url}`);
|
|
@@ -4426,7 +4390,7 @@ async function build(options = {}) {
|
|
|
4426
4390
|
let rspackConfig = createRspackConfig(config, cwd, { isDev: false });
|
|
4427
4391
|
rspackConfig = await pluginManager.applyRspackConfigHooks(rspackConfig);
|
|
4428
4392
|
const pluginNames = pluginManager.getPluginNames();
|
|
4429
|
-
const printer = new DevPrinter("
|
|
4393
|
+
const printer = new DevPrinter("127.0.0.1", 0, pluginNames, true);
|
|
4430
4394
|
printer.printBanner();
|
|
4431
4395
|
printer.printBuildStart();
|
|
4432
4396
|
printer.updateProgress(0, "preparing");
|
|
@@ -4435,7 +4399,7 @@ async function build(options = {}) {
|
|
|
4435
4399
|
rspackConfig.stats = "none";
|
|
4436
4400
|
rspackConfig.infrastructureLogging = { level: "none" };
|
|
4437
4401
|
const compiler = rspack4(rspackConfig);
|
|
4438
|
-
const stats = await new Promise((
|
|
4402
|
+
const stats = await new Promise((resolve5, reject) => {
|
|
4439
4403
|
compiler.run((err, stats2) => {
|
|
4440
4404
|
if (err) {
|
|
4441
4405
|
reject(err);
|
|
@@ -4445,13 +4409,13 @@ async function build(options = {}) {
|
|
|
4445
4409
|
reject(new Error("Build failed: no stats available"));
|
|
4446
4410
|
return;
|
|
4447
4411
|
}
|
|
4448
|
-
|
|
4412
|
+
resolve5(stats2);
|
|
4449
4413
|
});
|
|
4450
4414
|
});
|
|
4451
|
-
await new Promise((
|
|
4415
|
+
await new Promise((resolve5, reject) => {
|
|
4452
4416
|
compiler.close((err) => {
|
|
4453
4417
|
if (err) reject(err);
|
|
4454
|
-
else
|
|
4418
|
+
else resolve5();
|
|
4455
4419
|
});
|
|
4456
4420
|
});
|
|
4457
4421
|
const hasErrors = stats.hasErrors();
|
|
@@ -4482,17 +4446,17 @@ import chalk3 from "chalk";
|
|
|
4482
4446
|
// src/cli/port.ts
|
|
4483
4447
|
import net from "net";
|
|
4484
4448
|
function isPortAvailable(port, host) {
|
|
4485
|
-
return new Promise((
|
|
4449
|
+
return new Promise((resolve5) => {
|
|
4486
4450
|
const server = net.createServer();
|
|
4487
4451
|
server.once("error", (err) => {
|
|
4488
4452
|
if (err.code === "EADDRINUSE") {
|
|
4489
|
-
|
|
4453
|
+
resolve5(false);
|
|
4490
4454
|
} else {
|
|
4491
|
-
|
|
4455
|
+
resolve5(false);
|
|
4492
4456
|
}
|
|
4493
4457
|
});
|
|
4494
4458
|
server.once("listening", () => {
|
|
4495
|
-
server.close(() =>
|
|
4459
|
+
server.close(() => resolve5(true));
|
|
4496
4460
|
});
|
|
4497
4461
|
server.listen(port, host);
|
|
4498
4462
|
});
|
|
@@ -4509,88 +4473,212 @@ async function getAvailablePort(preferredPort, host) {
|
|
|
4509
4473
|
);
|
|
4510
4474
|
}
|
|
4511
4475
|
|
|
4476
|
+
// src/cli/restart-watcher.ts
|
|
4477
|
+
import { existsSync as existsSync13, watch as watch2 } from "fs";
|
|
4478
|
+
import { resolve as resolve4 } from "path";
|
|
4479
|
+
function isTriggerFile(filename, mode) {
|
|
4480
|
+
if (!filename) return false;
|
|
4481
|
+
if (CONFIG_FILES.includes(filename)) return true;
|
|
4482
|
+
if (filename === ".env" || filename === ".env.public" || filename === `.env.${mode}`) {
|
|
4483
|
+
return true;
|
|
4484
|
+
}
|
|
4485
|
+
return false;
|
|
4486
|
+
}
|
|
4487
|
+
function normalizeFilename(filename) {
|
|
4488
|
+
if (typeof filename === "string") return filename;
|
|
4489
|
+
if (Buffer.isBuffer(filename)) return filename.toString("utf8");
|
|
4490
|
+
return null;
|
|
4491
|
+
}
|
|
4492
|
+
function watchForRestart(opts) {
|
|
4493
|
+
const { cwd, mode, onChange, debounceMs = 300 } = opts;
|
|
4494
|
+
const watchers = [];
|
|
4495
|
+
let timer = null;
|
|
4496
|
+
const trigger = (filename) => {
|
|
4497
|
+
const name = normalizeFilename(filename);
|
|
4498
|
+
if (!isTriggerFile(name, mode)) return;
|
|
4499
|
+
if (timer) clearTimeout(timer);
|
|
4500
|
+
timer = setTimeout(() => {
|
|
4501
|
+
timer = null;
|
|
4502
|
+
Promise.resolve(onChange()).catch((err) => {
|
|
4503
|
+
console.error("[ywkf] \u81EA\u52A8\u91CD\u542F\u56DE\u8C03\u51FA\u9519:", err);
|
|
4504
|
+
});
|
|
4505
|
+
}, debounceMs);
|
|
4506
|
+
};
|
|
4507
|
+
watchers.push(watch2(cwd, { recursive: false }, (_event, filename) => trigger(filename)));
|
|
4508
|
+
const envDir = resolve4(cwd, "config/env");
|
|
4509
|
+
if (existsSync13(envDir)) {
|
|
4510
|
+
watchers.push(watch2(envDir, { recursive: false }, (_event, filename) => trigger(filename)));
|
|
4511
|
+
}
|
|
4512
|
+
return {
|
|
4513
|
+
close: () => {
|
|
4514
|
+
if (timer) clearTimeout(timer);
|
|
4515
|
+
for (const w of watchers) w.close();
|
|
4516
|
+
}
|
|
4517
|
+
};
|
|
4518
|
+
}
|
|
4519
|
+
|
|
4512
4520
|
// src/cli/dev.ts
|
|
4513
|
-
|
|
4514
|
-
const
|
|
4515
|
-
|
|
4516
|
-
|
|
4517
|
-
preloadEnv(cwd, mode, "development");
|
|
4518
|
-
const { config } = await resolveConfig(cwd);
|
|
4519
|
-
loadEnv(config, cwd, mode, "development");
|
|
4520
|
-
const pluginManager = new PluginManager(config, cwd, true);
|
|
4521
|
-
if (config.plugins && config.plugins.length > 0) {
|
|
4522
|
-
await pluginManager.loadPlugins(config.plugins);
|
|
4521
|
+
function restoreEnv(snapshot) {
|
|
4522
|
+
for (const key of Object.keys(process.env)) {
|
|
4523
|
+
if (!(key in snapshot)) {
|
|
4524
|
+
delete process.env[key];
|
|
4523
4525
|
}
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4526
|
+
}
|
|
4527
|
+
for (const key of Object.keys(snapshot)) {
|
|
4528
|
+
process.env[key] = snapshot[key];
|
|
4529
|
+
}
|
|
4530
|
+
}
|
|
4531
|
+
async function loadFullConfig(cwd, mode, envSnapshot, opts = {}) {
|
|
4532
|
+
restoreEnv(envSnapshot);
|
|
4533
|
+
preloadEnv(cwd, mode, "development");
|
|
4534
|
+
const { config } = await resolveConfig(cwd, { fresh: opts.fresh });
|
|
4535
|
+
loadEnv(config, cwd, mode, "development");
|
|
4536
|
+
return config;
|
|
4537
|
+
}
|
|
4538
|
+
async function startDevServer(cwd, config) {
|
|
4539
|
+
const pluginManager = new PluginManager(config, cwd, true);
|
|
4540
|
+
if (config.plugins && config.plugins.length > 0) {
|
|
4541
|
+
await pluginManager.loadPlugins(config.plugins);
|
|
4542
|
+
}
|
|
4543
|
+
await pluginManager.runBeforeDevServer();
|
|
4544
|
+
let rspackConfig = createRspackConfig(config, cwd, { isDev: true });
|
|
4545
|
+
rspackConfig = await pluginManager.applyRspackConfigHooks(rspackConfig);
|
|
4546
|
+
const host = config.dev.host || "127.0.0.1";
|
|
4547
|
+
const preferredPort = config.dev.port || 3e3;
|
|
4548
|
+
const port = await getAvailablePort(preferredPort, host);
|
|
4549
|
+
const pluginNames = pluginManager.getPluginNames();
|
|
4550
|
+
if (port !== preferredPort) {
|
|
4551
|
+
console.log(chalk3.yellow(` Port ${preferredPort} is in use, using ${port} instead.
|
|
4533
4552
|
`));
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
}
|
|
4563
|
-
console.log();
|
|
4553
|
+
}
|
|
4554
|
+
const printer = new DevPrinter(host, port, pluginNames);
|
|
4555
|
+
printer.printBanner();
|
|
4556
|
+
printer.printBuildStart();
|
|
4557
|
+
printer.updateProgress(0, "preparing");
|
|
4558
|
+
rspackConfig.plugins = rspackConfig.plugins || [];
|
|
4559
|
+
rspackConfig.plugins.push(new rspack5.ProgressPlugin(createProgressHandler(printer)));
|
|
4560
|
+
rspackConfig.stats = "none";
|
|
4561
|
+
rspackConfig.infrastructureLogging = { level: "none" };
|
|
4562
|
+
if (rspackConfig.devServer) {
|
|
4563
|
+
const ds = rspackConfig.devServer;
|
|
4564
|
+
const existingClient = ds.client ?? {};
|
|
4565
|
+
ds.client = {
|
|
4566
|
+
...existingClient,
|
|
4567
|
+
logging: "warn",
|
|
4568
|
+
overlay: false,
|
|
4569
|
+
progress: false
|
|
4570
|
+
};
|
|
4571
|
+
}
|
|
4572
|
+
const compiler = rspack5(rspackConfig);
|
|
4573
|
+
compiler.hooks.done.tap("ywkf-dev-printer", (stats) => {
|
|
4574
|
+
const hasErrors = stats.hasErrors();
|
|
4575
|
+
if (hasErrors) {
|
|
4576
|
+
const info = stats.toJson({ errors: true });
|
|
4577
|
+
console.log();
|
|
4578
|
+
console.log(chalk3.red(" Compile error:"));
|
|
4579
|
+
for (const err of info.errors || []) {
|
|
4580
|
+
console.log(chalk3.red(` ${err.message}`));
|
|
4564
4581
|
}
|
|
4565
|
-
|
|
4566
|
-
}
|
|
4567
|
-
|
|
4568
|
-
|
|
4569
|
-
|
|
4582
|
+
console.log();
|
|
4583
|
+
}
|
|
4584
|
+
printer.printBuildDone(hasErrors);
|
|
4585
|
+
});
|
|
4586
|
+
compiler.hooks.invalid.tap("ywkf-dev-printer", () => {
|
|
4587
|
+
printer.markRebuildStart();
|
|
4588
|
+
printer.updateProgress(0, "rebuilding");
|
|
4589
|
+
});
|
|
4590
|
+
const devServerOptions = {
|
|
4591
|
+
...rspackConfig.devServer || {},
|
|
4592
|
+
host,
|
|
4593
|
+
port,
|
|
4594
|
+
setupExitSignals: false
|
|
4595
|
+
};
|
|
4596
|
+
const server = new RspackDevServer(devServerOptions, compiler);
|
|
4597
|
+
await server.start();
|
|
4598
|
+
await pluginManager.runAfterDevServer({ host, port });
|
|
4599
|
+
let closed = false;
|
|
4600
|
+
const close = async () => {
|
|
4601
|
+
if (closed) return;
|
|
4602
|
+
closed = true;
|
|
4603
|
+
await server.stop();
|
|
4604
|
+
await new Promise((resolve5, reject) => {
|
|
4605
|
+
compiler.close((err) => {
|
|
4606
|
+
if (err) reject(err);
|
|
4607
|
+
else resolve5();
|
|
4608
|
+
});
|
|
4570
4609
|
});
|
|
4571
|
-
|
|
4572
|
-
|
|
4573
|
-
|
|
4574
|
-
|
|
4610
|
+
};
|
|
4611
|
+
return { printer, host, port, close };
|
|
4612
|
+
}
|
|
4613
|
+
async function dev(options = {}) {
|
|
4614
|
+
const cwd = options.cwd || process.cwd();
|
|
4615
|
+
const mode = options.mode || "development";
|
|
4616
|
+
const envSnapshot = { ...process.env };
|
|
4617
|
+
try {
|
|
4618
|
+
const config = await loadFullConfig(cwd, mode, envSnapshot);
|
|
4619
|
+
let current = await startDevServer(cwd, config);
|
|
4620
|
+
let shuttingDown = false;
|
|
4621
|
+
const shutdown = async () => {
|
|
4622
|
+
if (shuttingDown) return;
|
|
4623
|
+
shuttingDown = true;
|
|
4624
|
+
await current.close();
|
|
4625
|
+
process.exit(0);
|
|
4575
4626
|
};
|
|
4576
|
-
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
registerShortcuts(printer, {
|
|
4580
|
-
port,
|
|
4627
|
+
registerShortcuts(() => current.printer, {
|
|
4628
|
+
getPort: () => current.port,
|
|
4629
|
+
getHost: () => current.host,
|
|
4581
4630
|
onQuit: async () => {
|
|
4582
4631
|
console.log(chalk3.gray("\n Shutting down...\n"));
|
|
4583
|
-
await
|
|
4584
|
-
process.exit(0);
|
|
4632
|
+
await shutdown();
|
|
4585
4633
|
}
|
|
4586
4634
|
});
|
|
4587
4635
|
const signals = ["SIGINT", "SIGTERM"];
|
|
4588
4636
|
for (const signal of signals) {
|
|
4589
|
-
process.on(signal,
|
|
4590
|
-
|
|
4591
|
-
process.exit(0);
|
|
4637
|
+
process.on(signal, () => {
|
|
4638
|
+
void shutdown();
|
|
4592
4639
|
});
|
|
4593
4640
|
}
|
|
4641
|
+
let isRestarting = false;
|
|
4642
|
+
let pending = false;
|
|
4643
|
+
watchForRestart({
|
|
4644
|
+
cwd,
|
|
4645
|
+
mode,
|
|
4646
|
+
onChange: async () => {
|
|
4647
|
+
if (isRestarting) {
|
|
4648
|
+
pending = true;
|
|
4649
|
+
return;
|
|
4650
|
+
}
|
|
4651
|
+
isRestarting = true;
|
|
4652
|
+
try {
|
|
4653
|
+
do {
|
|
4654
|
+
pending = false;
|
|
4655
|
+
console.log(chalk3.cyan("\n \u27F3 \u68C0\u6D4B\u5230\u914D\u7F6E\u53D8\u66F4\uFF0C\u6B63\u5728\u91CD\u542F dev server...\n"));
|
|
4656
|
+
let nextConfig;
|
|
4657
|
+
try {
|
|
4658
|
+
nextConfig = await loadFullConfig(cwd, mode, envSnapshot, { fresh: true });
|
|
4659
|
+
} catch (error) {
|
|
4660
|
+
console.error(
|
|
4661
|
+
chalk3.red(" \u2716 \u914D\u7F6E\u52A0\u8F7D\u5931\u8D25\uFF0C\u4FDD\u6301\u5F53\u524D dev server \u8FD0\u884C\uFF08\u4FEE\u6B63\u540E\u4FDD\u5B58\u53EF\u91CD\u8BD5\uFF09:")
|
|
4662
|
+
);
|
|
4663
|
+
console.error(error);
|
|
4664
|
+
restoreEnv(envSnapshot);
|
|
4665
|
+
continue;
|
|
4666
|
+
}
|
|
4667
|
+
try {
|
|
4668
|
+
await current.close();
|
|
4669
|
+
current = await startDevServer(cwd, nextConfig);
|
|
4670
|
+
} catch (error) {
|
|
4671
|
+
console.error(
|
|
4672
|
+
chalk3.red(" \u2716 dev server \u91CD\u542F\u5931\u8D25\uFF0C\u5F53\u524D\u670D\u52A1\u4E0D\u53EF\u7528\uFF0C\u8BF7\u4FEE\u6B63\u540E\u4FDD\u5B58\u91CD\u8BD5:")
|
|
4673
|
+
);
|
|
4674
|
+
console.error(error);
|
|
4675
|
+
}
|
|
4676
|
+
} while (pending);
|
|
4677
|
+
} finally {
|
|
4678
|
+
isRestarting = false;
|
|
4679
|
+
}
|
|
4680
|
+
}
|
|
4681
|
+
});
|
|
4594
4682
|
} catch (error) {
|
|
4595
4683
|
console.error();
|
|
4596
4684
|
console.error(chalk3.red(" \u2716 Dev server failed to start"));
|