@fchc8/vite-plugin-multi-page 1.4.3 → 1.6.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/dist/cli.js +287 -80
- package/dist/index.d.mts +11 -2
- package/dist/index.d.ts +11 -2
- package/dist/index.js +45 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/cli.js
CHANGED
|
@@ -46,8 +46,8 @@ __export(config_loader_exports, {
|
|
|
46
46
|
});
|
|
47
47
|
function hasCustomConfig() {
|
|
48
48
|
for (const filename of CONFIG_FILES) {
|
|
49
|
-
const configPath =
|
|
50
|
-
if (
|
|
49
|
+
const configPath = path.resolve(process.cwd(), filename);
|
|
50
|
+
if (fs.existsSync(configPath)) {
|
|
51
51
|
return true;
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -56,14 +56,18 @@ function hasCustomConfig() {
|
|
|
56
56
|
async function loadUserConfig(context) {
|
|
57
57
|
const customConfig = await loadCustomConfig();
|
|
58
58
|
if (customConfig) {
|
|
59
|
-
|
|
59
|
+
const result = customConfig(context);
|
|
60
|
+
if (!result) {
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
60
64
|
}
|
|
61
65
|
return null;
|
|
62
66
|
}
|
|
63
67
|
async function loadConfigFile(filePath) {
|
|
64
68
|
if (filePath.endsWith(".ts")) {
|
|
65
69
|
try {
|
|
66
|
-
const code = await
|
|
70
|
+
const code = await fs.promises.readFile(filePath, "utf-8");
|
|
67
71
|
const esbuild = await import("esbuild");
|
|
68
72
|
const result = await esbuild.transform(code, {
|
|
69
73
|
loader: "ts",
|
|
@@ -74,16 +78,16 @@ async function loadConfigFile(filePath) {
|
|
|
74
78
|
});
|
|
75
79
|
const tempModule = new import_node_module.Module(filePath);
|
|
76
80
|
tempModule.filename = filePath;
|
|
77
|
-
tempModule.paths = import_node_module.Module._nodeModulePaths(
|
|
81
|
+
tempModule.paths = import_node_module.Module._nodeModulePaths(path.dirname(filePath));
|
|
78
82
|
tempModule._compile(result.code, filePath);
|
|
79
83
|
return tempModule.exports;
|
|
80
84
|
} catch (esbuildError) {
|
|
81
85
|
console.warn("esbuild \u8F6C\u8BD1\u5931\u8D25\uFF0C\u5C1D\u8BD5\u7B80\u5355\u8F6C\u6362:", esbuildError);
|
|
82
|
-
const code = await
|
|
86
|
+
const code = await fs.promises.readFile(filePath, "utf-8");
|
|
83
87
|
const jsCode = code.replace(/export\s+default\s+/, "module.exports = ").replace(/import\s+.*?from\s+['"][^'"]*['"];?\s*/g, "").replace(/:\s*[^=,})\]]+/g, "");
|
|
84
88
|
const tempModule = new import_node_module.Module(filePath);
|
|
85
89
|
tempModule.filename = filePath;
|
|
86
|
-
tempModule.paths = import_node_module.Module._nodeModulePaths(
|
|
90
|
+
tempModule.paths = import_node_module.Module._nodeModulePaths(path.dirname(filePath));
|
|
87
91
|
tempModule._compile(jsCode, filePath);
|
|
88
92
|
return tempModule.exports;
|
|
89
93
|
}
|
|
@@ -97,8 +101,8 @@ async function loadConfigFile(filePath) {
|
|
|
97
101
|
async function loadCustomConfig() {
|
|
98
102
|
const cwd = process.cwd();
|
|
99
103
|
for (const configFile of CONFIG_FILES) {
|
|
100
|
-
const configPath =
|
|
101
|
-
if (
|
|
104
|
+
const configPath = path.resolve(cwd, configFile);
|
|
105
|
+
if (fs.existsSync(configPath)) {
|
|
102
106
|
try {
|
|
103
107
|
const configModule = await loadConfigFile(configPath);
|
|
104
108
|
const configFunction = configModule.default || configModule;
|
|
@@ -119,13 +123,13 @@ async function loadCustomConfig() {
|
|
|
119
123
|
}
|
|
120
124
|
return null;
|
|
121
125
|
}
|
|
122
|
-
var
|
|
126
|
+
var fs, path, import_node_url, import_node_module, CONFIG_FILES;
|
|
123
127
|
var init_config_loader = __esm({
|
|
124
128
|
"src/config-loader.ts"() {
|
|
125
129
|
"use strict";
|
|
126
130
|
init_cjs_shims();
|
|
127
|
-
|
|
128
|
-
|
|
131
|
+
fs = __toESM(require("fs"));
|
|
132
|
+
path = __toESM(require("path"));
|
|
129
133
|
import_node_url = require("url");
|
|
130
134
|
import_node_module = require("module");
|
|
131
135
|
CONFIG_FILES = [
|
|
@@ -186,78 +190,40 @@ var init_defaults = __esm({
|
|
|
186
190
|
}
|
|
187
191
|
});
|
|
188
192
|
|
|
189
|
-
// src/cli.ts
|
|
190
|
-
var cli_exports = {};
|
|
191
|
-
__export(cli_exports, {
|
|
192
|
-
buildAll: () => main
|
|
193
|
-
});
|
|
194
|
-
module.exports = __toCommonJS(cli_exports);
|
|
195
|
-
init_cjs_shims();
|
|
196
|
-
var import_node_child_process = require("child_process");
|
|
197
|
-
var fs3 = __toESM(require("fs"));
|
|
198
|
-
var path4 = __toESM(require("path"));
|
|
199
|
-
var glob2 = __toESM(require("glob"));
|
|
200
|
-
|
|
201
|
-
// src/build-config.ts
|
|
202
|
-
init_cjs_shims();
|
|
203
|
-
var import_vite = require("vite");
|
|
204
|
-
var import_glob = require("glob");
|
|
205
|
-
var path2 = __toESM(require("path"));
|
|
206
|
-
var fs = __toESM(require("fs"));
|
|
207
|
-
|
|
208
193
|
// src/file-filter.ts
|
|
209
|
-
|
|
210
|
-
var path = __toESM(require("path"));
|
|
211
|
-
function filterEntryFiles(files, entry, exclude, log) {
|
|
194
|
+
function filterEntryFiles(files, entry, exclude, _log) {
|
|
212
195
|
const result = [];
|
|
213
196
|
const nameToFile = /* @__PURE__ */ new Map();
|
|
214
197
|
let basePattern = entry.replace(/\/\*.*$/, "");
|
|
215
198
|
if (!basePattern || basePattern === entry) {
|
|
216
|
-
basePattern =
|
|
199
|
+
basePattern = path2.dirname(entry.split("*")[0]);
|
|
217
200
|
}
|
|
218
|
-
log("\u57FA\u7840\u76EE\u5F55\u6A21\u5F0F:", basePattern);
|
|
219
201
|
const candidateFiles = [];
|
|
220
202
|
for (const file of files) {
|
|
221
203
|
if (exclude.includes(file)) {
|
|
222
|
-
log(`\u8DF3\u8FC7\u6392\u9664\u6587\u4EF6: ${file}`);
|
|
223
204
|
continue;
|
|
224
205
|
}
|
|
225
|
-
const relativePath =
|
|
226
|
-
const pathParts = relativePath.split(
|
|
227
|
-
log(`\u5904\u7406\u6587\u4EF6: ${file}`);
|
|
228
|
-
log(`\u76F8\u5BF9\u8DEF\u5F84: ${relativePath}`);
|
|
229
|
-
log(`\u8DEF\u5F84\u90E8\u5206: ${pathParts}`);
|
|
206
|
+
const relativePath = path2.relative(basePattern, file);
|
|
207
|
+
const pathParts = relativePath.split(path2.sep);
|
|
230
208
|
if (pathParts.length === 1) {
|
|
231
209
|
const fileName = pathParts[0];
|
|
232
|
-
const name =
|
|
210
|
+
const name = path2.basename(fileName, path2.extname(fileName));
|
|
233
211
|
candidateFiles.push({ name, file, priority: 1 });
|
|
234
|
-
log(`\u{1F4C4} \u7B2C\u4E00\u7EA7\u6587\u4EF6: ${file} -> ${name}.html (\u4F18\u5148\u7EA7: 1)`);
|
|
235
212
|
} else if (pathParts.length >= 2) {
|
|
236
|
-
const fileName =
|
|
213
|
+
const fileName = path2.basename(file, path2.extname(file));
|
|
237
214
|
const dirName = pathParts[0];
|
|
238
215
|
if (fileName === "main") {
|
|
239
216
|
candidateFiles.push({ name: dirName, file, priority: 2 });
|
|
240
|
-
log(`\u{1F4C1} \u76EE\u5F55main\u6587\u4EF6: ${file} -> ${dirName}.html (\u4F18\u5148\u7EA7: 2)`);
|
|
241
|
-
} else {
|
|
242
|
-
log(`\u274C \u8DF3\u8FC7\u6587\u4EF6: ${file} (\u4E0D\u662Fmain\u6587\u4EF6)`);
|
|
243
217
|
}
|
|
244
|
-
} else {
|
|
245
|
-
log(`\u274C \u8DF3\u8FC7\u6587\u4EF6: ${file} (\u8DEF\u5F84\u5C42\u7EA7\u4E0D\u7B26\u5408)`);
|
|
246
218
|
}
|
|
247
219
|
}
|
|
248
220
|
for (const candidate of candidateFiles) {
|
|
249
221
|
const existing = nameToFile.get(candidate.name);
|
|
250
222
|
if (!existing) {
|
|
251
223
|
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
252
|
-
log(`\u2705 \u6DFB\u52A0\u9875\u9762: ${candidate.name} -> ${candidate.file}`);
|
|
253
224
|
} else {
|
|
254
225
|
if (candidate.priority > existing.priority) {
|
|
255
226
|
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
256
|
-
log(
|
|
257
|
-
`\u{1F504} \u66FF\u6362\u9875\u9762: ${candidate.name} -> ${candidate.file} (\u66FF\u6362 ${existing.file}, \u76EE\u5F55\u4F18\u5148)`
|
|
258
|
-
);
|
|
259
|
-
} else {
|
|
260
|
-
log(`\u26A0\uFE0F \u51B2\u7A81\u8DF3\u8FC7: ${candidate.name} -> ${candidate.file} (\u4FDD\u7559 ${existing.file})`);
|
|
261
227
|
}
|
|
262
228
|
}
|
|
263
229
|
}
|
|
@@ -266,16 +232,22 @@ function filterEntryFiles(files, entry, exclude, log) {
|
|
|
266
232
|
}
|
|
267
233
|
return result;
|
|
268
234
|
}
|
|
235
|
+
var path2;
|
|
236
|
+
var init_file_filter = __esm({
|
|
237
|
+
"src/file-filter.ts"() {
|
|
238
|
+
"use strict";
|
|
239
|
+
init_cjs_shims();
|
|
240
|
+
path2 = __toESM(require("path"));
|
|
241
|
+
}
|
|
242
|
+
});
|
|
269
243
|
|
|
270
244
|
// src/page-config.ts
|
|
271
|
-
init_cjs_shims();
|
|
272
245
|
function getPageConfig(pageConfigs, context, log) {
|
|
273
246
|
if (!pageConfigs)
|
|
274
247
|
return null;
|
|
275
248
|
if (typeof pageConfigs === "function") {
|
|
276
249
|
const result = pageConfigs(context);
|
|
277
250
|
if (result) {
|
|
278
|
-
log(`\u51FD\u6570\u914D\u7F6E\u5339\u914D\u9875\u9762 ${context.pageName}:`, result);
|
|
279
251
|
}
|
|
280
252
|
return result;
|
|
281
253
|
}
|
|
@@ -306,9 +278,14 @@ function simpleMatch(pattern, text) {
|
|
|
306
278
|
const regex = new RegExp(`^${regexPattern}$`);
|
|
307
279
|
return regex.test(text);
|
|
308
280
|
}
|
|
281
|
+
var init_page_config = __esm({
|
|
282
|
+
"src/page-config.ts"() {
|
|
283
|
+
"use strict";
|
|
284
|
+
init_cjs_shims();
|
|
285
|
+
}
|
|
286
|
+
});
|
|
309
287
|
|
|
310
288
|
// src/utils.ts
|
|
311
|
-
init_cjs_shims();
|
|
312
289
|
function createLogger(debug) {
|
|
313
290
|
return (...args) => {
|
|
314
291
|
if (debug) {
|
|
@@ -316,20 +293,235 @@ function createLogger(debug) {
|
|
|
316
293
|
}
|
|
317
294
|
};
|
|
318
295
|
}
|
|
296
|
+
var init_utils = __esm({
|
|
297
|
+
"src/utils.ts"() {
|
|
298
|
+
"use strict";
|
|
299
|
+
init_cjs_shims();
|
|
300
|
+
}
|
|
301
|
+
});
|
|
319
302
|
|
|
320
303
|
// src/build-config.ts
|
|
304
|
+
var build_config_exports = {};
|
|
305
|
+
__export(build_config_exports, {
|
|
306
|
+
cleanViteOutputDirectory: () => cleanViteOutputDirectory,
|
|
307
|
+
generateBuildConfig: () => generateBuildConfig,
|
|
308
|
+
getAvailableStrategies: () => getAvailableStrategies,
|
|
309
|
+
getViteOutputDirectory: () => getViteOutputDirectory
|
|
310
|
+
});
|
|
311
|
+
function generateBuildConfig(options) {
|
|
312
|
+
var _a;
|
|
313
|
+
const {
|
|
314
|
+
entry = "src/pages/*/main.{ts,js}",
|
|
315
|
+
exclude = [],
|
|
316
|
+
template = "index.html",
|
|
317
|
+
placeholder = "<!--VITE_MULTI_PAGE_ENTRY-->",
|
|
318
|
+
strategies = {},
|
|
319
|
+
pageConfigs = {},
|
|
320
|
+
forceBuildStrategy
|
|
321
|
+
} = options;
|
|
322
|
+
const log = createLogger(true);
|
|
323
|
+
const buildConfigs = {};
|
|
324
|
+
try {
|
|
325
|
+
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
326
|
+
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
327
|
+
if (entryFiles.length === 0) {
|
|
328
|
+
log("\u8B66\u544A: \u672A\u627E\u5230\u5339\u914D\u7684\u5165\u53E3\u6587\u4EF6");
|
|
329
|
+
return {};
|
|
330
|
+
}
|
|
331
|
+
const pageStrategies = /* @__PURE__ */ new Map();
|
|
332
|
+
const strategyPages = /* @__PURE__ */ new Map();
|
|
333
|
+
for (const entryFile of entryFiles) {
|
|
334
|
+
const pageContext = {
|
|
335
|
+
pageName: entryFile.name,
|
|
336
|
+
filePath: entryFile.file,
|
|
337
|
+
relativePath: path3.relative(process.cwd(), entryFile.file)
|
|
338
|
+
};
|
|
339
|
+
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
340
|
+
const strategyName = (pageConfig == null ? void 0 : pageConfig.strategy) || "default";
|
|
341
|
+
pageStrategies.set(entryFile.name, strategyName);
|
|
342
|
+
if (!strategyPages.has(strategyName)) {
|
|
343
|
+
strategyPages.set(strategyName, []);
|
|
344
|
+
}
|
|
345
|
+
(_a = strategyPages.get(strategyName)) == null ? void 0 : _a.push(entryFile.name);
|
|
346
|
+
}
|
|
347
|
+
log(`\u{1F4C4} \u53D1\u73B0 ${entryFiles.length} \u4E2A\u9875\u9762: ${entryFiles.map((f) => f.name).join(", ")}`);
|
|
348
|
+
if (forceBuildStrategy) {
|
|
349
|
+
const targetPages = strategyPages.get(forceBuildStrategy) || [];
|
|
350
|
+
if (targetPages.length === 0) {
|
|
351
|
+
log(`\u8B66\u544A: \u7B56\u7565 "${forceBuildStrategy}" \u4E0B\u6CA1\u6709\u9875\u9762`);
|
|
352
|
+
return {};
|
|
353
|
+
}
|
|
354
|
+
log(`\u5F3A\u5236\u6784\u5EFA\u7B56\u7565: ${forceBuildStrategy}, \u9875\u9762: ${targetPages.join(", ")}`);
|
|
355
|
+
const config = generateStrategyConfig(
|
|
356
|
+
forceBuildStrategy,
|
|
357
|
+
targetPages,
|
|
358
|
+
entryFiles,
|
|
359
|
+
strategies[forceBuildStrategy],
|
|
360
|
+
pageConfigs,
|
|
361
|
+
template,
|
|
362
|
+
placeholder,
|
|
363
|
+
log
|
|
364
|
+
);
|
|
365
|
+
buildConfigs[forceBuildStrategy] = config;
|
|
366
|
+
return buildConfigs;
|
|
367
|
+
}
|
|
368
|
+
for (const [strategyName, pages] of strategyPages) {
|
|
369
|
+
if (pages.length === 0)
|
|
370
|
+
continue;
|
|
371
|
+
const strategyConfig = strategies[strategyName] || {};
|
|
372
|
+
const config = generateStrategyConfig(
|
|
373
|
+
strategyName,
|
|
374
|
+
pages,
|
|
375
|
+
entryFiles,
|
|
376
|
+
strategyConfig,
|
|
377
|
+
pageConfigs,
|
|
378
|
+
template,
|
|
379
|
+
placeholder,
|
|
380
|
+
log
|
|
381
|
+
);
|
|
382
|
+
buildConfigs[strategyName] = config;
|
|
383
|
+
}
|
|
384
|
+
if (Object.keys(buildConfigs).length === 0) {
|
|
385
|
+
log("\u8B66\u544A: \u672A\u751F\u6210\u4EFB\u4F55\u6784\u5EFA\u914D\u7F6E\uFF0C\u521B\u5EFA\u9ED8\u8BA4\u914D\u7F6E");
|
|
386
|
+
const allPageNames = entryFiles.map((f) => f.name);
|
|
387
|
+
const defaultConfig = generateStrategyConfig(
|
|
388
|
+
"default",
|
|
389
|
+
allPageNames,
|
|
390
|
+
entryFiles,
|
|
391
|
+
{},
|
|
392
|
+
pageConfigs,
|
|
393
|
+
template,
|
|
394
|
+
placeholder,
|
|
395
|
+
log
|
|
396
|
+
);
|
|
397
|
+
buildConfigs["default"] = defaultConfig;
|
|
398
|
+
}
|
|
399
|
+
const strategyNames = Object.keys(buildConfigs);
|
|
400
|
+
log(`\u{1F4E6} \u6784\u5EFA\u7B56\u7565: ${strategyNames.join(", ")}`);
|
|
401
|
+
return buildConfigs;
|
|
402
|
+
} catch (error) {
|
|
403
|
+
log("\u751F\u6210\u6784\u5EFA\u914D\u7F6E\u5931\u8D25:", error);
|
|
404
|
+
throw error;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function generateStrategyConfig(strategyName, pages, entryFiles, strategyConfig, pageConfigs, defaultTemplate, placeholder, log) {
|
|
408
|
+
const htmlInputs = {};
|
|
409
|
+
const tempFiles = [];
|
|
410
|
+
const allPageDefines = {};
|
|
411
|
+
for (const pageName of pages) {
|
|
412
|
+
const entryFile = entryFiles.find((f) => f.name === pageName);
|
|
413
|
+
if (!entryFile)
|
|
414
|
+
continue;
|
|
415
|
+
const pageContext = {
|
|
416
|
+
pageName,
|
|
417
|
+
filePath: entryFile.file,
|
|
418
|
+
relativePath: path3.relative(process.cwd(), entryFile.file),
|
|
419
|
+
strategy: strategyName
|
|
420
|
+
};
|
|
421
|
+
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
422
|
+
if (pageConfig == null ? void 0 : pageConfig.define) {
|
|
423
|
+
Object.assign(allPageDefines, pageConfig.define);
|
|
424
|
+
}
|
|
425
|
+
let templatePath = defaultTemplate;
|
|
426
|
+
const pageSpecificTemplate = `${pageName}.html`;
|
|
427
|
+
if (fs2.existsSync(path3.resolve(process.cwd(), pageSpecificTemplate))) {
|
|
428
|
+
templatePath = pageSpecificTemplate;
|
|
429
|
+
} else if (pageConfig == null ? void 0 : pageConfig.template) {
|
|
430
|
+
templatePath = pageConfig.template;
|
|
431
|
+
}
|
|
432
|
+
const templateFullPath = path3.resolve(process.cwd(), templatePath);
|
|
433
|
+
if (!fs2.existsSync(templateFullPath)) {
|
|
434
|
+
log(`\u8B66\u544A: \u6A21\u677F\u6587\u4EF6\u4E0D\u5B58\u5728: ${templatePath}`);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
let templateContent = fs2.readFileSync(templateFullPath, "utf-8");
|
|
438
|
+
if (templateContent.includes(placeholder)) {
|
|
439
|
+
const entryPath = `./${entryFile.file}`;
|
|
440
|
+
templateContent = templateContent.replace(
|
|
441
|
+
new RegExp(placeholder.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"),
|
|
442
|
+
entryPath
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
const tempHtmlPath = path3.resolve(process.cwd(), `.temp.mp.${pageName}.html`);
|
|
446
|
+
fs2.writeFileSync(tempHtmlPath, templateContent);
|
|
447
|
+
tempFiles.push(tempHtmlPath);
|
|
448
|
+
htmlInputs[pageName] = tempHtmlPath;
|
|
449
|
+
}
|
|
450
|
+
const baseConfig = {
|
|
451
|
+
build: {
|
|
452
|
+
rollupOptions: {
|
|
453
|
+
input: htmlInputs,
|
|
454
|
+
// 使用临时HTML文件作为输入
|
|
455
|
+
output: {
|
|
456
|
+
entryFileNames: "assets/[name]-[hash].js",
|
|
457
|
+
chunkFileNames: "assets/[name]-[hash].js",
|
|
458
|
+
assetFileNames: "assets/[name]-[hash][extname]"
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
emptyOutDir: false
|
|
462
|
+
// 不清空输出目录,避免删除临时HTML文件
|
|
463
|
+
},
|
|
464
|
+
define: {}
|
|
465
|
+
};
|
|
466
|
+
let config = baseConfig;
|
|
467
|
+
if (strategyConfig) {
|
|
468
|
+
config = (0, import_vite.mergeConfig)(baseConfig, strategyConfig);
|
|
469
|
+
}
|
|
470
|
+
if (Object.keys(allPageDefines).length > 0) {
|
|
471
|
+
config.define = {
|
|
472
|
+
...config.define,
|
|
473
|
+
...allPageDefines
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
if (!config.build)
|
|
477
|
+
config.build = {};
|
|
478
|
+
if (!config.build.rollupOptions)
|
|
479
|
+
config.build.rollupOptions = {};
|
|
480
|
+
config.build.rollupOptions.input = htmlInputs;
|
|
481
|
+
config.build.emptyOutDir = false;
|
|
482
|
+
log(`\u7B56\u7565 "${strategyName}" - ${pages.length} \u4E2A\u9875\u9762`);
|
|
483
|
+
return config;
|
|
484
|
+
}
|
|
485
|
+
function getViteOutputDirectory(viteBuildArgs = []) {
|
|
486
|
+
const outDirIndex = viteBuildArgs.findIndex((arg) => arg === "--outDir");
|
|
487
|
+
if (outDirIndex !== -1 && outDirIndex + 1 < viteBuildArgs.length) {
|
|
488
|
+
const outDir = viteBuildArgs[outDirIndex + 1];
|
|
489
|
+
return path3.resolve(process.cwd(), outDir);
|
|
490
|
+
}
|
|
491
|
+
const outDirArg = viteBuildArgs.find((arg) => arg.startsWith("--outDir="));
|
|
492
|
+
if (outDirArg) {
|
|
493
|
+
const outDir = outDirArg.split("=")[1];
|
|
494
|
+
return path3.resolve(process.cwd(), outDir);
|
|
495
|
+
}
|
|
496
|
+
return path3.resolve(process.cwd(), "dist");
|
|
497
|
+
}
|
|
498
|
+
function cleanViteOutputDirectory(viteBuildArgs = []) {
|
|
499
|
+
const outputDir = getViteOutputDirectory(viteBuildArgs);
|
|
500
|
+
const log = createLogger(true);
|
|
501
|
+
try {
|
|
502
|
+
if (fs2.existsSync(outputDir)) {
|
|
503
|
+
fs2.rmSync(outputDir, { recursive: true, force: true });
|
|
504
|
+
log(`\u{1F9F9} \u6E05\u7406\u8F93\u51FA\u76EE\u5F55: ${path3.relative(process.cwd(), outputDir)}`);
|
|
505
|
+
}
|
|
506
|
+
} catch (error) {
|
|
507
|
+
log(`\u26A0\uFE0F \u6E05\u7406\u8F93\u51FA\u76EE\u5F55\u5931\u8D25: ${outputDir}`, error);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
321
510
|
function getAvailableStrategies(options) {
|
|
322
511
|
const { entry = "src/pages/*/main.{ts,js}", exclude = [], pageConfigs = {} } = options;
|
|
323
512
|
const log = createLogger(false);
|
|
324
513
|
const strategySet = /* @__PURE__ */ new Set();
|
|
514
|
+
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
515
|
+
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
516
|
+
if (entryFiles.length === 0) {
|
|
517
|
+
throw new Error(`\u672A\u627E\u5230\u5339\u914D\u7684\u5165\u53E3\u6587\u4EF6: ${entry}`);
|
|
518
|
+
}
|
|
325
519
|
try {
|
|
326
|
-
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
327
|
-
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
328
520
|
for (const entryFile of entryFiles) {
|
|
329
521
|
const pageContext = {
|
|
330
522
|
pageName: entryFile.name,
|
|
331
523
|
filePath: entryFile.file,
|
|
332
|
-
relativePath:
|
|
524
|
+
relativePath: path3.relative(process.cwd(), entryFile.file)
|
|
333
525
|
};
|
|
334
526
|
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
335
527
|
const strategyName = (pageConfig == null ? void 0 : pageConfig.strategy) || "default";
|
|
@@ -341,8 +533,32 @@ function getAvailableStrategies(options) {
|
|
|
341
533
|
return ["default"];
|
|
342
534
|
}
|
|
343
535
|
}
|
|
536
|
+
var import_vite, import_glob, path3, fs2;
|
|
537
|
+
var init_build_config = __esm({
|
|
538
|
+
"src/build-config.ts"() {
|
|
539
|
+
"use strict";
|
|
540
|
+
init_cjs_shims();
|
|
541
|
+
import_vite = require("vite");
|
|
542
|
+
import_glob = require("glob");
|
|
543
|
+
path3 = __toESM(require("path"));
|
|
544
|
+
fs2 = __toESM(require("fs"));
|
|
545
|
+
init_file_filter();
|
|
546
|
+
init_page_config();
|
|
547
|
+
init_utils();
|
|
548
|
+
}
|
|
549
|
+
});
|
|
344
550
|
|
|
345
551
|
// src/cli.ts
|
|
552
|
+
var cli_exports = {};
|
|
553
|
+
__export(cli_exports, {
|
|
554
|
+
buildAll: () => main
|
|
555
|
+
});
|
|
556
|
+
module.exports = __toCommonJS(cli_exports);
|
|
557
|
+
init_cjs_shims();
|
|
558
|
+
var import_node_child_process = require("child_process");
|
|
559
|
+
var fs3 = __toESM(require("fs"));
|
|
560
|
+
var path4 = __toESM(require("path"));
|
|
561
|
+
var glob2 = __toESM(require("glob"));
|
|
346
562
|
function parseArgs() {
|
|
347
563
|
const args = process.argv.slice(2);
|
|
348
564
|
const viteBuildArgs = [];
|
|
@@ -366,11 +582,10 @@ function parseArgs() {
|
|
|
366
582
|
\u5176\u4ED6\u6240\u6709\u53C2\u6570\u5C06\u4F20\u9012\u7ED9 vite build \u547D\u4EE4
|
|
367
583
|
|
|
368
584
|
\u793A\u4F8B:
|
|
369
|
-
vite-
|
|
370
|
-
vite-
|
|
371
|
-
vite-
|
|
372
|
-
vite-
|
|
373
|
-
vite-multi-page-build --mode production --debug
|
|
585
|
+
vite-mp
|
|
586
|
+
vite-mp --debug
|
|
587
|
+
vite-mp --cwd example
|
|
588
|
+
vite-mp --mode production --debug
|
|
374
589
|
`);
|
|
375
590
|
process.exit(0);
|
|
376
591
|
} else if (arg !== "build") {
|
|
@@ -631,7 +846,8 @@ async function main() {
|
|
|
631
846
|
log("\u{1F680} \u5F00\u59CB\u591A\u7B56\u7565\u6784\u5EFA...");
|
|
632
847
|
log("\u{1F4CB} \u52A0\u8F7D\u914D\u7F6E...");
|
|
633
848
|
const options = await loadViteConfig();
|
|
634
|
-
const
|
|
849
|
+
const { getAvailableStrategies: getAvailableStrategies2 } = await Promise.resolve().then(() => (init_build_config(), build_config_exports));
|
|
850
|
+
const strategies = getAvailableStrategies2({
|
|
635
851
|
entry: options.entry || "src/pages/*/main.{ts,js}",
|
|
636
852
|
exclude: options.exclude || [],
|
|
637
853
|
template: options.template || "index.html",
|
|
@@ -643,6 +859,9 @@ async function main() {
|
|
|
643
859
|
throw new Error("\u672A\u627E\u5230\u4EFB\u4F55\u6784\u5EFA\u7B56\u7565");
|
|
644
860
|
}
|
|
645
861
|
log(`\u53D1\u73B0 ${strategies.length} \u4E2A\u7B56\u7565: ${strategies.join(", ")}`);
|
|
862
|
+
log("\u{1F9F9} \u6E05\u7406\u8F93\u51FA\u76EE\u5F55...");
|
|
863
|
+
const { cleanViteOutputDirectory: cleanViteOutputDirectory2 } = await Promise.resolve().then(() => (init_build_config(), build_config_exports));
|
|
864
|
+
cleanViteOutputDirectory2(viteBuildArgs);
|
|
646
865
|
log("\u{1F528} \u5F00\u59CB\u5E76\u884C\u6784\u5EFA...");
|
|
647
866
|
const buildPromises = strategies.map((strategy) => buildStrategy(strategy, viteBuildArgs, debug));
|
|
648
867
|
const results = await Promise.all(buildPromises);
|
|
@@ -671,18 +890,6 @@ async function main() {
|
|
|
671
890
|
console.log(`\u{1F4C1} \u6784\u5EFA\u7ED3\u679C\u4F4D\u4E8E: dist/`);
|
|
672
891
|
console.log(`\u{1F310} \u751F\u6210\u7684\u9875\u9762: ${htmlFiles.join(", ")}`);
|
|
673
892
|
console.log(`\u{1F4E6} \u6784\u5EFA\u7B56\u7565: ${successfulResults.map((r) => r.strategy).join(", ")}`);
|
|
674
|
-
if (debug) {
|
|
675
|
-
console.log(`
|
|
676
|
-
\u{1F4CB} \u8BE6\u7EC6\u4FE1\u606F:`);
|
|
677
|
-
successfulResults.forEach((result) => {
|
|
678
|
-
const strategyDir = path4.resolve(process.cwd(), result.outputDir);
|
|
679
|
-
if (fs3.existsSync(strategyDir)) {
|
|
680
|
-
const files = fs3.readdirSync(strategyDir);
|
|
681
|
-
const htmlCount = files.filter((f) => f.endsWith(".html")).length;
|
|
682
|
-
console.log(` - ${result.strategy}: ${htmlCount} \u4E2A\u9875\u9762`);
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
}
|
|
686
893
|
} catch (error) {
|
|
687
894
|
console.error("\u274C \u6784\u5EFA\u5931\u8D25:", error instanceof Error ? error.message : error);
|
|
688
895
|
process.exit(1);
|
package/dist/index.d.mts
CHANGED
|
@@ -53,6 +53,15 @@ declare function defineConfigTransform(transform: ConfigTransformFunction): Conf
|
|
|
53
53
|
* 根据策略和页面配置生成多页面构建配置
|
|
54
54
|
*/
|
|
55
55
|
declare function generateBuildConfig(options: BuildConfigOptions): Record<string, UserConfig>;
|
|
56
|
+
/**
|
|
57
|
+
* 获取Vite配置的输出目录
|
|
58
|
+
* 需要传入已解析的Vite配置或命令行参数
|
|
59
|
+
*/
|
|
60
|
+
declare function getViteOutputDirectory(viteBuildArgs?: string[]): string;
|
|
61
|
+
/**
|
|
62
|
+
* 清理Vite配置的输出目录
|
|
63
|
+
*/
|
|
64
|
+
declare function cleanViteOutputDirectory(viteBuildArgs?: string[]): void;
|
|
56
65
|
/**
|
|
57
66
|
* 获取所有可用的构建策略
|
|
58
67
|
*/
|
|
@@ -61,8 +70,8 @@ declare function getAvailableStrategies(options: BuildConfigOptions): string[];
|
|
|
61
70
|
/**
|
|
62
71
|
* 合并用户配置和默认配置
|
|
63
72
|
*/
|
|
64
|
-
declare function mergeWithDefaults(userConfig: MultiPageOptions | null): MultiPageOptions;
|
|
73
|
+
declare function mergeWithDefaults(userConfig: MultiPageOptions | null | undefined): MultiPageOptions;
|
|
65
74
|
|
|
66
75
|
declare function viteMultiPage(transform?: ConfigTransformFunction): Plugin;
|
|
67
76
|
|
|
68
|
-
export { type ConfigFunction, type ConfigTransformFunction, type Options, type PageConfig, type PageContext, type PluginContext, viteMultiPage as default, defineConfig, defineConfigTransform, generateBuildConfig, getAvailableStrategies, mergeWithDefaults, viteMultiPage };
|
|
77
|
+
export { type ConfigFunction, type ConfigTransformFunction, type Options, type PageConfig, type PageContext, type PluginContext, cleanViteOutputDirectory, viteMultiPage as default, defineConfig, defineConfigTransform, generateBuildConfig, getAvailableStrategies, getViteOutputDirectory, mergeWithDefaults, viteMultiPage };
|
package/dist/index.d.ts
CHANGED
|
@@ -53,6 +53,15 @@ declare function defineConfigTransform(transform: ConfigTransformFunction): Conf
|
|
|
53
53
|
* 根据策略和页面配置生成多页面构建配置
|
|
54
54
|
*/
|
|
55
55
|
declare function generateBuildConfig(options: BuildConfigOptions): Record<string, UserConfig>;
|
|
56
|
+
/**
|
|
57
|
+
* 获取Vite配置的输出目录
|
|
58
|
+
* 需要传入已解析的Vite配置或命令行参数
|
|
59
|
+
*/
|
|
60
|
+
declare function getViteOutputDirectory(viteBuildArgs?: string[]): string;
|
|
61
|
+
/**
|
|
62
|
+
* 清理Vite配置的输出目录
|
|
63
|
+
*/
|
|
64
|
+
declare function cleanViteOutputDirectory(viteBuildArgs?: string[]): void;
|
|
56
65
|
/**
|
|
57
66
|
* 获取所有可用的构建策略
|
|
58
67
|
*/
|
|
@@ -61,8 +70,8 @@ declare function getAvailableStrategies(options: BuildConfigOptions): string[];
|
|
|
61
70
|
/**
|
|
62
71
|
* 合并用户配置和默认配置
|
|
63
72
|
*/
|
|
64
|
-
declare function mergeWithDefaults(userConfig: MultiPageOptions | null): MultiPageOptions;
|
|
73
|
+
declare function mergeWithDefaults(userConfig: MultiPageOptions | null | undefined): MultiPageOptions;
|
|
65
74
|
|
|
66
75
|
declare function viteMultiPage(transform?: ConfigTransformFunction): Plugin;
|
|
67
76
|
|
|
68
|
-
export { type ConfigFunction, type ConfigTransformFunction, type Options, type PageConfig, type PageContext, type PluginContext, viteMultiPage as default, defineConfig, defineConfigTransform, generateBuildConfig, getAvailableStrategies, mergeWithDefaults, viteMultiPage };
|
|
77
|
+
export { type ConfigFunction, type ConfigTransformFunction, type Options, type PageConfig, type PageContext, type PluginContext, cleanViteOutputDirectory, viteMultiPage as default, defineConfig, defineConfigTransform, generateBuildConfig, getAvailableStrategies, getViteOutputDirectory, mergeWithDefaults, viteMultiPage };
|