vite-plugin-kiru 0.31.0 → 0.32.0-preview.1
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/build.dev.ts +4 -3
- package/dist/index.d.ts +63 -0
- package/dist/index.js +233 -89
- package/dist/server.d.ts +2 -3
- package/dist/server.js +138 -73
- package/package.json +14 -3
package/build.dev.ts
CHANGED
|
@@ -3,13 +3,13 @@ import fs from "node:fs"
|
|
|
3
3
|
|
|
4
4
|
await esbuild
|
|
5
5
|
.context({
|
|
6
|
-
entryPoints: ["src/index.ts"],
|
|
6
|
+
entryPoints: ["src/index.ts", "src/server.ts"],
|
|
7
7
|
bundle: true,
|
|
8
8
|
platform: "node",
|
|
9
9
|
target: "esnext",
|
|
10
10
|
format: "esm",
|
|
11
|
-
|
|
12
|
-
external: ["kiru", "vite"],
|
|
11
|
+
outdir: "./dist",
|
|
12
|
+
external: ["kiru", "vite", "virtual:kiru:entry-server"],
|
|
13
13
|
write: true,
|
|
14
14
|
plugins: [
|
|
15
15
|
{
|
|
@@ -18,6 +18,7 @@ await esbuild
|
|
|
18
18
|
onEnd(() => {
|
|
19
19
|
console.log("[vite-plugin-kiru]: Build complete!")
|
|
20
20
|
fs.copyFileSync("./src/types.d.ts", "dist/index.d.ts")
|
|
21
|
+
fs.copyFileSync("./src/types.server.d.ts", "dist/server.d.ts")
|
|
21
22
|
})
|
|
22
23
|
},
|
|
23
24
|
},
|
package/dist/index.d.ts
CHANGED
|
@@ -116,6 +116,11 @@ export interface SSGOptions {
|
|
|
116
116
|
* @default "layout.{tsx,jsx}"
|
|
117
117
|
*/
|
|
118
118
|
layout?: string
|
|
119
|
+
/**
|
|
120
|
+
* The filename of guard files to search for
|
|
121
|
+
* @default "guard.{ts,js}"
|
|
122
|
+
*/
|
|
123
|
+
guard?: string
|
|
119
124
|
/**
|
|
120
125
|
* Enable transitions for all routes and loading states
|
|
121
126
|
* @default false
|
|
@@ -133,6 +138,49 @@ export interface SSGOptions {
|
|
|
133
138
|
build?: SSGBuildOptions
|
|
134
139
|
}
|
|
135
140
|
|
|
141
|
+
export interface SSROptions {
|
|
142
|
+
/**
|
|
143
|
+
* The path to the server entry file
|
|
144
|
+
* @example "src/server.ts"
|
|
145
|
+
*/
|
|
146
|
+
runtimeEntry: string
|
|
147
|
+
/**
|
|
148
|
+
* The base URL of the app
|
|
149
|
+
* @default "/"
|
|
150
|
+
*/
|
|
151
|
+
baseUrl?: string
|
|
152
|
+
/**
|
|
153
|
+
* The directory of the app
|
|
154
|
+
* @default "src/pages"
|
|
155
|
+
*/
|
|
156
|
+
dir?: string
|
|
157
|
+
/**
|
|
158
|
+
* The name of the document component
|
|
159
|
+
* @default "document.{tsx,jsx}"
|
|
160
|
+
*/
|
|
161
|
+
document?: string
|
|
162
|
+
/**
|
|
163
|
+
* The filename of page components to search for
|
|
164
|
+
* @default "index.{tsx,jsx}"
|
|
165
|
+
*/
|
|
166
|
+
page?: string
|
|
167
|
+
/**
|
|
168
|
+
* The filename of layout components to search for
|
|
169
|
+
* @default "layout.{tsx,jsx}"
|
|
170
|
+
*/
|
|
171
|
+
layout?: string
|
|
172
|
+
/**
|
|
173
|
+
* The filename of guard files to search for
|
|
174
|
+
* @default "guard.{ts,js}"
|
|
175
|
+
*/
|
|
176
|
+
guard?: string
|
|
177
|
+
/**
|
|
178
|
+
* Enable transitions for all routes and loading states
|
|
179
|
+
* @default false
|
|
180
|
+
*/
|
|
181
|
+
transition?: boolean
|
|
182
|
+
}
|
|
183
|
+
|
|
136
184
|
export interface DevtoolsOptions {
|
|
137
185
|
/**
|
|
138
186
|
* Specifies the path to the devtools app displayed via popup
|
|
@@ -193,6 +241,21 @@ export interface KiruPluginOptions {
|
|
|
193
241
|
* ```
|
|
194
242
|
*/
|
|
195
243
|
ssg?: SSGOptions | true
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Options for SSR
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* ssr: {
|
|
250
|
+
* dir: "./src/app",
|
|
251
|
+
* document: "document.{tsx,jsx}",
|
|
252
|
+
* page: "index.{tsx,jsx}",
|
|
253
|
+
* layout: "layout.{tsx,jsx}",
|
|
254
|
+
* transition: true
|
|
255
|
+
* }
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
ssr?: SSROptions
|
|
196
259
|
}
|
|
197
260
|
|
|
198
261
|
export const defaultEsBuildOptions: ESBuildOptions
|
package/dist/index.js
CHANGED
|
@@ -27,7 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
27
|
// ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.5/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.umd.js
|
|
28
28
|
var require_sourcemap_codec_umd = __commonJS({
|
|
29
29
|
"../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.5/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.umd.js"(exports, module) {
|
|
30
|
-
(function(
|
|
30
|
+
(function(global2, factory) {
|
|
31
31
|
if (typeof exports === "object" && typeof module !== "undefined") {
|
|
32
32
|
factory(module);
|
|
33
33
|
module.exports = def(module);
|
|
@@ -39,8 +39,8 @@ var require_sourcemap_codec_umd = __commonJS({
|
|
|
39
39
|
} else {
|
|
40
40
|
const mod = { exports: {} };
|
|
41
41
|
factory(mod);
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self;
|
|
43
|
+
global2.sourcemapCodec = def(mod);
|
|
44
44
|
}
|
|
45
45
|
function def(m) {
|
|
46
46
|
return "default" in m.exports ? m.exports.default : m.exports;
|
|
@@ -8808,8 +8808,8 @@ function createLogger(state) {
|
|
|
8808
8808
|
console.log(ANSI.cyan("[vite-plugin-kiru]"), ...data);
|
|
8809
8809
|
};
|
|
8810
8810
|
}
|
|
8811
|
-
function resolveUserDocument(projectRoot,
|
|
8812
|
-
const { dir, document } =
|
|
8811
|
+
function resolveUserDocument(projectRoot, options) {
|
|
8812
|
+
const { dir, document } = options;
|
|
8813
8813
|
const fp = path2.resolve(projectRoot, dir, document).replace(/\\/g, "/");
|
|
8814
8814
|
const matches = globSync(fp);
|
|
8815
8815
|
if (!matches.length) {
|
|
@@ -8840,57 +8840,80 @@ function shouldTransformFile(id, state) {
|
|
|
8840
8840
|
}
|
|
8841
8841
|
|
|
8842
8842
|
// src/virtual-modules.ts
|
|
8843
|
-
var
|
|
8843
|
+
var VIRTUAL_CONFIG_ID = "virtual:kiru:config";
|
|
8844
8844
|
var VIRTUAL_ENTRY_SERVER_ID = "virtual:kiru:entry-server";
|
|
8845
8845
|
var VIRTUAL_ENTRY_CLIENT_ID = "virtual:kiru:entry-client";
|
|
8846
|
-
async function createVirtualModules(projectRoot,
|
|
8847
|
-
const userDoc = resolveUserDocument(projectRoot,
|
|
8848
|
-
function
|
|
8849
|
-
const { dir, baseUrl, page, layout, transition } =
|
|
8846
|
+
async function createVirtualModules(projectRoot, options, mode) {
|
|
8847
|
+
const userDoc = resolveUserDocument(projectRoot, options);
|
|
8848
|
+
function createConfigModule() {
|
|
8849
|
+
const { dir, baseUrl, page, layout, guard, transition } = options;
|
|
8850
8850
|
return `
|
|
8851
8851
|
import { formatViteImportMap, normalizePrefixPath } from "kiru/router/utils"
|
|
8852
8852
|
|
|
8853
8853
|
const dir = normalizePrefixPath("${dir}")
|
|
8854
8854
|
const baseUrl = normalizePrefixPath("${baseUrl}")
|
|
8855
|
-
const
|
|
8856
|
-
const
|
|
8857
|
-
const
|
|
8858
|
-
const layouts = formatViteImportMap(layoutsMap, dir, baseUrl)
|
|
8855
|
+
const pages = formatViteImportMap(import.meta.glob(["/**/${page}"]), dir, baseUrl)
|
|
8856
|
+
const layouts = formatViteImportMap(import.meta.glob(["/**/${layout}"]), dir, baseUrl)
|
|
8857
|
+
const guards = formatViteImportMap(import.meta.glob(["/**/${guard}"]), dir, baseUrl)
|
|
8859
8858
|
const transition = ${transition}
|
|
8860
8859
|
|
|
8861
|
-
export { dir, baseUrl, pages, layouts, transition }
|
|
8860
|
+
export { dir, baseUrl, pages, layouts, guards, transition }
|
|
8862
8861
|
`;
|
|
8863
8862
|
}
|
|
8864
8863
|
function createEntryServerModule() {
|
|
8864
|
+
const documentModuleId = userDoc.substring(projectRoot.length);
|
|
8865
|
+
if (mode === "ssr") {
|
|
8866
|
+
return `
|
|
8867
|
+
import { render as kiruServerRender } from "kiru/router/ssr"
|
|
8868
|
+
import Document from "${userDoc}"
|
|
8869
|
+
import * as config from "${VIRTUAL_CONFIG_ID}"
|
|
8870
|
+
|
|
8871
|
+
export const documentModuleId = "${documentModuleId}"
|
|
8872
|
+
|
|
8873
|
+
export async function render(url, ctx) {
|
|
8874
|
+
return kiruServerRender(url, { ...ctx, ...config, Document })
|
|
8875
|
+
}
|
|
8876
|
+
`;
|
|
8877
|
+
}
|
|
8865
8878
|
return `
|
|
8866
8879
|
import {
|
|
8867
|
-
render as
|
|
8868
|
-
generateStaticPaths as
|
|
8869
|
-
} from "kiru/router/
|
|
8880
|
+
render as kiruStaticRender,
|
|
8881
|
+
generateStaticPaths as kiruGenerateStaticPaths
|
|
8882
|
+
} from "kiru/router/ssg"
|
|
8870
8883
|
import Document from "${userDoc}"
|
|
8871
|
-
import
|
|
8884
|
+
import * as config from "${VIRTUAL_CONFIG_ID}"
|
|
8885
|
+
|
|
8886
|
+
export const documentModuleId = "${documentModuleId}"
|
|
8872
8887
|
|
|
8873
8888
|
export async function render(url, ctx) {
|
|
8874
|
-
|
|
8875
|
-
return kiruServerRender(url, { registerModule, registerPreloadedPageProps, Document, pages, layouts })
|
|
8889
|
+
return kiruStaticRender(url, { ...ctx, ...config, Document })
|
|
8876
8890
|
}
|
|
8877
8891
|
|
|
8878
8892
|
export async function generateStaticPaths() {
|
|
8879
|
-
return
|
|
8893
|
+
return kiruGenerateStaticPaths(config.pages)
|
|
8880
8894
|
}
|
|
8881
8895
|
`;
|
|
8882
8896
|
}
|
|
8883
8897
|
function createEntryClientModule() {
|
|
8898
|
+
if (mode === "ssr") {
|
|
8899
|
+
return `
|
|
8900
|
+
import { initClient } from "kiru/router/client"
|
|
8901
|
+
import * as config from "${VIRTUAL_CONFIG_ID}"
|
|
8902
|
+
import "${userDoc}"
|
|
8903
|
+
|
|
8904
|
+
initClient({ ...config, hydrationMode: "dynamic" })
|
|
8905
|
+
`;
|
|
8906
|
+
}
|
|
8884
8907
|
return `
|
|
8885
8908
|
import { initClient } from "kiru/router/client"
|
|
8886
|
-
import
|
|
8909
|
+
import * as config from "${VIRTUAL_CONFIG_ID}"
|
|
8887
8910
|
import "${userDoc}"
|
|
8888
8911
|
|
|
8889
|
-
initClient({
|
|
8912
|
+
initClient({ ...config })
|
|
8890
8913
|
`;
|
|
8891
8914
|
}
|
|
8892
8915
|
return {
|
|
8893
|
-
[
|
|
8916
|
+
[VIRTUAL_CONFIG_ID]: createConfigModule,
|
|
8894
8917
|
[VIRTUAL_ENTRY_SERVER_ID]: createEntryServerModule,
|
|
8895
8918
|
[VIRTUAL_ENTRY_CLIENT_ID]: createEntryClientModule
|
|
8896
8919
|
};
|
|
@@ -8911,11 +8934,21 @@ var defaultSSGOptions = {
|
|
|
8911
8934
|
document: "document.{tsx,jsx}",
|
|
8912
8935
|
page: "index.{tsx,jsx}",
|
|
8913
8936
|
layout: "layout.{tsx,jsx}",
|
|
8937
|
+
guard: "guard.{ts,js}",
|
|
8914
8938
|
transition: false,
|
|
8915
8939
|
build: {
|
|
8916
8940
|
maxConcurrentRenders: 100
|
|
8917
8941
|
}
|
|
8918
8942
|
};
|
|
8943
|
+
var defaultSSROptions = {
|
|
8944
|
+
baseUrl: "/",
|
|
8945
|
+
dir: "src/pages",
|
|
8946
|
+
document: "document.{tsx,jsx}",
|
|
8947
|
+
page: "index.{tsx,jsx}",
|
|
8948
|
+
layout: "layout.{tsx,jsx}",
|
|
8949
|
+
guard: "guard.{ts,js}",
|
|
8950
|
+
transition: false
|
|
8951
|
+
};
|
|
8919
8952
|
function createPluginState(opts = {}) {
|
|
8920
8953
|
let fileLinkFormatter = (path8, line) => `vscode://file/${path8}:${line}`;
|
|
8921
8954
|
let dtClientPathname = "/__devtools__";
|
|
@@ -8936,47 +8969,78 @@ function createPluginState(opts = {}) {
|
|
|
8936
8969
|
dtHostScriptPath: "/__devtools_host__.js",
|
|
8937
8970
|
manifestPath: "vite-manifest.json",
|
|
8938
8971
|
loggingEnabled: opts.loggingEnabled === true,
|
|
8939
|
-
ssgOptions: null
|
|
8972
|
+
ssgOptions: null,
|
|
8973
|
+
ssrOptions: null
|
|
8940
8974
|
};
|
|
8941
|
-
|
|
8942
|
-
|
|
8943
|
-
|
|
8975
|
+
if (opts.ssg && opts.ssr) {
|
|
8976
|
+
throw new Error(
|
|
8977
|
+
"[vite-plugin-kiru]: Cannot enable both SSG and SSR modes simultaneously"
|
|
8978
|
+
);
|
|
8979
|
+
}
|
|
8980
|
+
const { ssg, ssr } = opts;
|
|
8981
|
+
if (ssg) {
|
|
8982
|
+
if (ssg === true) {
|
|
8983
|
+
return {
|
|
8984
|
+
...state,
|
|
8985
|
+
ssgOptions: defaultSSGOptions
|
|
8986
|
+
};
|
|
8987
|
+
}
|
|
8988
|
+
if (ssg.baseUrl && !ssg.baseUrl.startsWith("/")) {
|
|
8989
|
+
throw new Error("[vite-plugin-kiru]: ssg.baseUrl must start with '/'");
|
|
8990
|
+
}
|
|
8991
|
+
const {
|
|
8992
|
+
baseUrl,
|
|
8993
|
+
dir,
|
|
8994
|
+
document,
|
|
8995
|
+
page,
|
|
8996
|
+
layout,
|
|
8997
|
+
guard,
|
|
8998
|
+
transition,
|
|
8999
|
+
build: { maxConcurrentRenders }
|
|
9000
|
+
} = defaultSSGOptions;
|
|
8944
9001
|
return {
|
|
8945
9002
|
...state,
|
|
8946
|
-
ssgOptions:
|
|
9003
|
+
ssgOptions: {
|
|
9004
|
+
baseUrl,
|
|
9005
|
+
dir,
|
|
9006
|
+
document,
|
|
9007
|
+
page,
|
|
9008
|
+
layout,
|
|
9009
|
+
guard,
|
|
9010
|
+
transition,
|
|
9011
|
+
sitemap: ssg.sitemap,
|
|
9012
|
+
...ssg,
|
|
9013
|
+
build: {
|
|
9014
|
+
maxConcurrentRenders: ssg.build?.maxConcurrentRenders ?? maxConcurrentRenders
|
|
9015
|
+
}
|
|
9016
|
+
}
|
|
8947
9017
|
};
|
|
8948
9018
|
}
|
|
8949
|
-
if (
|
|
8950
|
-
|
|
8951
|
-
|
|
8952
|
-
const {
|
|
8953
|
-
baseUrl,
|
|
8954
|
-
dir,
|
|
8955
|
-
document,
|
|
8956
|
-
page,
|
|
8957
|
-
layout,
|
|
8958
|
-
transition,
|
|
8959
|
-
build: { maxConcurrentRenders }
|
|
8960
|
-
} = defaultSSGOptions;
|
|
8961
|
-
return {
|
|
8962
|
-
...state,
|
|
8963
|
-
ssgOptions: {
|
|
8964
|
-
...ssg,
|
|
8965
|
-
baseUrl: ssg.baseUrl ?? baseUrl,
|
|
8966
|
-
dir: ssg.dir ?? dir,
|
|
8967
|
-
document: ssg.document ?? document,
|
|
8968
|
-
page: ssg.page ?? page,
|
|
8969
|
-
layout: ssg.layout ?? layout,
|
|
8970
|
-
transition: ssg.transition ?? transition,
|
|
8971
|
-
sitemap: ssg.sitemap,
|
|
8972
|
-
build: {
|
|
8973
|
-
maxConcurrentRenders: ssg.build?.maxConcurrentRenders ?? maxConcurrentRenders
|
|
8974
|
-
}
|
|
9019
|
+
if (ssr) {
|
|
9020
|
+
if (ssr.baseUrl && !ssr.baseUrl.startsWith("/")) {
|
|
9021
|
+
throw new Error("[vite-plugin-kiru]: ssr.baseUrl must start with '/'");
|
|
8975
9022
|
}
|
|
8976
|
-
|
|
9023
|
+
const { baseUrl, dir, document, page, layout, guard, transition } = defaultSSROptions;
|
|
9024
|
+
return {
|
|
9025
|
+
...state,
|
|
9026
|
+
ssrOptions: {
|
|
9027
|
+
baseUrl,
|
|
9028
|
+
dir,
|
|
9029
|
+
document,
|
|
9030
|
+
page,
|
|
9031
|
+
layout,
|
|
9032
|
+
guard,
|
|
9033
|
+
transition,
|
|
9034
|
+
...ssr
|
|
9035
|
+
}
|
|
9036
|
+
};
|
|
9037
|
+
}
|
|
9038
|
+
return state;
|
|
8977
9039
|
}
|
|
8978
9040
|
function createViteConfig(config, opts) {
|
|
8979
|
-
|
|
9041
|
+
const hasSSG = !!opts.ssg;
|
|
9042
|
+
const hasSSR = !!opts.ssr;
|
|
9043
|
+
if (!hasSSG && !hasSSR) {
|
|
8980
9044
|
return {
|
|
8981
9045
|
...config,
|
|
8982
9046
|
esbuild: {
|
|
@@ -9046,6 +9110,7 @@ function updatePluginState(state, config, opts) {
|
|
|
9046
9110
|
dtHostScriptPath: state.dtHostScriptPath,
|
|
9047
9111
|
manifestPath: state.manifestPath,
|
|
9048
9112
|
ssgOptions: state.ssgOptions,
|
|
9113
|
+
ssrOptions: state.ssrOptions,
|
|
9049
9114
|
staticProps: {}
|
|
9050
9115
|
};
|
|
9051
9116
|
}
|
|
@@ -11274,7 +11339,7 @@ async function getClientAssets(clientOutDirAbs, manifestPath) {
|
|
|
11274
11339
|
const clientManifestPath = path6.resolve(clientOutDirAbs, manifestPath);
|
|
11275
11340
|
if (fs3.existsSync(clientManifestPath)) {
|
|
11276
11341
|
const manifest = JSON.parse(fs3.readFileSync(clientManifestPath, "utf-8"));
|
|
11277
|
-
const clientEntryKey =
|
|
11342
|
+
const clientEntryKey = VIRTUAL_ENTRY_CLIENT_ID;
|
|
11278
11343
|
if (manifest[clientEntryKey]?.file) {
|
|
11279
11344
|
clientEntry = manifest[clientEntryKey].file;
|
|
11280
11345
|
}
|
|
@@ -11300,7 +11365,7 @@ function collectCssForModules(manifest, moduleIds, projectRoot) {
|
|
|
11300
11365
|
(it.css || []).forEach((c) => cssFiles.add(c));
|
|
11301
11366
|
(it.imports || []).forEach((imp) => collectCss(imp));
|
|
11302
11367
|
};
|
|
11303
|
-
const entryClientKey =
|
|
11368
|
+
const entryClientKey = VIRTUAL_ENTRY_CLIENT_ID;
|
|
11304
11369
|
if (manifest[entryClientKey] && !seen.has(entryClientKey)) {
|
|
11305
11370
|
collectCss(entryClientKey);
|
|
11306
11371
|
}
|
|
@@ -11363,10 +11428,10 @@ async function renderRoute(state, mod, route, srcFilePath, clientEntry, manifest
|
|
|
11363
11428
|
const documentModuleId = documentPath.replace(/\\/g, "/");
|
|
11364
11429
|
moduleIds.push(documentModuleId);
|
|
11365
11430
|
const ctx = {
|
|
11366
|
-
registerModule
|
|
11431
|
+
registerModule(moduleId) {
|
|
11367
11432
|
moduleIds.push(moduleId);
|
|
11368
11433
|
},
|
|
11369
|
-
|
|
11434
|
+
registerStaticProps(props) {
|
|
11370
11435
|
;
|
|
11371
11436
|
(state.staticProps[srcFilePath] ??= {})[route] = props;
|
|
11372
11437
|
}
|
|
@@ -11519,7 +11584,7 @@ async function appendStaticPropsToClientModules(state, clientOutDirAbs, log) {
|
|
|
11519
11584
|
Object.keys(manifest).length,
|
|
11520
11585
|
"entries"
|
|
11521
11586
|
);
|
|
11522
|
-
const clientEntryChunk = manifest[
|
|
11587
|
+
const clientEntryChunk = manifest[VIRTUAL_ENTRY_CLIENT_ID];
|
|
11523
11588
|
if (!clientEntryChunk) {
|
|
11524
11589
|
throw new Error("Client entry chunk not found in manifest");
|
|
11525
11590
|
}
|
|
@@ -11548,6 +11613,36 @@ ${code}`, "utf-8");
|
|
|
11548
11613
|
|
|
11549
11614
|
// src/index.ts
|
|
11550
11615
|
import { build } from "vite";
|
|
11616
|
+
|
|
11617
|
+
// src/globals.ts
|
|
11618
|
+
var $KIRU_HEADLESS_GLOBAL = Symbol.for("kiru.headlessGlobal");
|
|
11619
|
+
var global = globalThis[$KIRU_HEADLESS_GLOBAL] ??= {
|
|
11620
|
+
viteDevServer: null,
|
|
11621
|
+
server: null
|
|
11622
|
+
};
|
|
11623
|
+
var VITE_DEV_SERVER_INSTANCE = {
|
|
11624
|
+
get current() {
|
|
11625
|
+
return global.viteDevServer;
|
|
11626
|
+
},
|
|
11627
|
+
set current(server) {
|
|
11628
|
+
global.viteDevServer = server;
|
|
11629
|
+
}
|
|
11630
|
+
};
|
|
11631
|
+
var entryServerResolvers = [];
|
|
11632
|
+
var KIRU_SERVER_ENTRY = {
|
|
11633
|
+
get current() {
|
|
11634
|
+
return global.serverEntry;
|
|
11635
|
+
},
|
|
11636
|
+
set current(server) {
|
|
11637
|
+
global.serverEntry = server;
|
|
11638
|
+
if (server) {
|
|
11639
|
+
entryServerResolvers.forEach((fn) => fn());
|
|
11640
|
+
entryServerResolvers = [];
|
|
11641
|
+
}
|
|
11642
|
+
}
|
|
11643
|
+
};
|
|
11644
|
+
|
|
11645
|
+
// src/index.ts
|
|
11551
11646
|
function kiru(opts = {}) {
|
|
11552
11647
|
let state;
|
|
11553
11648
|
let log;
|
|
@@ -11563,10 +11658,17 @@ function kiru(opts = {}) {
|
|
|
11563
11658
|
const initialState = createPluginState(opts);
|
|
11564
11659
|
state = updatePluginState(initialState, config, opts);
|
|
11565
11660
|
log = createLogger(state);
|
|
11566
|
-
if (state.
|
|
11661
|
+
if (state.ssrOptions) {
|
|
11662
|
+
virtualModules = await createVirtualModules(
|
|
11663
|
+
state.projectRoot,
|
|
11664
|
+
state.ssrOptions,
|
|
11665
|
+
"ssr"
|
|
11666
|
+
);
|
|
11667
|
+
} else if (state.ssgOptions) {
|
|
11567
11668
|
virtualModules = await createVirtualModules(
|
|
11568
11669
|
state.projectRoot,
|
|
11569
|
-
state.ssgOptions
|
|
11670
|
+
state.ssgOptions,
|
|
11671
|
+
"ssg"
|
|
11570
11672
|
);
|
|
11571
11673
|
}
|
|
11572
11674
|
},
|
|
@@ -11583,10 +11685,12 @@ function kiru(opts = {}) {
|
|
|
11583
11685
|
createPreviewMiddleware(state.projectRoot, state.baseOutDir)
|
|
11584
11686
|
);
|
|
11585
11687
|
},
|
|
11586
|
-
configureServer(server) {
|
|
11688
|
+
async configureServer(server) {
|
|
11689
|
+
VITE_DEV_SERVER_INSTANCE.current = server;
|
|
11587
11690
|
if (state.isProduction || state.isBuild) return;
|
|
11588
11691
|
const {
|
|
11589
11692
|
ssgOptions,
|
|
11693
|
+
ssrOptions,
|
|
11590
11694
|
devtoolsEnabled,
|
|
11591
11695
|
dtClientPathname,
|
|
11592
11696
|
dtHostScriptPath,
|
|
@@ -11607,26 +11711,40 @@ function kiru(opts = {}) {
|
|
|
11607
11711
|
const url = req.originalUrl || req.url || "/";
|
|
11608
11712
|
const filePath = path7.join(state.baseOutDir, "client", url);
|
|
11609
11713
|
const extName = path7.extname(filePath);
|
|
11610
|
-
if (extName && extName !== ".html") {
|
|
11611
|
-
return next();
|
|
11612
|
-
}
|
|
11613
11714
|
const accept = req.headers["accept"] || "";
|
|
11614
|
-
|
|
11615
|
-
|
|
11616
|
-
|
|
11617
|
-
url,
|
|
11618
|
-
state.projectRoot,
|
|
11619
|
-
() => resolveUserDocument(projectRoot, ssgOptions)
|
|
11620
|
-
);
|
|
11621
|
-
res.statusCode = status;
|
|
11622
|
-
res.setHeader("Content-Type", "text/html");
|
|
11623
|
-
res.end(html);
|
|
11624
|
-
return;
|
|
11715
|
+
const shouldHandle = typeof accept === "string" && accept.includes("text/html") && !url.startsWith("/node_modules/") && !url.startsWith("/@") && !url.startsWith(dtHostScriptPath) && !url.startsWith(dtClientPathname) && (extName === ".html" || !extName);
|
|
11716
|
+
if (!shouldHandle) {
|
|
11717
|
+
return next();
|
|
11625
11718
|
}
|
|
11719
|
+
const { status, html } = await handleSSR(
|
|
11720
|
+
server,
|
|
11721
|
+
url,
|
|
11722
|
+
state.projectRoot,
|
|
11723
|
+
() => resolveUserDocument(projectRoot, ssgOptions)
|
|
11724
|
+
);
|
|
11725
|
+
res.statusCode = status;
|
|
11726
|
+
res.setHeader("Content-Type", "text/html");
|
|
11727
|
+
res.end(html);
|
|
11626
11728
|
} catch (e) {
|
|
11627
|
-
|
|
11729
|
+
const error = e;
|
|
11730
|
+
console.error(
|
|
11731
|
+
ANSI.red("[SSG] Middleware Error"),
|
|
11732
|
+
`
|
|
11733
|
+
${ANSI.yellow("URL:")} ${req.url}`,
|
|
11734
|
+
`
|
|
11735
|
+
${ANSI.yellow("Error:")} ${error.message}`
|
|
11736
|
+
);
|
|
11737
|
+
if (error.stack) {
|
|
11738
|
+
console.error(ANSI.black_bright(error.stack));
|
|
11739
|
+
}
|
|
11740
|
+
next(e);
|
|
11628
11741
|
}
|
|
11629
|
-
|
|
11742
|
+
});
|
|
11743
|
+
} else if (ssrOptions) {
|
|
11744
|
+
queueMicrotask(() => {
|
|
11745
|
+
server.ssrLoadModule(VIRTUAL_ENTRY_SERVER_ID).then((mod) => {
|
|
11746
|
+
KIRU_SERVER_ENTRY.current = mod;
|
|
11747
|
+
});
|
|
11630
11748
|
});
|
|
11631
11749
|
}
|
|
11632
11750
|
},
|
|
@@ -11696,15 +11814,14 @@ function kiru(opts = {}) {
|
|
|
11696
11814
|
};
|
|
11697
11815
|
}
|
|
11698
11816
|
};
|
|
11699
|
-
|
|
11700
|
-
|
|
11701
|
-
{
|
|
11817
|
+
const plugins = [mainPlugin];
|
|
11818
|
+
if (opts.ssg) {
|
|
11819
|
+
plugins.push({
|
|
11702
11820
|
name: "vite-plugin-kiru:ssg",
|
|
11703
11821
|
apply: "build",
|
|
11704
11822
|
enforce: "post",
|
|
11705
11823
|
async closeBundle(error) {
|
|
11706
|
-
if (error || this.environment.config.build.ssr
|
|
11707
|
-
return;
|
|
11824
|
+
if (error || this.environment.config.build.ssr) return;
|
|
11708
11825
|
log(ANSI.cyan("[SSG]"), "Starting SSG build...");
|
|
11709
11826
|
await build({
|
|
11710
11827
|
...inlineConfig,
|
|
@@ -11716,8 +11833,35 @@ function kiru(opts = {}) {
|
|
|
11716
11833
|
});
|
|
11717
11834
|
log(ANSI.cyan("[SSG]"), "SSG build complete!");
|
|
11718
11835
|
}
|
|
11719
|
-
}
|
|
11720
|
-
|
|
11836
|
+
});
|
|
11837
|
+
} else if (opts.ssr) {
|
|
11838
|
+
plugins.push({
|
|
11839
|
+
name: "vite-plugin-kiru:ssr",
|
|
11840
|
+
apply: "build",
|
|
11841
|
+
enforce: "post",
|
|
11842
|
+
async closeBundle(error) {
|
|
11843
|
+
if (error || this.environment.config.build.ssr || !state.ssrOptions)
|
|
11844
|
+
return;
|
|
11845
|
+
log(ANSI.cyan("[SSR]"), "Starting SSR build...");
|
|
11846
|
+
await build({
|
|
11847
|
+
...inlineConfig,
|
|
11848
|
+
configFile: false,
|
|
11849
|
+
build: {
|
|
11850
|
+
...inlineConfig?.build,
|
|
11851
|
+
ssr: true,
|
|
11852
|
+
rollupOptions: {
|
|
11853
|
+
input: [
|
|
11854
|
+
path7.resolve(state.projectRoot, state.ssrOptions.runtimeEntry),
|
|
11855
|
+
VIRTUAL_ENTRY_SERVER_ID
|
|
11856
|
+
]
|
|
11857
|
+
}
|
|
11858
|
+
}
|
|
11859
|
+
});
|
|
11860
|
+
log(ANSI.cyan("[SSR]"), "SSR build complete!");
|
|
11861
|
+
}
|
|
11862
|
+
});
|
|
11863
|
+
}
|
|
11864
|
+
return plugins;
|
|
11721
11865
|
}
|
|
11722
11866
|
function onHMR(callback) {
|
|
11723
11867
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import type { Readable } from "node:stream"
|
|
2
|
-
|
|
3
1
|
export interface SSRHttpResponse {
|
|
4
2
|
html: string
|
|
5
3
|
statusCode: number
|
|
6
4
|
headers: Array<[string, string]>
|
|
7
|
-
stream:
|
|
5
|
+
stream: ReadableStream | null
|
|
8
6
|
}
|
|
9
7
|
|
|
10
8
|
export interface SSRRenderResult {
|
|
@@ -13,6 +11,7 @@ export interface SSRRenderResult {
|
|
|
13
11
|
|
|
14
12
|
export interface ServerRenderOptions {
|
|
15
13
|
url: string
|
|
14
|
+
context: Kiru.RequestContext
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export declare function renderPage(
|
package/dist/server.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
// src/server.ts
|
|
2
|
-
import path3 from "node:path";
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
|
|
5
|
-
// src/utils.ts
|
|
6
2
|
import path2 from "node:path";
|
|
3
|
+
import fs from "node:fs";
|
|
7
4
|
|
|
8
5
|
// ../../node_modules/.pnpm/@isaacs+balanced-match@4.0.1/node_modules/@isaacs/balanced-match/dist/esm/index.js
|
|
9
6
|
var balanced = (a, b, str) => {
|
|
@@ -1557,7 +1554,7 @@ minimatch.Minimatch = Minimatch;
|
|
|
1557
1554
|
minimatch.escape = escape;
|
|
1558
1555
|
minimatch.unescape = unescape;
|
|
1559
1556
|
|
|
1560
|
-
// ../../node_modules/.pnpm/glob@
|
|
1557
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/glob.js
|
|
1561
1558
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
1562
1559
|
|
|
1563
1560
|
// ../../node_modules/.pnpm/lru-cache@11.2.2/node_modules/lru-cache/dist/esm/index.js
|
|
@@ -4111,12 +4108,12 @@ var PathBase = class {
|
|
|
4111
4108
|
/**
|
|
4112
4109
|
* Get the Path object referenced by the string path, resolved from this Path
|
|
4113
4110
|
*/
|
|
4114
|
-
resolve(
|
|
4115
|
-
if (!
|
|
4111
|
+
resolve(path3) {
|
|
4112
|
+
if (!path3) {
|
|
4116
4113
|
return this;
|
|
4117
4114
|
}
|
|
4118
|
-
const rootPath = this.getRootString(
|
|
4119
|
-
const dir =
|
|
4115
|
+
const rootPath = this.getRootString(path3);
|
|
4116
|
+
const dir = path3.substring(rootPath.length);
|
|
4120
4117
|
const dirParts = dir.split(this.splitSep);
|
|
4121
4118
|
const result = rootPath ? this.getRoot(rootPath).#resolveParts(dirParts) : this.#resolveParts(dirParts);
|
|
4122
4119
|
return result;
|
|
@@ -4868,8 +4865,8 @@ var PathWin32 = class _PathWin32 extends PathBase {
|
|
|
4868
4865
|
/**
|
|
4869
4866
|
* @internal
|
|
4870
4867
|
*/
|
|
4871
|
-
getRootString(
|
|
4872
|
-
return win32.parse(
|
|
4868
|
+
getRootString(path3) {
|
|
4869
|
+
return win32.parse(path3).root;
|
|
4873
4870
|
}
|
|
4874
4871
|
/**
|
|
4875
4872
|
* @internal
|
|
@@ -4915,8 +4912,8 @@ var PathPosix = class _PathPosix extends PathBase {
|
|
|
4915
4912
|
/**
|
|
4916
4913
|
* @internal
|
|
4917
4914
|
*/
|
|
4918
|
-
getRootString(
|
|
4919
|
-
return
|
|
4915
|
+
getRootString(path3) {
|
|
4916
|
+
return path3.startsWith("/") ? "/" : "";
|
|
4920
4917
|
}
|
|
4921
4918
|
/**
|
|
4922
4919
|
* @internal
|
|
@@ -5005,11 +5002,11 @@ var PathScurryBase = class {
|
|
|
5005
5002
|
/**
|
|
5006
5003
|
* Get the depth of a provided path, string, or the cwd
|
|
5007
5004
|
*/
|
|
5008
|
-
depth(
|
|
5009
|
-
if (typeof
|
|
5010
|
-
|
|
5005
|
+
depth(path3 = this.cwd) {
|
|
5006
|
+
if (typeof path3 === "string") {
|
|
5007
|
+
path3 = this.cwd.resolve(path3);
|
|
5011
5008
|
}
|
|
5012
|
-
return
|
|
5009
|
+
return path3.depth();
|
|
5013
5010
|
}
|
|
5014
5011
|
/**
|
|
5015
5012
|
* Return the cache of child entries. Exposed so subclasses can create
|
|
@@ -5496,9 +5493,9 @@ var PathScurryBase = class {
|
|
|
5496
5493
|
process2();
|
|
5497
5494
|
return results;
|
|
5498
5495
|
}
|
|
5499
|
-
chdir(
|
|
5496
|
+
chdir(path3 = this.cwd) {
|
|
5500
5497
|
const oldCwd = this.cwd;
|
|
5501
|
-
this.cwd = typeof
|
|
5498
|
+
this.cwd = typeof path3 === "string" ? this.cwd.resolve(path3) : path3;
|
|
5502
5499
|
this.cwd[setAsCwd](oldCwd);
|
|
5503
5500
|
}
|
|
5504
5501
|
};
|
|
@@ -5572,7 +5569,7 @@ var PathScurryDarwin = class extends PathScurryPosix {
|
|
|
5572
5569
|
var Path = process.platform === "win32" ? PathWin32 : PathPosix;
|
|
5573
5570
|
var PathScurry = process.platform === "win32" ? PathScurryWin32 : process.platform === "darwin" ? PathScurryDarwin : PathScurryPosix;
|
|
5574
5571
|
|
|
5575
|
-
// ../../node_modules/.pnpm/glob@
|
|
5572
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/pattern.js
|
|
5576
5573
|
var isPatternList = (pl) => pl.length >= 1;
|
|
5577
5574
|
var isGlobList = (gl) => gl.length >= 1;
|
|
5578
5575
|
var Pattern = class _Pattern {
|
|
@@ -5737,7 +5734,7 @@ var Pattern = class _Pattern {
|
|
|
5737
5734
|
}
|
|
5738
5735
|
};
|
|
5739
5736
|
|
|
5740
|
-
// ../../node_modules/.pnpm/glob@
|
|
5737
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/ignore.js
|
|
5741
5738
|
var defaultPlatform2 = typeof process === "object" && process && typeof process.platform === "string" ? process.platform : "linux";
|
|
5742
5739
|
var Ignore = class {
|
|
5743
5740
|
relative;
|
|
@@ -5824,7 +5821,7 @@ var Ignore = class {
|
|
|
5824
5821
|
}
|
|
5825
5822
|
};
|
|
5826
5823
|
|
|
5827
|
-
// ../../node_modules/.pnpm/glob@
|
|
5824
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/processor.js
|
|
5828
5825
|
var HasWalkedCache = class _HasWalkedCache {
|
|
5829
5826
|
store;
|
|
5830
5827
|
constructor(store = /* @__PURE__ */ new Map()) {
|
|
@@ -5854,8 +5851,8 @@ var MatchRecord = class {
|
|
|
5854
5851
|
}
|
|
5855
5852
|
// match, absolute, ifdir
|
|
5856
5853
|
entries() {
|
|
5857
|
-
return [...this.store.entries()].map(([
|
|
5858
|
-
|
|
5854
|
+
return [...this.store.entries()].map(([path3, n]) => [
|
|
5855
|
+
path3,
|
|
5859
5856
|
!!(n & 2),
|
|
5860
5857
|
!!(n & 1)
|
|
5861
5858
|
]);
|
|
@@ -6045,7 +6042,7 @@ var Processor = class _Processor {
|
|
|
6045
6042
|
}
|
|
6046
6043
|
};
|
|
6047
6044
|
|
|
6048
|
-
// ../../node_modules/.pnpm/glob@
|
|
6045
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/walker.js
|
|
6049
6046
|
var makeIgnore = (ignore, opts) => typeof ignore === "string" ? new Ignore([ignore], opts) : Array.isArray(ignore) ? new Ignore(ignore, opts) : ignore;
|
|
6050
6047
|
var GlobUtil = class {
|
|
6051
6048
|
path;
|
|
@@ -6060,9 +6057,9 @@ var GlobUtil = class {
|
|
|
6060
6057
|
signal;
|
|
6061
6058
|
maxDepth;
|
|
6062
6059
|
includeChildMatches;
|
|
6063
|
-
constructor(patterns,
|
|
6060
|
+
constructor(patterns, path3, opts) {
|
|
6064
6061
|
this.patterns = patterns;
|
|
6065
|
-
this.path =
|
|
6062
|
+
this.path = path3;
|
|
6066
6063
|
this.opts = opts;
|
|
6067
6064
|
this.#sep = !opts.posix && opts.platform === "win32" ? "\\" : "/";
|
|
6068
6065
|
this.includeChildMatches = opts.includeChildMatches !== false;
|
|
@@ -6081,11 +6078,11 @@ var GlobUtil = class {
|
|
|
6081
6078
|
});
|
|
6082
6079
|
}
|
|
6083
6080
|
}
|
|
6084
|
-
#ignored(
|
|
6085
|
-
return this.seen.has(
|
|
6081
|
+
#ignored(path3) {
|
|
6082
|
+
return this.seen.has(path3) || !!this.#ignore?.ignored?.(path3);
|
|
6086
6083
|
}
|
|
6087
|
-
#childrenIgnored(
|
|
6088
|
-
return !!this.#ignore?.childrenIgnored?.(
|
|
6084
|
+
#childrenIgnored(path3) {
|
|
6085
|
+
return !!this.#ignore?.childrenIgnored?.(path3);
|
|
6089
6086
|
}
|
|
6090
6087
|
// backpressure mechanism
|
|
6091
6088
|
pause() {
|
|
@@ -6300,8 +6297,8 @@ var GlobUtil = class {
|
|
|
6300
6297
|
};
|
|
6301
6298
|
var GlobWalker = class extends GlobUtil {
|
|
6302
6299
|
matches = /* @__PURE__ */ new Set();
|
|
6303
|
-
constructor(patterns,
|
|
6304
|
-
super(patterns,
|
|
6300
|
+
constructor(patterns, path3, opts) {
|
|
6301
|
+
super(patterns, path3, opts);
|
|
6305
6302
|
}
|
|
6306
6303
|
matchEmit(e) {
|
|
6307
6304
|
this.matches.add(e);
|
|
@@ -6338,8 +6335,8 @@ var GlobWalker = class extends GlobUtil {
|
|
|
6338
6335
|
};
|
|
6339
6336
|
var GlobStream = class extends GlobUtil {
|
|
6340
6337
|
results;
|
|
6341
|
-
constructor(patterns,
|
|
6342
|
-
super(patterns,
|
|
6338
|
+
constructor(patterns, path3, opts) {
|
|
6339
|
+
super(patterns, path3, opts);
|
|
6343
6340
|
this.results = new Minipass({
|
|
6344
6341
|
signal: this.signal,
|
|
6345
6342
|
objectMode: true
|
|
@@ -6372,7 +6369,7 @@ var GlobStream = class extends GlobUtil {
|
|
|
6372
6369
|
}
|
|
6373
6370
|
};
|
|
6374
6371
|
|
|
6375
|
-
// ../../node_modules/.pnpm/glob@
|
|
6372
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/glob.js
|
|
6376
6373
|
var defaultPlatform3 = typeof process === "object" && process && typeof process.platform === "string" ? process.platform : "linux";
|
|
6377
6374
|
var Glob = class {
|
|
6378
6375
|
absolute;
|
|
@@ -6572,7 +6569,7 @@ var Glob = class {
|
|
|
6572
6569
|
}
|
|
6573
6570
|
};
|
|
6574
6571
|
|
|
6575
|
-
// ../../node_modules/.pnpm/glob@
|
|
6572
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/has-magic.js
|
|
6576
6573
|
var hasMagic = (pattern, options = {}) => {
|
|
6577
6574
|
if (!Array.isArray(pattern)) {
|
|
6578
6575
|
pattern = [pattern];
|
|
@@ -6584,7 +6581,7 @@ var hasMagic = (pattern, options = {}) => {
|
|
|
6584
6581
|
return false;
|
|
6585
6582
|
};
|
|
6586
6583
|
|
|
6587
|
-
// ../../node_modules/.pnpm/glob@
|
|
6584
|
+
// ../../node_modules/.pnpm/glob@12.0.0/node_modules/glob/dist/esm/index.js
|
|
6588
6585
|
function globStreamSync(pattern, options = {}) {
|
|
6589
6586
|
return new Glob(pattern, options).streamSync();
|
|
6590
6587
|
}
|
|
@@ -6660,47 +6657,66 @@ function tEntries(obj) {
|
|
|
6660
6657
|
return Object.entries(obj);
|
|
6661
6658
|
}
|
|
6662
6659
|
|
|
6663
|
-
// src/utils.ts
|
|
6664
|
-
function resolveUserDocument(projectRoot, options) {
|
|
6665
|
-
const { dir, document } = options;
|
|
6666
|
-
const fp = path2.resolve(projectRoot, dir, document).replace(/\\/g, "/");
|
|
6667
|
-
const matches = globSync(fp);
|
|
6668
|
-
if (!matches.length) {
|
|
6669
|
-
throw new Error(`Document not found at ${fp}`);
|
|
6670
|
-
}
|
|
6671
|
-
return path2.resolve(projectRoot, matches[0]).replace(/\\/g, "/");
|
|
6672
|
-
}
|
|
6673
|
-
|
|
6674
6660
|
// src/virtual-modules.ts
|
|
6661
|
+
var VIRTUAL_ENTRY_SERVER_ID = "virtual:kiru:entry-server";
|
|
6675
6662
|
var VIRTUAL_ENTRY_CLIENT_ID = "virtual:kiru:entry-client";
|
|
6676
6663
|
|
|
6677
6664
|
// src/globals.ts
|
|
6665
|
+
var $KIRU_HEADLESS_GLOBAL = Symbol.for("kiru.headlessGlobal");
|
|
6666
|
+
var global = globalThis[$KIRU_HEADLESS_GLOBAL] ??= {
|
|
6667
|
+
viteDevServer: null,
|
|
6668
|
+
server: null
|
|
6669
|
+
};
|
|
6678
6670
|
var VITE_DEV_SERVER_INSTANCE = {
|
|
6679
6671
|
get current() {
|
|
6680
|
-
return
|
|
6681
|
-
// @ts-ignore
|
|
6682
|
-
globalThis.KIRU_VITE_DEV_SERVER_INSTANCE ?? null
|
|
6683
|
-
);
|
|
6672
|
+
return global.viteDevServer;
|
|
6684
6673
|
},
|
|
6685
6674
|
set current(server) {
|
|
6686
|
-
|
|
6675
|
+
global.viteDevServer = server;
|
|
6687
6676
|
}
|
|
6688
6677
|
};
|
|
6678
|
+
var entryServerResolvers = [];
|
|
6679
|
+
var KIRU_SERVER_ENTRY = {
|
|
6680
|
+
get current() {
|
|
6681
|
+
return global.serverEntry;
|
|
6682
|
+
},
|
|
6683
|
+
set current(server) {
|
|
6684
|
+
global.serverEntry = server;
|
|
6685
|
+
if (server) {
|
|
6686
|
+
entryServerResolvers.forEach((fn) => fn());
|
|
6687
|
+
entryServerResolvers = [];
|
|
6688
|
+
}
|
|
6689
|
+
}
|
|
6690
|
+
};
|
|
6691
|
+
async function awaitServerRendererInitialized_Dev() {
|
|
6692
|
+
if (KIRU_SERVER_ENTRY.current) {
|
|
6693
|
+
return Promise.resolve(KIRU_SERVER_ENTRY.current);
|
|
6694
|
+
}
|
|
6695
|
+
return new Promise((resolve, reject) => {
|
|
6696
|
+
const timeout = setTimeout(() => {
|
|
6697
|
+
entryServerResolvers = entryServerResolvers.filter((r) => r !== resolve);
|
|
6698
|
+
reject(new Error("Failed to acquire server renderer. Seek help!"));
|
|
6699
|
+
}, 1e4);
|
|
6700
|
+
entryServerResolvers.push(() => {
|
|
6701
|
+
clearTimeout(timeout);
|
|
6702
|
+
resolve(KIRU_SERVER_ENTRY.current);
|
|
6703
|
+
});
|
|
6704
|
+
});
|
|
6705
|
+
}
|
|
6689
6706
|
|
|
6690
6707
|
// src/server.ts
|
|
6691
6708
|
async function getClientAssets(clientOutDirAbs, manifestPath) {
|
|
6692
6709
|
let clientEntry = null;
|
|
6693
6710
|
let manifest = null;
|
|
6694
6711
|
try {
|
|
6695
|
-
const clientManifestPath =
|
|
6712
|
+
const clientManifestPath = path2.resolve(clientOutDirAbs, manifestPath);
|
|
6696
6713
|
if (fs.existsSync(clientManifestPath)) {
|
|
6697
6714
|
const parsedManifest = JSON.parse(
|
|
6698
6715
|
fs.readFileSync(clientManifestPath, "utf-8")
|
|
6699
6716
|
);
|
|
6700
6717
|
manifest = parsedManifest;
|
|
6701
|
-
|
|
6702
|
-
|
|
6703
|
-
clientEntry = parsedManifest[clientEntryKey].file;
|
|
6718
|
+
if (parsedManifest[VIRTUAL_ENTRY_CLIENT_ID]?.file) {
|
|
6719
|
+
clientEntry = parsedManifest[VIRTUAL_ENTRY_CLIENT_ID].file;
|
|
6704
6720
|
}
|
|
6705
6721
|
}
|
|
6706
6722
|
} catch {
|
|
@@ -6723,7 +6739,7 @@ function collectCssForModules(manifest, moduleIds, projectRoot) {
|
|
|
6723
6739
|
(it.css || []).forEach((c) => cssFiles.add(c));
|
|
6724
6740
|
(it.imports || []).forEach((imp) => collectCss(imp));
|
|
6725
6741
|
};
|
|
6726
|
-
const entryClientKey =
|
|
6742
|
+
const entryClientKey = VIRTUAL_ENTRY_CLIENT_ID;
|
|
6727
6743
|
if (manifestRef[entryClientKey] && !seen.has(entryClientKey)) {
|
|
6728
6744
|
collectCss(entryClientKey);
|
|
6729
6745
|
}
|
|
@@ -6749,8 +6765,8 @@ function collectCssForModules(manifest, moduleIds, projectRoot) {
|
|
|
6749
6765
|
}
|
|
6750
6766
|
for (const key in manifestRef) {
|
|
6751
6767
|
const keyNormalized = key.replace(/\\/g, "/");
|
|
6752
|
-
const moduleBaseName =
|
|
6753
|
-
const keyBaseName =
|
|
6768
|
+
const moduleBaseName = path2.basename(normalizedId);
|
|
6769
|
+
const keyBaseName = path2.basename(keyNormalized);
|
|
6754
6770
|
if ((keyNormalized.endsWith(normalizedId) || normalizedId.endsWith(keyNormalized)) && moduleBaseName === keyBaseName) {
|
|
6755
6771
|
collectCss(key);
|
|
6756
6772
|
break;
|
|
@@ -6763,17 +6779,61 @@ function collectCssForModules(manifest, moduleIds, projectRoot) {
|
|
|
6763
6779
|
}
|
|
6764
6780
|
return "";
|
|
6765
6781
|
}
|
|
6782
|
+
async function resolveServerRenderer() {
|
|
6783
|
+
if (process.env.NODE_ENV !== "production") {
|
|
6784
|
+
return awaitServerRendererInitialized_Dev();
|
|
6785
|
+
}
|
|
6786
|
+
return loadServerRenderer_Production();
|
|
6787
|
+
}
|
|
6788
|
+
async function loadServerRenderer_Production() {
|
|
6789
|
+
const projectRoot = process.cwd().replace(/\\/g, "/");
|
|
6790
|
+
const serverOutDirAbs = path2.resolve(projectRoot, "dist/server");
|
|
6791
|
+
const manifestPath = path2.resolve(serverOutDirAbs, "vite-manifest.json");
|
|
6792
|
+
if (!fs.existsSync(manifestPath)) {
|
|
6793
|
+
throw new Error(
|
|
6794
|
+
`Server manifest not found at ${manifestPath}. Make sure the SSR build has been completed.`
|
|
6795
|
+
);
|
|
6796
|
+
}
|
|
6797
|
+
const manifest = JSON.parse(
|
|
6798
|
+
fs.readFileSync(manifestPath, "utf-8")
|
|
6799
|
+
);
|
|
6800
|
+
const virtualEntryServerModule = Object.values(manifest).find(
|
|
6801
|
+
(value) => value.src === VIRTUAL_ENTRY_SERVER_ID
|
|
6802
|
+
);
|
|
6803
|
+
if (!virtualEntryServerModule) {
|
|
6804
|
+
throw new Error(
|
|
6805
|
+
"Virtual entry server module not found in manifest. Make sure the SSR build has been completed."
|
|
6806
|
+
);
|
|
6807
|
+
}
|
|
6808
|
+
const entryServerFile = virtualEntryServerModule.file;
|
|
6809
|
+
const entryServerPath = path2.resolve(serverOutDirAbs, entryServerFile);
|
|
6810
|
+
if (!fs.existsSync(entryServerPath)) {
|
|
6811
|
+
throw new Error(
|
|
6812
|
+
`Virtual entry server module file not found at ${entryServerPath}`
|
|
6813
|
+
);
|
|
6814
|
+
}
|
|
6815
|
+
const fileUrl = `file://${entryServerPath.replace(/\\/g, "/")}`;
|
|
6816
|
+
const module = await import(
|
|
6817
|
+
/* @vite-ignore */
|
|
6818
|
+
fileUrl
|
|
6819
|
+
);
|
|
6820
|
+
if (!module.render || !module.documentModuleId) {
|
|
6821
|
+
throw new Error(
|
|
6822
|
+
"Virtual entry server module does not export render and documentModuleId"
|
|
6823
|
+
);
|
|
6824
|
+
}
|
|
6825
|
+
return {
|
|
6826
|
+
render: module.render,
|
|
6827
|
+
documentModuleId: module.documentModuleId
|
|
6828
|
+
};
|
|
6829
|
+
}
|
|
6766
6830
|
async function renderPage(options) {
|
|
6767
|
-
const { render } = await
|
|
6768
|
-
const moduleIds = [];
|
|
6831
|
+
const { render, documentModuleId } = await resolveServerRenderer();
|
|
6832
|
+
const moduleIds = [documentModuleId];
|
|
6769
6833
|
const projectRoot = process.cwd().replace(/\\/g, "/");
|
|
6770
|
-
const documentModule = resolveUserDocument(projectRoot, {
|
|
6771
|
-
dir: "src/pages",
|
|
6772
|
-
document: "document.tsx"
|
|
6773
|
-
}).substring(projectRoot.length);
|
|
6774
|
-
moduleIds.push(documentModule);
|
|
6775
6834
|
const { httpResponse } = await render(options.url, {
|
|
6776
|
-
|
|
6835
|
+
userContext: options.context,
|
|
6836
|
+
registerModule(moduleId) {
|
|
6777
6837
|
moduleIds.push(moduleId);
|
|
6778
6838
|
}
|
|
6779
6839
|
});
|
|
@@ -6785,7 +6845,7 @@ async function renderPage(options) {
|
|
|
6785
6845
|
const isDevelopment = process.env.NODE_ENV !== "production";
|
|
6786
6846
|
let html = httpResponse.html;
|
|
6787
6847
|
if (isDevelopment) {
|
|
6788
|
-
const scriptTag = `<script type="module" src="/@id/${VIRTUAL_ENTRY_CLIENT_ID}"></script>`;
|
|
6848
|
+
const scriptTag = `<script type="module" src="/@id/${VIRTUAL_ENTRY_CLIENT_ID}" async></script>`;
|
|
6789
6849
|
html = html.includes("</body>") ? html.replace("</body>", scriptTag + "</body>") : html + scriptTag;
|
|
6790
6850
|
const importedModules = /* @__PURE__ */ new Set();
|
|
6791
6851
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -6799,7 +6859,7 @@ async function renderPage(options) {
|
|
|
6799
6859
|
}
|
|
6800
6860
|
};
|
|
6801
6861
|
for (const id of moduleIds) {
|
|
6802
|
-
const p =
|
|
6862
|
+
const p = path2.join(projectRoot, id).replace(/\\/g, "/");
|
|
6803
6863
|
const mod = VITE_DEV_SERVER_INSTANCE.current.moduleGraph.getModuleById(p);
|
|
6804
6864
|
if (!mod) {
|
|
6805
6865
|
console.error(`Module not found: ${p}`);
|
|
@@ -6819,7 +6879,7 @@ async function renderPage(options) {
|
|
|
6819
6879
|
html = html.replace("<head>", "<head>" + stylesheets.join("\n"));
|
|
6820
6880
|
}
|
|
6821
6881
|
} else {
|
|
6822
|
-
const clientOutDirAbs =
|
|
6882
|
+
const clientOutDirAbs = path2.resolve(projectRoot, "dist/client");
|
|
6823
6883
|
const { clientEntry, manifest } = await getClientAssets(
|
|
6824
6884
|
clientOutDirAbs,
|
|
6825
6885
|
"vite-manifest.json"
|
|
@@ -6831,10 +6891,15 @@ async function renderPage(options) {
|
|
|
6831
6891
|
}
|
|
6832
6892
|
}
|
|
6833
6893
|
if (clientEntry) {
|
|
6834
|
-
const scriptTag = `<script type="module" src="/${clientEntry}"></script>`;
|
|
6894
|
+
const scriptTag = `<script type="module" src="/${clientEntry}" async></script>`;
|
|
6835
6895
|
html = html.includes("</body>") ? html.replace("</body>", scriptTag + "</body>") : html + scriptTag;
|
|
6836
6896
|
}
|
|
6837
6897
|
}
|
|
6898
|
+
const contextString = JSON.stringify(options.context);
|
|
6899
|
+
html = html.replace(
|
|
6900
|
+
"</head>",
|
|
6901
|
+
`<script type="application/json" k-request-context>${contextString}</script></head>`
|
|
6902
|
+
);
|
|
6838
6903
|
return {
|
|
6839
6904
|
httpResponse: {
|
|
6840
6905
|
...httpResponse,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-kiru",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.0-preview.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -8,8 +8,18 @@
|
|
|
8
8
|
"keywords": [],
|
|
9
9
|
"author": "",
|
|
10
10
|
"license": "ISC",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./server": {
|
|
17
|
+
"types": "./dist/server.d.ts",
|
|
18
|
+
"default": "./dist/server.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
11
21
|
"peerDependencies": {
|
|
12
|
-
"kiru": ">=0.
|
|
22
|
+
"kiru": ">=0.54.0-preview.0"
|
|
13
23
|
},
|
|
14
24
|
"devDependencies": {
|
|
15
25
|
"@types/node": "^22.17.0",
|
|
@@ -17,8 +27,9 @@
|
|
|
17
27
|
"rollup": "^4.46.2",
|
|
18
28
|
"tsx": "^4.20.3",
|
|
19
29
|
"typescript": "^5.9.2",
|
|
30
|
+
"kiru-devtools-client": "^0.0.0",
|
|
20
31
|
"kiru-devtools-host": "^1.0.0",
|
|
21
|
-
"kiru
|
|
32
|
+
"kiru": "^0.54.0-preview.0"
|
|
22
33
|
},
|
|
23
34
|
"dependencies": {
|
|
24
35
|
"glob": "^12.0.0",
|