@fchc8/vite-plugin-multi-page 1.5.0 → 1.7.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 +376 -142
- package/dist/index.d.mts +10 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +36 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -38,6 +38,364 @@ var init_cjs_shims = __esm({
|
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
+
// src/file-filter.ts
|
|
42
|
+
function filterEntryFiles(files, entry, exclude, _log) {
|
|
43
|
+
const result = [];
|
|
44
|
+
const nameToFile = /* @__PURE__ */ new Map();
|
|
45
|
+
let basePattern = entry.replace(/\/\*.*$/, "");
|
|
46
|
+
if (!basePattern || basePattern === entry) {
|
|
47
|
+
basePattern = path.dirname(entry.split("*")[0]);
|
|
48
|
+
}
|
|
49
|
+
const candidateFiles = [];
|
|
50
|
+
for (const file of files) {
|
|
51
|
+
if (exclude.includes(file)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const relativePath = path.relative(basePattern, file);
|
|
55
|
+
const pathParts = relativePath.split(path.sep);
|
|
56
|
+
if (pathParts.length === 1) {
|
|
57
|
+
const fileName = pathParts[0];
|
|
58
|
+
const name = path.basename(fileName, path.extname(fileName));
|
|
59
|
+
candidateFiles.push({ name, file, priority: 1 });
|
|
60
|
+
} else if (pathParts.length >= 2) {
|
|
61
|
+
const fileName = path.basename(file, path.extname(file));
|
|
62
|
+
const dirName = pathParts[0];
|
|
63
|
+
if (fileName === "main") {
|
|
64
|
+
candidateFiles.push({ name: dirName, file, priority: 2 });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (const candidate of candidateFiles) {
|
|
69
|
+
const existing = nameToFile.get(candidate.name);
|
|
70
|
+
if (!existing) {
|
|
71
|
+
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
72
|
+
} else {
|
|
73
|
+
if (candidate.priority > existing.priority) {
|
|
74
|
+
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const [name, { file }] of nameToFile.entries()) {
|
|
79
|
+
result.push({ name, file });
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
var path;
|
|
84
|
+
var init_file_filter = __esm({
|
|
85
|
+
"src/file-filter.ts"() {
|
|
86
|
+
"use strict";
|
|
87
|
+
init_cjs_shims();
|
|
88
|
+
path = __toESM(require("path"));
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// src/page-config.ts
|
|
93
|
+
function getPageConfig(pageConfigs, context, log) {
|
|
94
|
+
if (!pageConfigs)
|
|
95
|
+
return null;
|
|
96
|
+
if (typeof pageConfigs === "function") {
|
|
97
|
+
const result = pageConfigs(context);
|
|
98
|
+
if (result) {
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
for (const [key, config] of Object.entries(pageConfigs)) {
|
|
103
|
+
if (key === context.pageName) {
|
|
104
|
+
log(`\u7CBE\u786E\u5339\u914D\u9875\u9762 ${context.pageName}:`, config);
|
|
105
|
+
return config;
|
|
106
|
+
}
|
|
107
|
+
if (config.match) {
|
|
108
|
+
const patterns = Array.isArray(config.match) ? config.match : [config.match];
|
|
109
|
+
const isMatched = patterns.some(
|
|
110
|
+
(pattern) => simpleMatch(pattern, context.pageName) || simpleMatch(pattern, context.relativePath) || simpleMatch(pattern, context.filePath)
|
|
111
|
+
);
|
|
112
|
+
if (isMatched) {
|
|
113
|
+
log(`\u6A21\u5F0F\u5339\u914D\u9875\u9762 ${context.pageName} (\u6A21\u5F0F: ${config.match}):`, config);
|
|
114
|
+
return { ...config, match: void 0 };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (simpleMatch(key, context.pageName)) {
|
|
118
|
+
log(`Glob\u5339\u914D\u9875\u9762 ${context.pageName} (\u6A21\u5F0F: ${key}):`, config);
|
|
119
|
+
return config;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function simpleMatch(pattern, text) {
|
|
125
|
+
const regexPattern = pattern.replace(/\*\*/g, "__DOUBLE_STAR__").replace(/\*/g, "[^/]*").replace(/__DOUBLE_STAR__/g, ".*");
|
|
126
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
127
|
+
return regex.test(text);
|
|
128
|
+
}
|
|
129
|
+
var init_page_config = __esm({
|
|
130
|
+
"src/page-config.ts"() {
|
|
131
|
+
"use strict";
|
|
132
|
+
init_cjs_shims();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// src/utils.ts
|
|
137
|
+
function createLogger(debug) {
|
|
138
|
+
return (...args) => {
|
|
139
|
+
if (debug) {
|
|
140
|
+
console.log("[vite-plugin-multi-page]", ...args);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
var init_utils = __esm({
|
|
145
|
+
"src/utils.ts"() {
|
|
146
|
+
"use strict";
|
|
147
|
+
init_cjs_shims();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// src/build-config.ts
|
|
152
|
+
var build_config_exports = {};
|
|
153
|
+
__export(build_config_exports, {
|
|
154
|
+
cleanViteOutputDirectory: () => cleanViteOutputDirectory,
|
|
155
|
+
generateBuildConfig: () => generateBuildConfig,
|
|
156
|
+
getAvailableStrategies: () => getAvailableStrategies,
|
|
157
|
+
getViteOutputDirectory: () => getViteOutputDirectory
|
|
158
|
+
});
|
|
159
|
+
function generateBuildConfig(options) {
|
|
160
|
+
var _a;
|
|
161
|
+
const {
|
|
162
|
+
entry = "src/pages/*/main.{ts,js}",
|
|
163
|
+
exclude = [],
|
|
164
|
+
template = "index.html",
|
|
165
|
+
placeholder = "<!--VITE_MULTI_PAGE_ENTRY-->",
|
|
166
|
+
strategies = {},
|
|
167
|
+
pageConfigs = {},
|
|
168
|
+
forceBuildStrategy
|
|
169
|
+
} = options;
|
|
170
|
+
const log = createLogger(true);
|
|
171
|
+
const buildConfigs = {};
|
|
172
|
+
try {
|
|
173
|
+
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
174
|
+
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
175
|
+
if (entryFiles.length === 0) {
|
|
176
|
+
log("\u8B66\u544A: \u672A\u627E\u5230\u5339\u914D\u7684\u5165\u53E3\u6587\u4EF6");
|
|
177
|
+
return {};
|
|
178
|
+
}
|
|
179
|
+
const pageStrategies = /* @__PURE__ */ new Map();
|
|
180
|
+
const strategyPages = /* @__PURE__ */ new Map();
|
|
181
|
+
for (const entryFile of entryFiles) {
|
|
182
|
+
const pageContext = {
|
|
183
|
+
pageName: entryFile.name,
|
|
184
|
+
filePath: entryFile.file,
|
|
185
|
+
relativePath: path2.relative(process.cwd(), entryFile.file)
|
|
186
|
+
};
|
|
187
|
+
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
188
|
+
const strategyName = (pageConfig == null ? void 0 : pageConfig.strategy) || "default";
|
|
189
|
+
pageStrategies.set(entryFile.name, strategyName);
|
|
190
|
+
if (!strategyPages.has(strategyName)) {
|
|
191
|
+
strategyPages.set(strategyName, []);
|
|
192
|
+
}
|
|
193
|
+
(_a = strategyPages.get(strategyName)) == null ? void 0 : _a.push(entryFile.name);
|
|
194
|
+
}
|
|
195
|
+
log(`\u{1F4C4} \u53D1\u73B0 ${entryFiles.length} \u4E2A\u9875\u9762: ${entryFiles.map((f) => f.name).join(", ")}`);
|
|
196
|
+
if (forceBuildStrategy) {
|
|
197
|
+
const targetPages = strategyPages.get(forceBuildStrategy) || [];
|
|
198
|
+
if (targetPages.length === 0) {
|
|
199
|
+
log(`\u8B66\u544A: \u7B56\u7565 "${forceBuildStrategy}" \u4E0B\u6CA1\u6709\u9875\u9762`);
|
|
200
|
+
return {};
|
|
201
|
+
}
|
|
202
|
+
log(`\u5F3A\u5236\u6784\u5EFA\u7B56\u7565: ${forceBuildStrategy}, \u9875\u9762: ${targetPages.join(", ")}`);
|
|
203
|
+
const config = generateStrategyConfig(
|
|
204
|
+
forceBuildStrategy,
|
|
205
|
+
targetPages,
|
|
206
|
+
entryFiles,
|
|
207
|
+
strategies[forceBuildStrategy],
|
|
208
|
+
pageConfigs,
|
|
209
|
+
template,
|
|
210
|
+
placeholder,
|
|
211
|
+
log
|
|
212
|
+
);
|
|
213
|
+
buildConfigs[forceBuildStrategy] = config;
|
|
214
|
+
return buildConfigs;
|
|
215
|
+
}
|
|
216
|
+
for (const [strategyName, pages] of strategyPages) {
|
|
217
|
+
if (pages.length === 0)
|
|
218
|
+
continue;
|
|
219
|
+
const strategyConfig = strategies[strategyName] || {};
|
|
220
|
+
const config = generateStrategyConfig(
|
|
221
|
+
strategyName,
|
|
222
|
+
pages,
|
|
223
|
+
entryFiles,
|
|
224
|
+
strategyConfig,
|
|
225
|
+
pageConfigs,
|
|
226
|
+
template,
|
|
227
|
+
placeholder,
|
|
228
|
+
log
|
|
229
|
+
);
|
|
230
|
+
buildConfigs[strategyName] = config;
|
|
231
|
+
}
|
|
232
|
+
if (Object.keys(buildConfigs).length === 0) {
|
|
233
|
+
log("\u8B66\u544A: \u672A\u751F\u6210\u4EFB\u4F55\u6784\u5EFA\u914D\u7F6E\uFF0C\u521B\u5EFA\u9ED8\u8BA4\u914D\u7F6E");
|
|
234
|
+
const allPageNames = entryFiles.map((f) => f.name);
|
|
235
|
+
const defaultConfig = generateStrategyConfig(
|
|
236
|
+
"default",
|
|
237
|
+
allPageNames,
|
|
238
|
+
entryFiles,
|
|
239
|
+
{},
|
|
240
|
+
pageConfigs,
|
|
241
|
+
template,
|
|
242
|
+
placeholder,
|
|
243
|
+
log
|
|
244
|
+
);
|
|
245
|
+
buildConfigs["default"] = defaultConfig;
|
|
246
|
+
}
|
|
247
|
+
const strategyNames = Object.keys(buildConfigs);
|
|
248
|
+
log(`\u{1F4E6} \u6784\u5EFA\u7B56\u7565: ${strategyNames.join(", ")}`);
|
|
249
|
+
return buildConfigs;
|
|
250
|
+
} catch (error) {
|
|
251
|
+
log("\u751F\u6210\u6784\u5EFA\u914D\u7F6E\u5931\u8D25:", error);
|
|
252
|
+
throw error;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function generateStrategyConfig(strategyName, pages, entryFiles, strategyConfig, pageConfigs, defaultTemplate, placeholder, log) {
|
|
256
|
+
const htmlInputs = {};
|
|
257
|
+
const tempFiles = [];
|
|
258
|
+
const allPageDefines = {};
|
|
259
|
+
for (const pageName of pages) {
|
|
260
|
+
const entryFile = entryFiles.find((f) => f.name === pageName);
|
|
261
|
+
if (!entryFile)
|
|
262
|
+
continue;
|
|
263
|
+
const pageContext = {
|
|
264
|
+
pageName,
|
|
265
|
+
filePath: entryFile.file,
|
|
266
|
+
relativePath: path2.relative(process.cwd(), entryFile.file),
|
|
267
|
+
strategy: strategyName
|
|
268
|
+
};
|
|
269
|
+
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
270
|
+
if (pageConfig == null ? void 0 : pageConfig.define) {
|
|
271
|
+
Object.assign(allPageDefines, pageConfig.define);
|
|
272
|
+
}
|
|
273
|
+
let templatePath = defaultTemplate;
|
|
274
|
+
const pageSpecificTemplate = `${pageName}.html`;
|
|
275
|
+
if (fs.existsSync(path2.resolve(process.cwd(), pageSpecificTemplate))) {
|
|
276
|
+
templatePath = pageSpecificTemplate;
|
|
277
|
+
} else if (pageConfig == null ? void 0 : pageConfig.template) {
|
|
278
|
+
templatePath = pageConfig.template;
|
|
279
|
+
}
|
|
280
|
+
const templateFullPath = path2.resolve(process.cwd(), templatePath);
|
|
281
|
+
if (!fs.existsSync(templateFullPath)) {
|
|
282
|
+
log(`\u8B66\u544A: \u6A21\u677F\u6587\u4EF6\u4E0D\u5B58\u5728: ${templatePath}`);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
let templateContent = fs.readFileSync(templateFullPath, "utf-8");
|
|
286
|
+
if (templateContent.includes(placeholder)) {
|
|
287
|
+
const entryPath = `./${entryFile.file}`;
|
|
288
|
+
templateContent = templateContent.replace(
|
|
289
|
+
new RegExp(placeholder.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"),
|
|
290
|
+
entryPath
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
const tempHtmlPath = path2.resolve(process.cwd(), `.temp.mp.${pageName}.html`);
|
|
294
|
+
fs.writeFileSync(tempHtmlPath, templateContent);
|
|
295
|
+
tempFiles.push(tempHtmlPath);
|
|
296
|
+
htmlInputs[pageName] = tempHtmlPath;
|
|
297
|
+
}
|
|
298
|
+
const baseConfig = {
|
|
299
|
+
build: {
|
|
300
|
+
rollupOptions: {
|
|
301
|
+
input: htmlInputs,
|
|
302
|
+
// 使用临时HTML文件作为输入
|
|
303
|
+
output: {
|
|
304
|
+
entryFileNames: "assets/[name]-[hash].js",
|
|
305
|
+
chunkFileNames: "assets/[name]-[hash].js",
|
|
306
|
+
assetFileNames: "assets/[name]-[hash][extname]"
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
emptyOutDir: false
|
|
310
|
+
// 不清空输出目录,避免删除临时HTML文件
|
|
311
|
+
},
|
|
312
|
+
define: {}
|
|
313
|
+
};
|
|
314
|
+
let config = baseConfig;
|
|
315
|
+
if (strategyConfig) {
|
|
316
|
+
config = (0, import_vite.mergeConfig)(baseConfig, strategyConfig);
|
|
317
|
+
}
|
|
318
|
+
if (Object.keys(allPageDefines).length > 0) {
|
|
319
|
+
config.define = {
|
|
320
|
+
...config.define,
|
|
321
|
+
...allPageDefines
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
if (!config.build)
|
|
325
|
+
config.build = {};
|
|
326
|
+
if (!config.build.rollupOptions)
|
|
327
|
+
config.build.rollupOptions = {};
|
|
328
|
+
config.build.rollupOptions.input = htmlInputs;
|
|
329
|
+
config.build.emptyOutDir = false;
|
|
330
|
+
log(`\u7B56\u7565 "${strategyName}" - ${pages.length} \u4E2A\u9875\u9762`);
|
|
331
|
+
return config;
|
|
332
|
+
}
|
|
333
|
+
function getViteOutputDirectory(viteBuildArgs = []) {
|
|
334
|
+
const outDirIndex = viteBuildArgs.findIndex((arg) => arg === "--outDir");
|
|
335
|
+
if (outDirIndex !== -1 && outDirIndex + 1 < viteBuildArgs.length) {
|
|
336
|
+
const outDir = viteBuildArgs[outDirIndex + 1];
|
|
337
|
+
return path2.resolve(process.cwd(), outDir);
|
|
338
|
+
}
|
|
339
|
+
const outDirArg = viteBuildArgs.find((arg) => arg.startsWith("--outDir="));
|
|
340
|
+
if (outDirArg) {
|
|
341
|
+
const outDir = outDirArg.split("=")[1];
|
|
342
|
+
return path2.resolve(process.cwd(), outDir);
|
|
343
|
+
}
|
|
344
|
+
return path2.resolve(process.cwd(), "dist");
|
|
345
|
+
}
|
|
346
|
+
function cleanViteOutputDirectory(viteBuildArgs = []) {
|
|
347
|
+
const outputDir = getViteOutputDirectory(viteBuildArgs);
|
|
348
|
+
const log = createLogger(true);
|
|
349
|
+
try {
|
|
350
|
+
if (fs.existsSync(outputDir)) {
|
|
351
|
+
fs.rmSync(outputDir, { recursive: true, force: true });
|
|
352
|
+
log(`\u{1F9F9} \u6E05\u7406\u8F93\u51FA\u76EE\u5F55: ${path2.relative(process.cwd(), outputDir)}`);
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
log(`\u26A0\uFE0F \u6E05\u7406\u8F93\u51FA\u76EE\u5F55\u5931\u8D25: ${outputDir}`, error);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
function getAvailableStrategies(options) {
|
|
359
|
+
const { entry = "src/pages/*/main.{ts,js}", exclude = [], pageConfigs = {} } = options;
|
|
360
|
+
const log = createLogger(false);
|
|
361
|
+
const strategySet = /* @__PURE__ */ new Set();
|
|
362
|
+
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
363
|
+
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
364
|
+
if (entryFiles.length === 0) {
|
|
365
|
+
throw new Error(`\u672A\u627E\u5230\u5339\u914D\u7684\u5165\u53E3\u6587\u4EF6: ${entry}`);
|
|
366
|
+
}
|
|
367
|
+
try {
|
|
368
|
+
for (const entryFile of entryFiles) {
|
|
369
|
+
const pageContext = {
|
|
370
|
+
pageName: entryFile.name,
|
|
371
|
+
filePath: entryFile.file,
|
|
372
|
+
relativePath: path2.relative(process.cwd(), entryFile.file)
|
|
373
|
+
};
|
|
374
|
+
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
375
|
+
const strategyName = (pageConfig == null ? void 0 : pageConfig.strategy) || "default";
|
|
376
|
+
strategySet.add(strategyName);
|
|
377
|
+
}
|
|
378
|
+
return Array.from(strategySet).sort();
|
|
379
|
+
} catch (error) {
|
|
380
|
+
log("\u83B7\u53D6\u53EF\u7528\u7B56\u7565\u5931\u8D25:", error);
|
|
381
|
+
return ["default"];
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
var import_vite, import_glob, path2, fs;
|
|
385
|
+
var init_build_config = __esm({
|
|
386
|
+
"src/build-config.ts"() {
|
|
387
|
+
"use strict";
|
|
388
|
+
init_cjs_shims();
|
|
389
|
+
import_vite = require("vite");
|
|
390
|
+
import_glob = require("glob");
|
|
391
|
+
path2 = __toESM(require("path"));
|
|
392
|
+
fs = __toESM(require("fs"));
|
|
393
|
+
init_file_filter();
|
|
394
|
+
init_page_config();
|
|
395
|
+
init_utils();
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
|
|
41
399
|
// src/config-loader.ts
|
|
42
400
|
var config_loader_exports = {};
|
|
43
401
|
__export(config_loader_exports, {
|
|
@@ -201,134 +559,7 @@ var import_node_child_process = require("child_process");
|
|
|
201
559
|
var fs3 = __toESM(require("fs"));
|
|
202
560
|
var path4 = __toESM(require("path"));
|
|
203
561
|
var glob2 = __toESM(require("glob"));
|
|
204
|
-
|
|
205
|
-
// src/build-config.ts
|
|
206
|
-
init_cjs_shims();
|
|
207
|
-
var import_vite = require("vite");
|
|
208
|
-
var import_glob = require("glob");
|
|
209
|
-
var path2 = __toESM(require("path"));
|
|
210
|
-
var fs = __toESM(require("fs"));
|
|
211
|
-
|
|
212
|
-
// src/file-filter.ts
|
|
213
|
-
init_cjs_shims();
|
|
214
|
-
var path = __toESM(require("path"));
|
|
215
|
-
function filterEntryFiles(files, entry, exclude, _log) {
|
|
216
|
-
const result = [];
|
|
217
|
-
const nameToFile = /* @__PURE__ */ new Map();
|
|
218
|
-
let basePattern = entry.replace(/\/\*.*$/, "");
|
|
219
|
-
if (!basePattern || basePattern === entry) {
|
|
220
|
-
basePattern = path.dirname(entry.split("*")[0]);
|
|
221
|
-
}
|
|
222
|
-
const candidateFiles = [];
|
|
223
|
-
for (const file of files) {
|
|
224
|
-
if (exclude.includes(file)) {
|
|
225
|
-
continue;
|
|
226
|
-
}
|
|
227
|
-
const relativePath = path.relative(basePattern, file);
|
|
228
|
-
const pathParts = relativePath.split(path.sep);
|
|
229
|
-
if (pathParts.length === 1) {
|
|
230
|
-
const fileName = pathParts[0];
|
|
231
|
-
const name = path.basename(fileName, path.extname(fileName));
|
|
232
|
-
candidateFiles.push({ name, file, priority: 1 });
|
|
233
|
-
} else if (pathParts.length >= 2) {
|
|
234
|
-
const fileName = path.basename(file, path.extname(file));
|
|
235
|
-
const dirName = pathParts[0];
|
|
236
|
-
if (fileName === "main") {
|
|
237
|
-
candidateFiles.push({ name: dirName, file, priority: 2 });
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
for (const candidate of candidateFiles) {
|
|
242
|
-
const existing = nameToFile.get(candidate.name);
|
|
243
|
-
if (!existing) {
|
|
244
|
-
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
245
|
-
} else {
|
|
246
|
-
if (candidate.priority > existing.priority) {
|
|
247
|
-
nameToFile.set(candidate.name, { file: candidate.file, priority: candidate.priority });
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
for (const [name, { file }] of nameToFile.entries()) {
|
|
252
|
-
result.push({ name, file });
|
|
253
|
-
}
|
|
254
|
-
return result;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// src/page-config.ts
|
|
258
|
-
init_cjs_shims();
|
|
259
|
-
function getPageConfig(pageConfigs, context, log) {
|
|
260
|
-
if (!pageConfigs)
|
|
261
|
-
return null;
|
|
262
|
-
if (typeof pageConfigs === "function") {
|
|
263
|
-
const result = pageConfigs(context);
|
|
264
|
-
if (result) {
|
|
265
|
-
}
|
|
266
|
-
return result;
|
|
267
|
-
}
|
|
268
|
-
for (const [key, config] of Object.entries(pageConfigs)) {
|
|
269
|
-
if (key === context.pageName) {
|
|
270
|
-
log(`\u7CBE\u786E\u5339\u914D\u9875\u9762 ${context.pageName}:`, config);
|
|
271
|
-
return config;
|
|
272
|
-
}
|
|
273
|
-
if (config.match) {
|
|
274
|
-
const patterns = Array.isArray(config.match) ? config.match : [config.match];
|
|
275
|
-
const isMatched = patterns.some(
|
|
276
|
-
(pattern) => simpleMatch(pattern, context.pageName) || simpleMatch(pattern, context.relativePath) || simpleMatch(pattern, context.filePath)
|
|
277
|
-
);
|
|
278
|
-
if (isMatched) {
|
|
279
|
-
log(`\u6A21\u5F0F\u5339\u914D\u9875\u9762 ${context.pageName} (\u6A21\u5F0F: ${config.match}):`, config);
|
|
280
|
-
return { ...config, match: void 0 };
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
if (simpleMatch(key, context.pageName)) {
|
|
284
|
-
log(`Glob\u5339\u914D\u9875\u9762 ${context.pageName} (\u6A21\u5F0F: ${key}):`, config);
|
|
285
|
-
return config;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
return null;
|
|
289
|
-
}
|
|
290
|
-
function simpleMatch(pattern, text) {
|
|
291
|
-
const regexPattern = pattern.replace(/\*\*/g, "__DOUBLE_STAR__").replace(/\*/g, "[^/]*").replace(/__DOUBLE_STAR__/g, ".*");
|
|
292
|
-
const regex = new RegExp(`^${regexPattern}$`);
|
|
293
|
-
return regex.test(text);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// src/utils.ts
|
|
297
|
-
init_cjs_shims();
|
|
298
|
-
function createLogger(debug) {
|
|
299
|
-
return (...args) => {
|
|
300
|
-
if (debug) {
|
|
301
|
-
console.log("[vite-plugin-multi-page]", ...args);
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// src/build-config.ts
|
|
307
|
-
function getAvailableStrategies(options) {
|
|
308
|
-
const { entry = "src/pages/*/main.{ts,js}", exclude = [], pageConfigs = {} } = options;
|
|
309
|
-
const log = createLogger(false);
|
|
310
|
-
const strategySet = /* @__PURE__ */ new Set();
|
|
311
|
-
try {
|
|
312
|
-
const allFiles = import_glob.glob.sync(entry, { cwd: process.cwd() });
|
|
313
|
-
const entryFiles = filterEntryFiles(allFiles, entry, exclude, log);
|
|
314
|
-
for (const entryFile of entryFiles) {
|
|
315
|
-
const pageContext = {
|
|
316
|
-
pageName: entryFile.name,
|
|
317
|
-
filePath: entryFile.file,
|
|
318
|
-
relativePath: path2.relative(process.cwd(), entryFile.file)
|
|
319
|
-
};
|
|
320
|
-
const pageConfig = getPageConfig(pageConfigs, pageContext, log);
|
|
321
|
-
const strategyName = (pageConfig == null ? void 0 : pageConfig.strategy) || "default";
|
|
322
|
-
strategySet.add(strategyName);
|
|
323
|
-
}
|
|
324
|
-
return Array.from(strategySet).sort();
|
|
325
|
-
} catch (error) {
|
|
326
|
-
log("\u83B7\u53D6\u53EF\u7528\u7B56\u7565\u5931\u8D25:", error);
|
|
327
|
-
return ["default"];
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// src/cli.ts
|
|
562
|
+
init_build_config();
|
|
332
563
|
function parseArgs() {
|
|
333
564
|
const args = process.argv.slice(2);
|
|
334
565
|
const viteBuildArgs = [];
|
|
@@ -352,11 +583,10 @@ function parseArgs() {
|
|
|
352
583
|
\u5176\u4ED6\u6240\u6709\u53C2\u6570\u5C06\u4F20\u9012\u7ED9 vite build \u547D\u4EE4
|
|
353
584
|
|
|
354
585
|
\u793A\u4F8B:
|
|
355
|
-
vite-
|
|
356
|
-
vite-
|
|
357
|
-
vite-
|
|
358
|
-
vite-
|
|
359
|
-
vite-multi-page-build --mode production --debug
|
|
586
|
+
vite-mp
|
|
587
|
+
vite-mp --debug
|
|
588
|
+
vite-mp --cwd example
|
|
589
|
+
vite-mp --mode production --debug
|
|
360
590
|
`);
|
|
361
591
|
process.exit(0);
|
|
362
592
|
} else if (arg !== "build") {
|
|
@@ -406,19 +636,18 @@ function buildStrategy(strategy, viteBuildArgs, debug) {
|
|
|
406
636
|
}
|
|
407
637
|
child.on("close", (code) => {
|
|
408
638
|
const success = code === 0;
|
|
409
|
-
const
|
|
639
|
+
const actualOutputDir = getViteOutputDirectory(viteBuildArgs);
|
|
410
640
|
if (success) {
|
|
411
641
|
log(`\u2705 \u7B56\u7565 ${strategy} \u6784\u5EFA\u6210\u529F`);
|
|
412
642
|
try {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
const files = fs3.readdirSync(outputPath);
|
|
643
|
+
if (fs3.existsSync(actualOutputDir)) {
|
|
644
|
+
const files = fs3.readdirSync(actualOutputDir);
|
|
416
645
|
for (const file of files) {
|
|
417
646
|
if (file.startsWith(".temp.mp.") && file.endsWith(".html")) {
|
|
418
|
-
const oldPath = path4.resolve(
|
|
647
|
+
const oldPath = path4.resolve(actualOutputDir, file);
|
|
419
648
|
const name = file.replace(/^\.temp\.mp\./, "").replace(/\.html$/, "");
|
|
420
649
|
const newName = `${name}.html`;
|
|
421
|
-
const newPath = path4.resolve(
|
|
650
|
+
const newPath = path4.resolve(actualOutputDir, newName);
|
|
422
651
|
fs3.renameSync(oldPath, newPath);
|
|
423
652
|
log(`\u91CD\u547D\u540DHTML: ${file} -> ${newName}`);
|
|
424
653
|
}
|
|
@@ -437,16 +666,17 @@ function buildStrategy(strategy, viteBuildArgs, debug) {
|
|
|
437
666
|
strategy,
|
|
438
667
|
success,
|
|
439
668
|
error: success ? void 0 : errorOutput || `\u6784\u5EFA\u5931\u8D25\uFF0C\u9000\u51FA\u7801: ${code}`,
|
|
440
|
-
outputDir
|
|
669
|
+
outputDir: actualOutputDir
|
|
441
670
|
});
|
|
442
671
|
});
|
|
443
672
|
child.on("error", (error) => {
|
|
444
673
|
log(`\u274C \u7B56\u7565 ${strategy} \u6784\u5EFA\u51FA\u9519:`, error.message);
|
|
674
|
+
const actualOutputDir = getViteOutputDirectory(viteBuildArgs);
|
|
445
675
|
resolve4({
|
|
446
676
|
strategy,
|
|
447
677
|
success: false,
|
|
448
678
|
error: error.message,
|
|
449
|
-
outputDir:
|
|
679
|
+
outputDir: actualOutputDir
|
|
450
680
|
});
|
|
451
681
|
});
|
|
452
682
|
});
|
|
@@ -617,7 +847,8 @@ async function main() {
|
|
|
617
847
|
log("\u{1F680} \u5F00\u59CB\u591A\u7B56\u7565\u6784\u5EFA...");
|
|
618
848
|
log("\u{1F4CB} \u52A0\u8F7D\u914D\u7F6E...");
|
|
619
849
|
const options = await loadViteConfig();
|
|
620
|
-
const
|
|
850
|
+
const { getAvailableStrategies: getAvailableStrategies2 } = await Promise.resolve().then(() => (init_build_config(), build_config_exports));
|
|
851
|
+
const strategies = getAvailableStrategies2({
|
|
621
852
|
entry: options.entry || "src/pages/*/main.{ts,js}",
|
|
622
853
|
exclude: options.exclude || [],
|
|
623
854
|
template: options.template || "index.html",
|
|
@@ -629,6 +860,9 @@ async function main() {
|
|
|
629
860
|
throw new Error("\u672A\u627E\u5230\u4EFB\u4F55\u6784\u5EFA\u7B56\u7565");
|
|
630
861
|
}
|
|
631
862
|
log(`\u53D1\u73B0 ${strategies.length} \u4E2A\u7B56\u7565: ${strategies.join(", ")}`);
|
|
863
|
+
log("\u{1F9F9} \u6E05\u7406\u8F93\u51FA\u76EE\u5F55...");
|
|
864
|
+
const { cleanViteOutputDirectory: cleanViteOutputDirectory2 } = await Promise.resolve().then(() => (init_build_config(), build_config_exports));
|
|
865
|
+
cleanViteOutputDirectory2(viteBuildArgs);
|
|
632
866
|
log("\u{1F528} \u5F00\u59CB\u5E76\u884C\u6784\u5EFA...");
|
|
633
867
|
const buildPromises = strategies.map((strategy) => buildStrategy(strategy, viteBuildArgs, debug));
|
|
634
868
|
const results = await Promise.all(buildPromises);
|
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
|
*/
|
|
@@ -65,4 +74,4 @@ declare function mergeWithDefaults(userConfig: MultiPageOptions | null | undefin
|
|
|
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
|
*/
|
|
@@ -65,4 +74,4 @@ declare function mergeWithDefaults(userConfig: MultiPageOptions | null | undefin
|
|
|
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 };
|