@md-plugins/vite-ssg-plugin 0.1.0-beta.26 → 0.1.0-beta.27
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/index.mjs +40 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { globSync } from 'tinyglobby';
|
|
2
2
|
import { Buffer } from 'node:buffer';
|
|
3
|
-
import { readFile, mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { readFile, mkdir, writeFile, readdir } from 'node:fs/promises';
|
|
4
4
|
import { resolve, join, dirname } from 'node:path';
|
|
5
5
|
|
|
6
6
|
function escapeJsonForHtml(json) {
|
|
@@ -158,6 +158,40 @@ async function readSsgRouteManifest(outDir, manifestFile) {
|
|
|
158
158
|
const manifestJson = await readFile(join(outDir, manifestFile), "utf8");
|
|
159
159
|
return JSON.parse(manifestJson);
|
|
160
160
|
}
|
|
161
|
+
function joinAssetHref(base, file) {
|
|
162
|
+
if (base === "./") {
|
|
163
|
+
return `./${file}`;
|
|
164
|
+
}
|
|
165
|
+
if (base.startsWith("http://") || base.startsWith("https://")) {
|
|
166
|
+
return `${base.replace(/\/$/, "")}/${file}`;
|
|
167
|
+
}
|
|
168
|
+
return `${base === "/" ? "" : base}/${file}`;
|
|
169
|
+
}
|
|
170
|
+
async function collectCssFiles(outDir, dir = "assets") {
|
|
171
|
+
const entries = await readdir(join(outDir, dir), {
|
|
172
|
+
withFileTypes: true
|
|
173
|
+
}).catch(() => []);
|
|
174
|
+
const files = [];
|
|
175
|
+
for (const entry of entries) {
|
|
176
|
+
const path = `${dir}/${entry.name}`;
|
|
177
|
+
if (entry.isDirectory()) {
|
|
178
|
+
files.push(...await collectCssFiles(outDir, path));
|
|
179
|
+
} else if (entry.isFile() && path.endsWith(".css")) {
|
|
180
|
+
files.push(path);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return files.sort();
|
|
184
|
+
}
|
|
185
|
+
async function injectMissingCssAssets(appHtml, outDir, base) {
|
|
186
|
+
const cssFiles = await collectCssFiles(outDir);
|
|
187
|
+
const missingLinks = cssFiles.map((file) => joinAssetHref(base, file)).filter((href) => !appHtml.includes(`href="${href}"`) && !appHtml.includes(`href=${href}`)).map((href) => `<link rel="stylesheet" crossorigin href="${href}">`);
|
|
188
|
+
if (missingLinks.length === 0) {
|
|
189
|
+
return appHtml;
|
|
190
|
+
}
|
|
191
|
+
const content = `${missingLinks.join("\n")}
|
|
192
|
+
`;
|
|
193
|
+
return appHtml.includes("</head>") ? appHtml.replace("</head>", `${content}</head>`) : `${content}${appHtml}`;
|
|
194
|
+
}
|
|
161
195
|
async function prerenderSsgRoutes({
|
|
162
196
|
outDir,
|
|
163
197
|
appHtmlFile = "index.html",
|
|
@@ -169,7 +203,11 @@ async function prerenderSsgRoutes({
|
|
|
169
203
|
}) {
|
|
170
204
|
const resolvedOutDir = resolve(outDir);
|
|
171
205
|
const resolvedManifest = manifest ?? await readSsgRouteManifest(resolvedOutDir, manifestFile);
|
|
172
|
-
const appHtml = await
|
|
206
|
+
const appHtml = await injectMissingCssAssets(
|
|
207
|
+
await readFile(join(resolvedOutDir, appHtmlFile), "utf8"),
|
|
208
|
+
resolvedOutDir,
|
|
209
|
+
resolvedManifest.base
|
|
210
|
+
);
|
|
173
211
|
const routes = [];
|
|
174
212
|
for (const [routeIndex, route] of resolvedManifest.routes.entries()) {
|
|
175
213
|
const html = await renderSsgRouteHtml(
|