@fchc8/vite-plugin-multi-page 1.1.1 → 1.3.0
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/README-EN.md +183 -337
- package/README.md +191 -278
- package/dist/cli.js +649 -0
- package/dist/index.d.mts +43 -69
- package/dist/index.d.ts +43 -69
- package/dist/index.js +653 -428
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +649 -429
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -3
package/dist/cli.js
ADDED
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __esm = (fn, res) => function __init() {
|
|
10
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
+
|
|
34
|
+
// node_modules/.pnpm/tsup@8.0.0_typescript@5.0.2/node_modules/tsup/assets/cjs_shims.js
|
|
35
|
+
var init_cjs_shims = __esm({
|
|
36
|
+
"node_modules/.pnpm/tsup@8.0.0_typescript@5.0.2/node_modules/tsup/assets/cjs_shims.js"() {
|
|
37
|
+
"use strict";
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// src/config-loader.ts
|
|
42
|
+
var config_loader_exports = {};
|
|
43
|
+
__export(config_loader_exports, {
|
|
44
|
+
hasCustomConfig: () => hasCustomConfig,
|
|
45
|
+
loadUserConfig: () => loadUserConfig
|
|
46
|
+
});
|
|
47
|
+
function hasCustomConfig() {
|
|
48
|
+
for (const filename of CONFIG_FILES) {
|
|
49
|
+
const configPath = path3.resolve(process.cwd(), filename);
|
|
50
|
+
if (fs2.existsSync(configPath)) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
async function loadUserConfig(context) {
|
|
57
|
+
const customConfig = await loadCustomConfig();
|
|
58
|
+
if (customConfig) {
|
|
59
|
+
return customConfig(context);
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
async function loadConfigFile(filePath) {
|
|
64
|
+
if (filePath.endsWith(".ts")) {
|
|
65
|
+
try {
|
|
66
|
+
const code = await fs2.promises.readFile(filePath, "utf-8");
|
|
67
|
+
const esbuild = await import("esbuild");
|
|
68
|
+
const result = await esbuild.transform(code, {
|
|
69
|
+
loader: "ts",
|
|
70
|
+
format: "cjs",
|
|
71
|
+
// 使用 CommonJS 格式便于使用 Module._compile
|
|
72
|
+
target: "node16",
|
|
73
|
+
sourcemap: false
|
|
74
|
+
});
|
|
75
|
+
const tempModule = new import_node_module.Module(filePath);
|
|
76
|
+
tempModule.filename = filePath;
|
|
77
|
+
tempModule.paths = import_node_module.Module._nodeModulePaths(path3.dirname(filePath));
|
|
78
|
+
tempModule._compile(result.code, filePath);
|
|
79
|
+
return tempModule.exports;
|
|
80
|
+
} catch (esbuildError) {
|
|
81
|
+
console.warn("esbuild \u8F6C\u8BD1\u5931\u8D25\uFF0C\u5C1D\u8BD5\u7B80\u5355\u8F6C\u6362:", esbuildError);
|
|
82
|
+
const code = await fs2.promises.readFile(filePath, "utf-8");
|
|
83
|
+
const jsCode = code.replace(/export\s+default\s+/, "module.exports = ").replace(/import\s+.*?from\s+['"][^'"]*['"];?\s*/g, "").replace(/:\s*[^=,})\]]+/g, "");
|
|
84
|
+
const tempModule = new import_node_module.Module(filePath);
|
|
85
|
+
tempModule.filename = filePath;
|
|
86
|
+
tempModule.paths = import_node_module.Module._nodeModulePaths(path3.dirname(filePath));
|
|
87
|
+
tempModule._compile(jsCode, filePath);
|
|
88
|
+
return tempModule.exports;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (filePath.endsWith(".js") || filePath.endsWith(".mjs")) {
|
|
92
|
+
const fileUrl = (0, import_node_url.pathToFileURL)(filePath).href;
|
|
93
|
+
return import(`${fileUrl}?t=${Date.now()}`);
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`\u4E0D\u652F\u6301\u7684\u914D\u7F6E\u6587\u4EF6\u7C7B\u578B: ${filePath}`);
|
|
96
|
+
}
|
|
97
|
+
async function loadCustomConfig() {
|
|
98
|
+
const cwd = process.cwd();
|
|
99
|
+
for (const configFile of CONFIG_FILES) {
|
|
100
|
+
const configPath = path3.resolve(cwd, configFile);
|
|
101
|
+
if (fs2.existsSync(configPath)) {
|
|
102
|
+
try {
|
|
103
|
+
const configModule = await loadConfigFile(configPath);
|
|
104
|
+
const configFunction = configModule.default || configModule;
|
|
105
|
+
if (typeof configFunction === "function") {
|
|
106
|
+
return configFunction;
|
|
107
|
+
} else {
|
|
108
|
+
console.warn(`\u914D\u7F6E\u6587\u4EF6 ${configFile} \u5FC5\u987B\u9ED8\u8BA4\u5BFC\u51FA\u4E00\u4E2A\u51FD\u6570`);
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (configFile.endsWith(".ts")) {
|
|
112
|
+
console.error(`\u52A0\u8F7DTypeScript\u914D\u7F6E\u6587\u4EF6 ${configFile} \u5931\u8D25:`, error);
|
|
113
|
+
console.log("\u63D0\u793A\uFF1A\u786E\u4FDD\u4F60\u7684\u9879\u76EE\u652F\u6301TypeScript\uFF0C\u6216\u8005\u4F7F\u7528 .js/.mjs \u914D\u7F6E\u6587\u4EF6");
|
|
114
|
+
} else {
|
|
115
|
+
console.error(`\u52A0\u8F7D\u914D\u7F6E\u6587\u4EF6 ${configFile} \u5931\u8D25:`, error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
var fs2, path3, import_node_url, import_node_module, CONFIG_FILES;
|
|
123
|
+
var init_config_loader = __esm({
|
|
124
|
+
"src/config-loader.ts"() {
|
|
125
|
+
"use strict";
|
|
126
|
+
init_cjs_shims();
|
|
127
|
+
fs2 = __toESM(require("fs"));
|
|
128
|
+
path3 = __toESM(require("path"));
|
|
129
|
+
import_node_url = require("url");
|
|
130
|
+
import_node_module = require("module");
|
|
131
|
+
CONFIG_FILES = [
|
|
132
|
+
"multipage.config.js",
|
|
133
|
+
"multipage.config.mjs",
|
|
134
|
+
"multipage.config.ts"
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// src/cli.ts
|
|
140
|
+
var cli_exports = {};
|
|
141
|
+
__export(cli_exports, {
|
|
142
|
+
buildAll: () => main
|
|
143
|
+
});
|
|
144
|
+
module.exports = __toCommonJS(cli_exports);
|
|
145
|
+
init_cjs_shims();
|
|
146
|
+
var import_node_child_process = require("child_process");
|
|
147
|
+
var fs3 = __toESM(require("fs"));
|
|
148
|
+
var path4 = __toESM(require("path"));
|
|
149
|
+
var glob2 = __toESM(require("glob"));
|
|
150
|
+
|
|
151
|
+
// src/build-config.ts
|
|
152
|
+
init_cjs_shims();
|
|
153
|
+
var import_vite = require("vite");
|
|
154
|
+
var import_glob = require("glob");
|
|
155
|
+
var path2 = __toESM(require("path"));
|
|
156
|
+
var fs = __toESM(require("fs"));
|
|
157
|
+
|
|
158
|
+
// src/file-filter.ts
|
|
159
|
+
init_cjs_shims();
|
|
160
|
+
var path = __toESM(require("path"));
|
|
161
|
+
function filterEntryFiles(files, entry, exclude, log) {
|
|
162
|
+
const result = [];
|
|
163
|
+
const nameToFile = /* @__PURE__ */ new Map();
|
|
164
|
+
let basePattern = entry.replace(/\/\*.*$/, "");
|
|
165
|
+
if (!basePattern || basePattern === entry) {
|
|
166
|
+
basePattern = path.dirname(entry.split("*")[0]);
|
|
167
|
+
}
|
|
168
|
+
log("\u57FA\u7840\u76EE\u5F55\u6A21\u5F0F:", basePattern);
|
|
169
|
+
const candidateFiles = [];
|
|
170
|
+
for (const file of files) {
|
|
171
|
+
if (exclude.includes(file)) {
|
|
172
|
+
log(`\u8DF3\u8FC7\u6392\u9664\u6587\u4EF6: ${file}`);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const relativePath = path.relative(basePattern, file);
|
|
176
|
+
const pathParts = relativePath.split(path.sep);
|
|
177
|
+
log(`\u5904\u7406\u6587\u4EF6: ${file}`);
|
|
178
|
+
log(`\u76F8\u5BF9\u8DEF\u5F84: ${relativePath}`);
|
|
179
|
+
log(`\u8DEF\u5F84\u90E8\u5206: ${pathParts}`);
|
|
180
|
+
if (pathParts.length === 1) {
|
|
181
|
+
const fileName = pathParts[0];
|
|
182
|
+
const name = path.basename(fileName, path.extname(fileName));
|
|
183
|
+
candidateFiles.push({ name, file, priority: 1 });
|
|
184
|
+
log(`\u{1F4C4} \u7B2C\u4E00\u7EA7\u6587\u4EF6: ${file} -> ${name}.html (\u4F18\u5148\u7EA7: 1)`);
|
|
185
|
+
} else if (pathParts.length >= 2) {
|
|
186
|
+
const fileName = path.basename(file, path.extname(file));
|
|
187
|
+
const dirName = pathParts[0];
|
|
188
|
+
if (fileName === "main") {
|
|
189
|
+
candidateFiles.push({ name: dirName, file, priority: 2 });
|
|
190
|
+
log(`\u{1F4C1} \u76EE\u5F55main\u6587\u4EF6: ${file} -> ${dirName}.html (\u4F18\u5148\u7EA7: 2)`);
|
|
191
|
+
} else {
|
|
192
|
+
log(`\u274C \u8DF3\u8FC7\u6587\u4EF6: ${file} (\u4E0D\u662Fmain\u6587\u4EF6)`);
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
log(`\u274C \u8DF3\u8FC7\u6587\u4EF6: ${file} (\u8DEF\u5F84\u5C42\u7EA7\u4E0D\u7B26\u5408)`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
for (const candidate of candidateFiles) {
|
|
199
|
+
const existing = nameToFile.get(candidate.name);
|
|
200
|
+
if (!existing) {
|
|
201
|
+
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
202
|
+
log(`\u2705 \u6DFB\u52A0\u9875\u9762: ${candidate.name} -> ${candidate.file}`);
|
|
203
|
+
} else {
|
|
204
|
+
if (candidate.priority > existing.priority) {
|
|
205
|
+
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
206
|
+
log(
|
|
207
|
+
`\u{1F504} \u66FF\u6362\u9875\u9762: ${candidate.name} -> ${candidate.file} (\u66FF\u6362 ${existing.file}, \u76EE\u5F55\u4F18\u5148)`
|
|
208
|
+
);
|
|
209
|
+
} else {
|
|
210
|
+
log(`\u26A0\uFE0F \u51B2\u7A81\u8DF3\u8FC7: ${candidate.name} -> ${candidate.file} (\u4FDD\u7559 ${existing.file})`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const [name, { file }] of nameToFile.entries()) {
|
|
215
|
+
result.push({ name, file });
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/page-config.ts
|
|
221
|
+
init_cjs_shims();
|
|
222
|
+
function getPageConfig(pageConfigs, context, log) {
|
|
223
|
+
if (!pageConfigs)
|
|
224
|
+
return null;
|
|
225
|
+
if (typeof pageConfigs === "function") {
|
|
226
|
+
const result = pageConfigs(context);
|
|
227
|
+
if (result) {
|
|
228
|
+
log(`\u51FD\u6570\u914D\u7F6E\u5339\u914D\u9875\u9762 ${context.pageName}:`, result);
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
for (const [key, config] of Object.entries(pageConfigs)) {
|
|
233
|
+
if (key === context.pageName) {
|
|
234
|
+
log(`\u7CBE\u786E\u5339\u914D\u9875\u9762 ${context.pageName}:`, config);
|
|
235
|
+
return config;
|
|
236
|
+
}
|
|
237
|
+
if (config.match) {
|
|
238
|
+
const patterns = Array.isArray(config.match) ? config.match : [config.match];
|
|
239
|
+
const isMatched = patterns.some(
|
|
240
|
+
(pattern) => simpleMatch(pattern, context.pageName) || simpleMatch(pattern, context.relativePath) || simpleMatch(pattern, context.filePath)
|
|
241
|
+
);
|
|
242
|
+
if (isMatched) {
|
|
243
|
+
log(`\u6A21\u5F0F\u5339\u914D\u9875\u9762 ${context.pageName} (\u6A21\u5F0F: ${config.match}):`, config);
|
|
244
|
+
return { ...config, match: void 0 };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (simpleMatch(key, context.pageName)) {
|
|
248
|
+
log(`Glob\u5339\u914D\u9875\u9762 ${context.pageName} (\u6A21\u5F0F: ${key}):`, config);
|
|
249
|
+
return config;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
function simpleMatch(pattern, text) {
|
|
255
|
+
const regexPattern = pattern.replace(/\*\*/g, "__DOUBLE_STAR__").replace(/\*/g, "[^/]*").replace(/__DOUBLE_STAR__/g, ".*");
|
|
256
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
257
|
+
return regex.test(text);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/utils.ts
|
|
261
|
+
init_cjs_shims();
|
|
262
|
+
function createLogger(debug) {
|
|
263
|
+
return (...args) => {
|
|
264
|
+
if (debug) {
|
|
265
|
+
console.log("[vite-plugin-multi-page]", ...args);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// src/build-config.ts
|
|
271
|
+
function getAvailableStrategies(options) {
|
|
272
|
+
const { entry = "src/pages/*/main.{ts,js}", exclude = [], pageConfigs = {} } = options;
|
|
273
|
+
const log = createLogger(false);
|
|
274
|
+
const strategySet = /* @__PURE__ */ new Set();
|
|
275
|
+
try {
|
|
276
|
+
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
277
|
+
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
278
|
+
for (const entryFile of entryFiles) {
|
|
279
|
+
const pageContext = {
|
|
280
|
+
pageName: entryFile.name,
|
|
281
|
+
filePath: entryFile.file,
|
|
282
|
+
relativePath: path2.relative(process.cwd(), entryFile.file)
|
|
283
|
+
};
|
|
284
|
+
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
285
|
+
const strategyName = (pageConfig == null ? void 0 : pageConfig.strategy) || "default";
|
|
286
|
+
strategySet.add(strategyName);
|
|
287
|
+
}
|
|
288
|
+
return Array.from(strategySet).sort();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
log("\u83B7\u53D6\u53EF\u7528\u7B56\u7565\u5931\u8D25:", error);
|
|
291
|
+
return ["default"];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/cli.ts
|
|
296
|
+
function parseArgs() {
|
|
297
|
+
const args = process.argv.slice(2);
|
|
298
|
+
const viteBuildArgs = [];
|
|
299
|
+
let debug = false;
|
|
300
|
+
for (let i = 0; i < args.length; i++) {
|
|
301
|
+
const arg = args[i];
|
|
302
|
+
if (arg === "--debug") {
|
|
303
|
+
debug = true;
|
|
304
|
+
} else if (arg === "--help" || arg === "-h") {
|
|
305
|
+
console.log(`
|
|
306
|
+
\u4F7F\u7528\u65B9\u6CD5: vite-multi-page-build [\u9009\u9879]
|
|
307
|
+
|
|
308
|
+
\u9009\u9879:
|
|
309
|
+
--debug \u542F\u7528\u8C03\u8BD5\u6A21\u5F0F
|
|
310
|
+
--help, -h \u663E\u793A\u5E2E\u52A9\u4FE1\u606F
|
|
311
|
+
|
|
312
|
+
\u5176\u4ED6\u6240\u6709\u53C2\u6570\u5C06\u4F20\u9012\u7ED9 vite build \u547D\u4EE4
|
|
313
|
+
|
|
314
|
+
\u793A\u4F8B:
|
|
315
|
+
vite-multi-page-build
|
|
316
|
+
vite-multi-page-build --debug
|
|
317
|
+
vite-multi-page-build --host --port 3000
|
|
318
|
+
vite-multi-page-build --mode production --debug
|
|
319
|
+
`);
|
|
320
|
+
process.exit(0);
|
|
321
|
+
} else {
|
|
322
|
+
viteBuildArgs.push(arg);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return { viteBuildArgs, debug };
|
|
326
|
+
}
|
|
327
|
+
async function loadViteConfig() {
|
|
328
|
+
const { loadUserConfig: loadUserConfig2, hasCustomConfig: hasCustomConfig2 } = await Promise.resolve().then(() => (init_config_loader(), config_loader_exports));
|
|
329
|
+
if (!hasCustomConfig2()) {
|
|
330
|
+
console.error("\u274C \u672A\u627E\u5230\u591A\u9875\u9762\u914D\u7F6E\u6587\u4EF6\uFF01");
|
|
331
|
+
console.log("\u8BF7\u521B\u5EFA\u4EE5\u4E0B\u914D\u7F6E\u6587\u4EF6\u4E4B\u4E00\uFF1A");
|
|
332
|
+
console.log(" - multipage.config.ts");
|
|
333
|
+
console.log(" - multipage.config.js");
|
|
334
|
+
console.log(" - multipage.config.mjs");
|
|
335
|
+
console.log("");
|
|
336
|
+
console.log("\u914D\u7F6E\u6587\u4EF6\u793A\u4F8B\uFF1A");
|
|
337
|
+
console.log("");
|
|
338
|
+
console.log("```typescript");
|
|
339
|
+
console.log("export default (context) => {");
|
|
340
|
+
console.log(" const { mode, command, isCLI } = context;");
|
|
341
|
+
console.log(' const isProduction = mode === "production";');
|
|
342
|
+
console.log(" ");
|
|
343
|
+
console.log(" return {");
|
|
344
|
+
console.log(' entry: "src/pages/**/*.{ts,js}",');
|
|
345
|
+
console.log(' template: "index.html",');
|
|
346
|
+
console.log(' placeholder: "{{ENTRY_FILE}}",');
|
|
347
|
+
console.log(" strategies: {");
|
|
348
|
+
console.log(" default: {");
|
|
349
|
+
console.log(" define: { IS_DEFAULT: true },");
|
|
350
|
+
console.log(" build: { sourcemap: !isProduction },");
|
|
351
|
+
console.log(" },");
|
|
352
|
+
console.log(" },");
|
|
353
|
+
console.log(" pageConfigs: (pageContext) => ({");
|
|
354
|
+
console.log(' strategy: "default",');
|
|
355
|
+
console.log(" }),");
|
|
356
|
+
console.log(" };");
|
|
357
|
+
console.log("};");
|
|
358
|
+
console.log("```");
|
|
359
|
+
process.exit(1);
|
|
360
|
+
}
|
|
361
|
+
const userConfig = await loadUserConfig2({
|
|
362
|
+
mode: "production",
|
|
363
|
+
command: "build",
|
|
364
|
+
isCLI: true
|
|
365
|
+
});
|
|
366
|
+
if (!userConfig) {
|
|
367
|
+
console.error("\u274C \u914D\u7F6E\u6587\u4EF6\u52A0\u8F7D\u5931\u8D25\uFF01");
|
|
368
|
+
process.exit(1);
|
|
369
|
+
}
|
|
370
|
+
return userConfig;
|
|
371
|
+
}
|
|
372
|
+
function buildStrategy(strategy, viteBuildArgs, debug) {
|
|
373
|
+
return new Promise((resolve4) => {
|
|
374
|
+
var _a;
|
|
375
|
+
const log = debug ? console.log.bind(console, `[${strategy}]`) : () => {
|
|
376
|
+
};
|
|
377
|
+
log(`\u5F00\u59CB\u6784\u5EFA\u7B56\u7565: ${strategy}`);
|
|
378
|
+
const env = {
|
|
379
|
+
...process.env,
|
|
380
|
+
VITE_BUILD_STRATEGY: strategy
|
|
381
|
+
};
|
|
382
|
+
const args = ["build", ...viteBuildArgs];
|
|
383
|
+
log(`\u6267\u884C\u547D\u4EE4: npx vite ${args.join(" ")}`);
|
|
384
|
+
const child = (0, import_node_child_process.spawn)("npx", ["vite", ...args], {
|
|
385
|
+
stdio: debug ? "inherit" : "pipe",
|
|
386
|
+
env,
|
|
387
|
+
cwd: process.cwd()
|
|
388
|
+
});
|
|
389
|
+
let errorOutput = "";
|
|
390
|
+
if (!debug) {
|
|
391
|
+
(_a = child.stderr) == null ? void 0 : _a.on("data", (data) => {
|
|
392
|
+
errorOutput += data.toString();
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
child.on("close", (code) => {
|
|
396
|
+
const success = code === 0;
|
|
397
|
+
const outputDir = `dist/${strategy}`;
|
|
398
|
+
if (success) {
|
|
399
|
+
log(`\u2705 \u7B56\u7565 ${strategy} \u6784\u5EFA\u6210\u529F`);
|
|
400
|
+
try {
|
|
401
|
+
const outputPath = path4.resolve(process.cwd(), outputDir);
|
|
402
|
+
if (fs3.existsSync(outputPath)) {
|
|
403
|
+
const files = fs3.readdirSync(outputPath);
|
|
404
|
+
for (const file of files) {
|
|
405
|
+
if (file.startsWith(".temp.mp.") && file.endsWith(".html")) {
|
|
406
|
+
const oldPath = path4.resolve(outputPath, file);
|
|
407
|
+
const name = file.replace(/^\.temp\.mp\./, "").replace(/\.html$/, "");
|
|
408
|
+
const newName = `${name}.html`;
|
|
409
|
+
const newPath = path4.resolve(outputPath, newName);
|
|
410
|
+
fs3.renameSync(oldPath, newPath);
|
|
411
|
+
log(`\u91CD\u547D\u540DHTML: ${file} -> ${newName}`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
} catch (error) {
|
|
416
|
+
log(`\u91CD\u547D\u540DHTML\u6587\u4EF6\u5931\u8D25:`, error);
|
|
417
|
+
}
|
|
418
|
+
} else {
|
|
419
|
+
log(`\u274C \u7B56\u7565 ${strategy} \u6784\u5EFA\u5931\u8D25 (\u9000\u51FA\u7801: ${code})`);
|
|
420
|
+
if (!debug && errorOutput) {
|
|
421
|
+
console.error(`\u7B56\u7565 ${strategy} \u9519\u8BEF\u8F93\u51FA:`, errorOutput);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
resolve4({
|
|
425
|
+
strategy,
|
|
426
|
+
success,
|
|
427
|
+
error: success ? void 0 : errorOutput || `\u6784\u5EFA\u5931\u8D25\uFF0C\u9000\u51FA\u7801: ${code}`,
|
|
428
|
+
outputDir
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
child.on("error", (error) => {
|
|
432
|
+
log(`\u274C \u7B56\u7565 ${strategy} \u6784\u5EFA\u51FA\u9519:`, error.message);
|
|
433
|
+
resolve4({
|
|
434
|
+
strategy,
|
|
435
|
+
success: false,
|
|
436
|
+
error: error.message,
|
|
437
|
+
outputDir: `dist/${strategy}`
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
async function mergeResults(results, debug) {
|
|
443
|
+
const log = debug ? console.log.bind(console, "[merge]") : () => {
|
|
444
|
+
};
|
|
445
|
+
log("\u5F00\u59CB\u5408\u5E76\u6784\u5EFA\u7ED3\u679C...");
|
|
446
|
+
const distDir = path4.resolve(process.cwd(), "dist");
|
|
447
|
+
const assetsDir = path4.resolve(distDir, "assets");
|
|
448
|
+
if (!fs3.existsSync(assetsDir)) {
|
|
449
|
+
fs3.mkdirSync(assetsDir, { recursive: true });
|
|
450
|
+
}
|
|
451
|
+
const htmlFiles = [];
|
|
452
|
+
const strategyInfo = [];
|
|
453
|
+
for (const result of results) {
|
|
454
|
+
strategyInfo.push({
|
|
455
|
+
strategy: result.strategy,
|
|
456
|
+
success: result.success,
|
|
457
|
+
error: result.error
|
|
458
|
+
});
|
|
459
|
+
if (!result.success)
|
|
460
|
+
continue;
|
|
461
|
+
const sourceDir = path4.resolve(distDir, result.strategy);
|
|
462
|
+
if (!fs3.existsSync(sourceDir)) {
|
|
463
|
+
log(`\u8B66\u544A: \u7B56\u7565\u76EE\u5F55\u4E0D\u5B58\u5728: ${sourceDir}`);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
log(`\u5904\u7406\u7B56\u7565: ${result.strategy}`);
|
|
467
|
+
const entries = fs3.readdirSync(sourceDir, { withFileTypes: true });
|
|
468
|
+
for (const entry of entries) {
|
|
469
|
+
const sourcePath = path4.resolve(sourceDir, entry.name);
|
|
470
|
+
if (entry.isFile()) {
|
|
471
|
+
if (entry.name.endsWith(".html")) {
|
|
472
|
+
const targetPath = path4.resolve(distDir, entry.name);
|
|
473
|
+
fs3.copyFileSync(sourcePath, targetPath);
|
|
474
|
+
htmlFiles.push(entry.name);
|
|
475
|
+
log(`\u590D\u5236HTML: ${entry.name} -> dist/${entry.name}`);
|
|
476
|
+
} else {
|
|
477
|
+
const targetPath = path4.resolve(distDir, entry.name);
|
|
478
|
+
fs3.copyFileSync(sourcePath, targetPath);
|
|
479
|
+
log(`\u590D\u5236\u6587\u4EF6: ${entry.name} -> dist/${entry.name}`);
|
|
480
|
+
}
|
|
481
|
+
} else if (entry.isDirectory() && entry.name === "assets") {
|
|
482
|
+
const sourceAssetsDir = sourcePath;
|
|
483
|
+
const assetEntries = fs3.readdirSync(sourceAssetsDir, { withFileTypes: true });
|
|
484
|
+
for (const assetEntry of assetEntries) {
|
|
485
|
+
const assetSourcePath = path4.resolve(sourceAssetsDir, assetEntry.name);
|
|
486
|
+
const assetTargetPath = path4.resolve(assetsDir, assetEntry.name);
|
|
487
|
+
if (assetEntry.isFile()) {
|
|
488
|
+
if (fs3.existsSync(assetTargetPath)) {
|
|
489
|
+
const sourceContent = fs3.readFileSync(assetSourcePath);
|
|
490
|
+
const targetContent = fs3.readFileSync(assetTargetPath);
|
|
491
|
+
if (!sourceContent.equals(targetContent)) {
|
|
492
|
+
const ext = path4.extname(assetEntry.name);
|
|
493
|
+
const baseName = path4.basename(assetEntry.name, ext);
|
|
494
|
+
const newName = `${baseName}-${result.strategy}${ext}`;
|
|
495
|
+
const newTargetPath = path4.resolve(assetsDir, newName);
|
|
496
|
+
fs3.copyFileSync(assetSourcePath, newTargetPath);
|
|
497
|
+
log(`\u590D\u5236\u8D44\u6E90(\u91CD\u547D\u540D): ${assetEntry.name} -> assets/${newName}`);
|
|
498
|
+
} else {
|
|
499
|
+
log(`\u8DF3\u8FC7\u91CD\u590D\u8D44\u6E90: ${assetEntry.name}`);
|
|
500
|
+
}
|
|
501
|
+
} else {
|
|
502
|
+
fs3.copyFileSync(assetSourcePath, assetTargetPath);
|
|
503
|
+
log(`\u590D\u5236\u8D44\u6E90: ${assetEntry.name} -> assets/${assetEntry.name}`);
|
|
504
|
+
}
|
|
505
|
+
} else if (assetEntry.isDirectory()) {
|
|
506
|
+
const subTargetDir = path4.resolve(assetsDir, assetEntry.name);
|
|
507
|
+
if (!fs3.existsSync(subTargetDir)) {
|
|
508
|
+
fs3.mkdirSync(subTargetDir, { recursive: true });
|
|
509
|
+
}
|
|
510
|
+
fs3.cpSync(assetSourcePath, subTargetDir, { recursive: true });
|
|
511
|
+
log(`\u590D\u5236\u8D44\u6E90\u76EE\u5F55: ${assetEntry.name} -> assets/${assetEntry.name}`);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
} else if (entry.isDirectory()) {
|
|
515
|
+
const targetDir = path4.resolve(distDir, entry.name);
|
|
516
|
+
fs3.cpSync(sourcePath, targetDir, { recursive: true });
|
|
517
|
+
log(`\u590D\u5236\u76EE\u5F55: ${entry.name} -> dist/${entry.name}`);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
log("\u2705 \u6784\u5EFA\u7ED3\u679C\u5408\u5E76\u5B8C\u6210");
|
|
522
|
+
log(`\u{1F4C1} HTML\u6587\u4EF6: ${htmlFiles.join(", ")}`);
|
|
523
|
+
log(`\u{1F4E6} \u8D44\u6E90\u76EE\u5F55: dist/assets/`);
|
|
524
|
+
}
|
|
525
|
+
async function cleanupTempFiles(debug) {
|
|
526
|
+
const log = debug ? console.log.bind(console, "[cleanup]") : () => {
|
|
527
|
+
};
|
|
528
|
+
log("\u6E05\u7406\u4E34\u65F6HTML\u6587\u4EF6...");
|
|
529
|
+
const tempHtmlFiles = glob2.sync(".temp.mp.*.html", { cwd: process.cwd() });
|
|
530
|
+
for (const tempFile of tempHtmlFiles) {
|
|
531
|
+
const tempPath = path4.resolve(process.cwd(), tempFile);
|
|
532
|
+
try {
|
|
533
|
+
fs3.unlinkSync(tempPath);
|
|
534
|
+
log(`\u5220\u9664\u4E34\u65F6\u6587\u4EF6: ${tempFile}`);
|
|
535
|
+
} catch (error) {
|
|
536
|
+
log(`\u5220\u9664\u4E34\u65F6\u6587\u4EF6\u5931\u8D25: ${tempFile}`, error);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (tempHtmlFiles.length === 0) {
|
|
540
|
+
log("\u6CA1\u6709\u627E\u5230\u4E34\u65F6\u6587\u4EF6");
|
|
541
|
+
} else {
|
|
542
|
+
log(`\u2705 \u6E05\u7406\u4E86 ${tempHtmlFiles.length} \u4E2A\u4E34\u65F6\u6587\u4EF6`);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
async function cleanup(strategies, debug) {
|
|
546
|
+
const log = debug ? console.log.bind(console, "[cleanup]") : () => {
|
|
547
|
+
};
|
|
548
|
+
log("\u6E05\u7406\u4E34\u65F6\u6587\u4EF6...");
|
|
549
|
+
const rootTempFiles = glob2.sync(".temp.mp.*.html", { cwd: process.cwd() });
|
|
550
|
+
for (const tempFile of rootTempFiles) {
|
|
551
|
+
const tempPath = path4.resolve(process.cwd(), tempFile);
|
|
552
|
+
try {
|
|
553
|
+
fs3.unlinkSync(tempPath);
|
|
554
|
+
log(`\u5220\u9664\u6839\u76EE\u5F55\u4E34\u65F6\u6587\u4EF6: ${tempFile}`);
|
|
555
|
+
} catch (error) {
|
|
556
|
+
log(`\u5220\u9664\u6839\u76EE\u5F55\u4E34\u65F6\u6587\u4EF6\u5931\u8D25: ${tempFile}`, error);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
for (const strategy of strategies) {
|
|
560
|
+
const strategyDir = path4.resolve(process.cwd(), "dist", strategy);
|
|
561
|
+
if (fs3.existsSync(strategyDir)) {
|
|
562
|
+
const strategyTempFiles = glob2.sync("*.mp.temp.html", { cwd: strategyDir });
|
|
563
|
+
for (const tempFile of strategyTempFiles) {
|
|
564
|
+
const tempPath = path4.resolve(strategyDir, tempFile);
|
|
565
|
+
try {
|
|
566
|
+
fs3.unlinkSync(tempPath);
|
|
567
|
+
log(`\u5220\u9664\u7B56\u7565\u76EE\u5F55\u4E34\u65F6\u6587\u4EF6: ${strategy}/${tempFile}`);
|
|
568
|
+
} catch (error) {
|
|
569
|
+
log(`\u5220\u9664\u7B56\u7565\u76EE\u5F55\u4E34\u65F6\u6587\u4EF6\u5931\u8D25: ${strategy}/${tempFile}`, error);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
const distDir = path4.resolve(process.cwd(), "dist");
|
|
575
|
+
for (const strategy of strategies) {
|
|
576
|
+
const strategyDir = path4.resolve(distDir, strategy);
|
|
577
|
+
if (fs3.existsSync(strategyDir)) {
|
|
578
|
+
try {
|
|
579
|
+
fs3.rmSync(strategyDir, { recursive: true, force: true });
|
|
580
|
+
log(`\u5220\u9664\u7B56\u7565\u76EE\u5F55: ${strategy}`);
|
|
581
|
+
} catch (error) {
|
|
582
|
+
log(`\u5220\u9664\u7B56\u7565\u76EE\u5F55\u5931\u8D25: ${strategy}`, error);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
log("\u2705 \u6E05\u7406\u5B8C\u6210");
|
|
587
|
+
}
|
|
588
|
+
async function main() {
|
|
589
|
+
const { viteBuildArgs, debug } = parseArgs();
|
|
590
|
+
const log = debug ? console.log.bind(console, "[main]") : () => {
|
|
591
|
+
};
|
|
592
|
+
try {
|
|
593
|
+
log("\u{1F680} \u5F00\u59CB\u591A\u7B56\u7565\u6784\u5EFA...");
|
|
594
|
+
log("\u{1F4CB} \u52A0\u8F7D\u914D\u7F6E...");
|
|
595
|
+
const options = await loadViteConfig();
|
|
596
|
+
const strategies = getAvailableStrategies({
|
|
597
|
+
entry: options.entry || "src/pages/*/main.{ts,js}",
|
|
598
|
+
exclude: options.exclude || [],
|
|
599
|
+
template: options.template || "index.html",
|
|
600
|
+
placeholder: options.placeholder || "{{ENTRY_FILE}}",
|
|
601
|
+
pageConfigs: options.pageConfigs || {},
|
|
602
|
+
strategies: options.strategies || {}
|
|
603
|
+
});
|
|
604
|
+
if (strategies.length === 0) {
|
|
605
|
+
throw new Error("\u672A\u627E\u5230\u4EFB\u4F55\u6784\u5EFA\u7B56\u7565");
|
|
606
|
+
}
|
|
607
|
+
log(`\u53D1\u73B0 ${strategies.length} \u4E2A\u7B56\u7565: ${strategies.join(", ")}`);
|
|
608
|
+
log("\u{1F528} \u5F00\u59CB\u5E76\u884C\u6784\u5EFA...");
|
|
609
|
+
const buildPromises = strategies.map((strategy) => buildStrategy(strategy, viteBuildArgs, debug));
|
|
610
|
+
const results = await Promise.all(buildPromises);
|
|
611
|
+
const successCount = results.filter((r) => r.success).length;
|
|
612
|
+
const failureCount = results.length - successCount;
|
|
613
|
+
console.log(`
|
|
614
|
+
\u{1F4CA} \u6784\u5EFA\u7ED3\u679C\u7EDF\u8BA1:`);
|
|
615
|
+
console.log(`\u2705 \u6210\u529F: ${successCount}`);
|
|
616
|
+
console.log(`\u274C \u5931\u8D25: ${failureCount}`);
|
|
617
|
+
if (failureCount > 0) {
|
|
618
|
+
console.log(`
|
|
619
|
+
\u274C \u5931\u8D25\u7684\u7B56\u7565:`);
|
|
620
|
+
results.filter((r) => !r.success).forEach((result) => {
|
|
621
|
+
console.log(` - ${result.strategy}: ${result.error}`);
|
|
622
|
+
});
|
|
623
|
+
await cleanupTempFiles(debug);
|
|
624
|
+
process.exit(1);
|
|
625
|
+
}
|
|
626
|
+
log("\u{1F4E6} \u5408\u5E76\u6784\u5EFA\u7ED3\u679C...");
|
|
627
|
+
await mergeResults(results, debug);
|
|
628
|
+
await cleanup(strategies, debug);
|
|
629
|
+
console.log(`
|
|
630
|
+
\u{1F389} \u6240\u6709\u7B56\u7565\u6784\u5EFA\u6210\u529F\uFF01`);
|
|
631
|
+
console.log(`\u{1F4C1} \u6784\u5EFA\u7ED3\u679C\u4F4D\u4E8E: dist/`);
|
|
632
|
+
console.log(
|
|
633
|
+
`\u{1F310} HTML\u6587\u4EF6: ${results.filter((r) => r.success).map((r) => r.strategy).join(", ")}`
|
|
634
|
+
);
|
|
635
|
+
} catch (error) {
|
|
636
|
+
console.error("\u274C \u6784\u5EFA\u5931\u8D25:", error instanceof Error ? error.message : error);
|
|
637
|
+
process.exit(1);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (require.main === module) {
|
|
641
|
+
main().catch((error) => {
|
|
642
|
+
console.error("\u274C \u672A\u5904\u7406\u7684\u9519\u8BEF:", error);
|
|
643
|
+
process.exit(1);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
647
|
+
0 && (module.exports = {
|
|
648
|
+
buildAll
|
|
649
|
+
});
|