外观
构建
执行:
全量构建:
bash
npm run build1
支持 TASKS 参数
该命令支持通过 TASKS 来指定具体执行哪些任务(不传的话默认执行全部任务)。
该参数支持白名单和黑名单模式,并且支持 * 通配符。注意,白名单和黑名单模式不可混用,以避免歧义。
bash
# 白名单:执行所有 spa: 开头的任务
cross-env TASKS=spa:* npm run <example-command>
# 白名单:混合精确匹配和通配符
cross-env TASKS=cleanAllBuildFiles,ssg:* npm run <example-command>
# 黑名单:排除所有 spa: 开头的任务
cross-env TASKS=-spa:* npm run <example-command>1
2
3
4
5
6
2
3
4
5
6
scripts/build.mts 源码
typescript
import { isPathExists, joinPath } from "nsuite";
import { cleanAllBuildFiles } from "#scripts/utils/clean-files";
import { buildPublic } from "#scripts/utils/public-builder";
import { runCommandInChildProcess } from "#scripts/utils/child-process";
import { SUB_APPS_NAMES, STANDALONE_APPS_NAMES, PATH_APPS_HOME } from "#utils/ConstantUtils";
import { useTask } from "#scripts/utils/use-task";
import * as zod from "zod";
const { killAllChildProcessWhenSuitable, addTask, runTasksSequentially, printTaskStatisticsInfo } =
useTask({ allowTasks: process.env.TASKS || "" });
killAllChildProcessWhenSuitable();
const env = { ...process.env };
zod
.object({
NODE_ENV: zod.string().min(1, "NODE_ENV is required"),
MODE: zod.string().min(1, "MODE is required"),
})
.parse(env);
/**
* 编译开始前先清空所有可能遗留的编译产物
*/
addTask("cleanAllBuildFiles", async () => {
await cleanAllBuildFiles();
});
/**
* 构建 public 目录
*/
addTask("public:build", async () => {
await buildPublic({ enableWatch: false });
});
/**
* 顺序构建所有 SPA APP
*/
for (const appName of SUB_APPS_NAMES) {
addTask(`spa:${appName}`, async (payload) => {
const { progress, taskName } = payload;
const { promise } = runCommandInChildProcess({
appName: `${progress.current}/${progress.total}-SPA-${appName}`,
command: "npx",
args: `cross-env APP_NAME=${appName} vite build ./apps/${appName}`,
env,
});
const exitCode = await promise;
if (exitCode) {
throw new Error(`Task ${taskName} failed: exitCode=${exitCode}`);
}
});
}
/**
* 顺序构建所有 SSG APP
*/
for (const appName of STANDALONE_APPS_NAMES) {
addTask(`ssg:${appName}`, async (payload) => {
const { progress, taskName } = payload;
// 区分应用类型:Nuxt 4 应用、VitePress 2 应用
const pathApp = joinPath(PATH_APPS_HOME, appName);
const pathNuxtConfig = joinPath(pathApp, "nuxt.config.ts");
const isNuxt = await isPathExists(pathNuxtConfig);
const args = isNuxt
? `cross-env APP_NAME=${appName} nuxt generate --cwd=./apps-home/${appName}`
: `vitepress build ./apps-home/${appName}/config`;
const { promise } = runCommandInChildProcess({
appName: `${progress.current}/${progress.total}-${taskName}`,
command: "npx",
args,
env,
});
const exitCode = await promise;
if (exitCode) {
throw new Error(`Task ${taskName} failed: exitCode=${exitCode}, commands=npx ${args}`);
}
});
}
await runTasksSequentially();
printTaskStatisticsInfo();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84