vite-plugin-kiru 0.29.7 → 0.29.8
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.js +31 -28
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2173,17 +2173,17 @@ import path2 from "node:path";
|
|
|
2173
2173
|
|
|
2174
2174
|
// src/utils.ts
|
|
2175
2175
|
import path from "node:path";
|
|
2176
|
-
import fs2 from "node:fs";
|
|
2177
2176
|
function createLogger(state) {
|
|
2178
2177
|
return (...data) => {
|
|
2179
2178
|
if (!state.loggingEnabled) return;
|
|
2180
2179
|
console.log(ANSI.cyan("[vite-plugin-kiru]"), ...data);
|
|
2181
2180
|
};
|
|
2182
2181
|
}
|
|
2183
|
-
function resolveUserDocument(projectRoot, ssgOptions) {
|
|
2182
|
+
async function resolveUserDocument(projectRoot, ssgOptions) {
|
|
2184
2183
|
const { dir, document } = ssgOptions;
|
|
2185
2184
|
const fp = path.resolve(projectRoot, dir, document);
|
|
2186
|
-
const
|
|
2185
|
+
const { globSync } = await import("node:fs");
|
|
2186
|
+
const res = globSync(fp);
|
|
2187
2187
|
if (res.length) return res[0].replace(/\\/g, "/");
|
|
2188
2188
|
throw new Error(`Document not found at ${fp}`);
|
|
2189
2189
|
}
|
|
@@ -2213,7 +2213,8 @@ function shouldTransformFile(id, state) {
|
|
|
2213
2213
|
var VIRTUAL_ROUTES_ID = "virtual:kiru:routes";
|
|
2214
2214
|
var VIRTUAL_ENTRY_SERVER_ID = "virtual:kiru:entry-server";
|
|
2215
2215
|
var VIRTUAL_ENTRY_CLIENT_ID = "virtual:kiru:entry-client";
|
|
2216
|
-
function createVirtualModules(projectRoot, ssgOptions) {
|
|
2216
|
+
async function createVirtualModules(projectRoot, ssgOptions) {
|
|
2217
|
+
const userDoc = await resolveUserDocument(projectRoot, ssgOptions);
|
|
2217
2218
|
function createRoutesModule() {
|
|
2218
2219
|
const { dir, baseUrl, page, layout, transition } = ssgOptions;
|
|
2219
2220
|
return `
|
|
@@ -2231,7 +2232,6 @@ export { dir, baseUrl, pages, layouts, transition }
|
|
|
2231
2232
|
`;
|
|
2232
2233
|
}
|
|
2233
2234
|
function createEntryServerModule() {
|
|
2234
|
-
const userDoc = resolveUserDocument(projectRoot, ssgOptions);
|
|
2235
2235
|
return `
|
|
2236
2236
|
import {
|
|
2237
2237
|
render as kiruServerRender,
|
|
@@ -2254,7 +2254,7 @@ export async function generateStaticPaths() {
|
|
|
2254
2254
|
return `
|
|
2255
2255
|
import { initClient } from "kiru/router/client"
|
|
2256
2256
|
import { dir, baseUrl, pages, layouts, transition } from "${VIRTUAL_ROUTES_ID}"
|
|
2257
|
-
import "${
|
|
2257
|
+
import "${userDoc}"
|
|
2258
2258
|
|
|
2259
2259
|
initClient({ dir, baseUrl, pages, layouts, transition })
|
|
2260
2260
|
`;
|
|
@@ -3287,7 +3287,9 @@ async function handleSSR(server, url, projectRoot, resolveUserDocument2) {
|
|
|
3287
3287
|
VIRTUAL_ENTRY_SERVER_ID
|
|
3288
3288
|
);
|
|
3289
3289
|
const moduleIds = [];
|
|
3290
|
-
const documentModule = resolveUserDocument2().substring(
|
|
3290
|
+
const documentModule = (await resolveUserDocument2()).substring(
|
|
3291
|
+
projectRoot.length
|
|
3292
|
+
);
|
|
3291
3293
|
moduleIds.push(documentModule);
|
|
3292
3294
|
const ctx = {
|
|
3293
3295
|
registerModule: (moduleId) => {
|
|
@@ -3339,7 +3341,7 @@ async function handleSSR(server, url, projectRoot, resolveUserDocument2) {
|
|
|
3339
3341
|
|
|
3340
3342
|
// src/preview-server.ts
|
|
3341
3343
|
import { resolve } from "node:path";
|
|
3342
|
-
import
|
|
3344
|
+
import fs2 from "node:fs";
|
|
3343
3345
|
import path4 from "node:path";
|
|
3344
3346
|
|
|
3345
3347
|
// ../../node_modules/.pnpm/mime@4.1.0/node_modules/mime/dist/types/other.js
|
|
@@ -4538,11 +4540,11 @@ function createPreviewMiddleware(projectRoot, baseOutDir) {
|
|
|
4538
4540
|
}
|
|
4539
4541
|
if (!path4.extname(filePath) && !url.endsWith("/")) {
|
|
4540
4542
|
const htmlCandidate = filePath + ".html";
|
|
4541
|
-
if (
|
|
4543
|
+
if (fs2.existsSync(htmlCandidate)) {
|
|
4542
4544
|
filePath = htmlCandidate;
|
|
4543
4545
|
}
|
|
4544
4546
|
}
|
|
4545
|
-
if (!
|
|
4547
|
+
if (!fs2.existsSync(filePath)) {
|
|
4546
4548
|
const normalizedUrl = url.replace(/\/$/, "") || "/";
|
|
4547
4549
|
const urlSegments = normalizedUrl.split("/").filter(Boolean);
|
|
4548
4550
|
let found404 = false;
|
|
@@ -4553,7 +4555,7 @@ function createPreviewMiddleware(projectRoot, baseOutDir) {
|
|
|
4553
4555
|
clientOutDir,
|
|
4554
4556
|
fourOhFourPath + ".html"
|
|
4555
4557
|
);
|
|
4556
|
-
if (
|
|
4558
|
+
if (fs2.existsSync(fourOhFourFilePath)) {
|
|
4557
4559
|
filePath = fourOhFourFilePath;
|
|
4558
4560
|
found404 = true;
|
|
4559
4561
|
break;
|
|
@@ -4566,7 +4568,7 @@ function createPreviewMiddleware(projectRoot, baseOutDir) {
|
|
|
4566
4568
|
}
|
|
4567
4569
|
}
|
|
4568
4570
|
const type = src_default.getType(filePath) ?? "application/octet-stream";
|
|
4569
|
-
const content =
|
|
4571
|
+
const content = fs2.readFileSync(filePath);
|
|
4570
4572
|
const is404Page = filePath.includes("/404.html");
|
|
4571
4573
|
res.statusCode = is404Page ? 404 : 200;
|
|
4572
4574
|
res.setHeader("Content-Type", type);
|
|
@@ -4579,7 +4581,7 @@ function createPreviewMiddleware(projectRoot, baseOutDir) {
|
|
|
4579
4581
|
|
|
4580
4582
|
// src/ssg.ts
|
|
4581
4583
|
import path5 from "node:path";
|
|
4582
|
-
import
|
|
4584
|
+
import fs3 from "node:fs";
|
|
4583
4585
|
import { pathToFileURL } from "node:url";
|
|
4584
4586
|
async function generateStaticSite(state, outputOptions, bundle, log) {
|
|
4585
4587
|
const { projectRoot, baseOutDir, manifestPath } = state;
|
|
@@ -4595,9 +4597,9 @@ async function generateStaticSite(state, outputOptions, bundle, log) {
|
|
|
4595
4597
|
const { clientEntry } = await getClientAssets(clientOutDirAbs, manifestPath);
|
|
4596
4598
|
let manifest = null;
|
|
4597
4599
|
const clientManifestPath = path5.resolve(clientOutDirAbs, manifestPath);
|
|
4598
|
-
if (
|
|
4600
|
+
if (fs3.existsSync(clientManifestPath)) {
|
|
4599
4601
|
try {
|
|
4600
|
-
manifest = JSON.parse(
|
|
4602
|
+
manifest = JSON.parse(fs3.readFileSync(clientManifestPath, "utf-8"));
|
|
4601
4603
|
} catch {
|
|
4602
4604
|
}
|
|
4603
4605
|
}
|
|
@@ -4628,8 +4630,8 @@ async function generateStaticSite(state, outputOptions, bundle, log) {
|
|
|
4628
4630
|
);
|
|
4629
4631
|
const filePath = getOutputPath(clientOutDirAbs, route);
|
|
4630
4632
|
log(ANSI.cyan("[SSG]"), "write:", ANSI.black(filePath));
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
+
fs3.mkdirSync(path5.dirname(filePath), { recursive: true });
|
|
4634
|
+
fs3.writeFileSync(filePath, html, "utf-8");
|
|
4633
4635
|
})
|
|
4634
4636
|
);
|
|
4635
4637
|
}
|
|
@@ -4639,8 +4641,8 @@ async function getClientAssets(clientOutDirAbs, manifestPath) {
|
|
|
4639
4641
|
let clientEntry = null;
|
|
4640
4642
|
try {
|
|
4641
4643
|
const clientManifestPath = path5.resolve(clientOutDirAbs, manifestPath);
|
|
4642
|
-
if (
|
|
4643
|
-
const manifest = JSON.parse(
|
|
4644
|
+
if (fs3.existsSync(clientManifestPath)) {
|
|
4645
|
+
const manifest = JSON.parse(fs3.readFileSync(clientManifestPath, "utf-8"));
|
|
4644
4646
|
const clientEntryKey = "virtual:kiru:entry-client";
|
|
4645
4647
|
if (manifest[clientEntryKey]?.file) {
|
|
4646
4648
|
clientEntry = manifest[clientEntryKey].file;
|
|
@@ -4708,13 +4710,13 @@ function collectCssForModules(manifest, moduleIds, projectRoot) {
|
|
|
4708
4710
|
return "";
|
|
4709
4711
|
}
|
|
4710
4712
|
function findClientEntry(dir) {
|
|
4711
|
-
if (!
|
|
4712
|
-
const top =
|
|
4713
|
+
if (!fs3.existsSync(dir)) return null;
|
|
4714
|
+
const top = fs3.readdirSync(dir);
|
|
4713
4715
|
const topJs = top.find((f) => f.endsWith(".js"));
|
|
4714
4716
|
if (topJs) return topJs;
|
|
4715
4717
|
const assetsDir = path5.join(dir, "assets");
|
|
4716
|
-
if (
|
|
4717
|
-
const assetJs =
|
|
4718
|
+
if (fs3.existsSync(assetsDir)) {
|
|
4719
|
+
const assetJs = fs3.readdirSync(assetsDir).find((f) => f.endsWith(".js"));
|
|
4718
4720
|
return assetJs ? `assets/${assetJs}` : null;
|
|
4719
4721
|
}
|
|
4720
4722
|
return null;
|
|
@@ -4772,7 +4774,7 @@ async function appendStaticPropsToClientModules(state, clientOutDirAbs, log) {
|
|
|
4772
4774
|
try {
|
|
4773
4775
|
log(ANSI.cyan("[SSG]"), "Starting static props collection...");
|
|
4774
4776
|
const clientManifestPath = path5.resolve(clientOutDirAbs, manifestPath);
|
|
4775
|
-
if (!
|
|
4777
|
+
if (!fs3.existsSync(clientManifestPath)) {
|
|
4776
4778
|
log(
|
|
4777
4779
|
ANSI.yellow("[SSG]"),
|
|
4778
4780
|
"Client manifest not found, skipping static props"
|
|
@@ -4780,11 +4782,12 @@ async function appendStaticPropsToClientModules(state, clientOutDirAbs, log) {
|
|
|
4780
4782
|
return;
|
|
4781
4783
|
}
|
|
4782
4784
|
log(ANSI.cyan("[SSG]"), "Found client manifest at:", clientManifestPath);
|
|
4785
|
+
const { globSync } = await import("node:fs");
|
|
4783
4786
|
const srcPages = globSync(`${ssgOptions.dir}/**/${ssgOptions.page}`, {
|
|
4784
4787
|
cwd: projectRoot
|
|
4785
4788
|
}).map((s) => s.replace(/\\/g, "/"));
|
|
4786
4789
|
const manifest = JSON.parse(
|
|
4787
|
-
|
|
4790
|
+
fs3.readFileSync(clientManifestPath, "utf-8")
|
|
4788
4791
|
);
|
|
4789
4792
|
log(
|
|
4790
4793
|
ANSI.cyan("[SSG]"),
|
|
@@ -4809,7 +4812,7 @@ async function appendStaticPropsToClientModules(state, clientOutDirAbs, log) {
|
|
|
4809
4812
|
const code = `export const __KIRU_STATIC_PROPS__ = ${JSON.stringify(
|
|
4810
4813
|
staticProps
|
|
4811
4814
|
)};`;
|
|
4812
|
-
|
|
4815
|
+
fs3.appendFileSync(filePath, `
|
|
4813
4816
|
${code}`, "utf-8");
|
|
4814
4817
|
log(ANSI.cyan("[SSG]"), "Added static props to:", chunk.file);
|
|
4815
4818
|
})
|
|
@@ -4832,12 +4835,12 @@ function kiru(opts = {}) {
|
|
|
4832
4835
|
inlineConfig = config;
|
|
4833
4836
|
return createViteConfig(config, opts);
|
|
4834
4837
|
},
|
|
4835
|
-
configResolved(config) {
|
|
4838
|
+
async configResolved(config) {
|
|
4836
4839
|
const initialState = createPluginState(opts);
|
|
4837
4840
|
state = updatePluginState(initialState, config, opts);
|
|
4838
4841
|
log = createLogger(state);
|
|
4839
4842
|
if (state.ssgOptions) {
|
|
4840
|
-
virtualModules = createVirtualModules(
|
|
4843
|
+
virtualModules = await createVirtualModules(
|
|
4841
4844
|
state.projectRoot,
|
|
4842
4845
|
state.ssgOptions
|
|
4843
4846
|
);
|