vike-lite 1.0.17 → 1.0.18
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/README.md +4 -3
- package/dist/vite.mjs +14 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,18 +17,19 @@ Add Vite plugin
|
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
19
|
// vite.config.ts
|
|
20
|
+
import type { UserConfig } from 'vite'
|
|
20
21
|
import vikeLite from 'vike-lite/vite'
|
|
21
22
|
|
|
22
23
|
export default {
|
|
23
24
|
plugins: [
|
|
24
25
|
vikeLite()
|
|
25
26
|
]
|
|
26
|
-
}
|
|
27
|
+
} satisfies UserConfig
|
|
27
28
|
```
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
### 🪝 Hooks
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
#### renderPage
|
|
32
33
|
```ts
|
|
33
34
|
// /server/index.ts
|
|
34
35
|
import { cors } from 'hono/cors'
|
package/dist/vite.mjs
CHANGED
|
@@ -54,31 +54,27 @@ function generateRoutes(pagesAbsPath) {
|
|
|
54
54
|
errorRoute
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
const cssCache = /* @__PURE__ */ new Map();
|
|
58
57
|
/**
|
|
59
58
|
* Anti-FOUC: Inspect all known SSR modules, ask the client environment
|
|
60
59
|
* to translate them into plain text (thanks to ?direct) and return them.
|
|
61
60
|
*/
|
|
62
61
|
async function injectFOUCStyles(server, html) {
|
|
62
|
+
const styles = /* @__PURE__ */ new Set();
|
|
63
63
|
const ssrEnv = server.environments.ssr;
|
|
64
64
|
const clientEnv = server.environments.client;
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
} catch {}
|
|
73
|
-
if (css) styles.add(css);
|
|
65
|
+
for (const mod of ssrEnv.moduleGraph.idToModuleMap.values()) {
|
|
66
|
+
if (!(mod.file && /\.(css|scss|sass|less|styl|stylus)($|\?)/.test(mod.file))) continue;
|
|
67
|
+
const url = mod.url.split("?", 1)[0] + "?direct";
|
|
68
|
+
try {
|
|
69
|
+
const result = await clientEnv.transformRequest(url);
|
|
70
|
+
if (result?.code) styles.add(result.code);
|
|
71
|
+
} catch (e) {}
|
|
74
72
|
}
|
|
75
73
|
if (styles.size === 0) return html;
|
|
74
|
+
const cssContent = Array.from(styles).join("");
|
|
76
75
|
const headEndIndex = html.lastIndexOf("</head>");
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
for (const s of styles) cssContent += s;
|
|
80
|
-
const inlineCss = `<style type="text/css" data-vite-dev-fouc>${cssContent}</style>`;
|
|
81
|
-
return html.slice(0, headEndIndex) + inlineCss + html.slice(headEndIndex, bodyEndIndex) + `<script type="module" data-vite-dev-fouc-cleanup>requestAnimationFrame(()=>{document.querySelectorAll('[data-vite-dev-fouc]').forEach(el=>el.remove());document.currentScript?.remove();})<\/script>` + html.slice(bodyEndIndex);
|
|
76
|
+
if (headEndIndex === -1) return html;
|
|
77
|
+
return `${html.slice(0, headEndIndex)}<style type="text/css" data-vite-dev-fouc>${cssContent}</style>${html.slice(headEndIndex)}`;
|
|
82
78
|
}
|
|
83
79
|
function routerPlugin({ pagesDir = "pages", serverEntry = "server/index.ts", apiPrefix = "/api" } = {}) {
|
|
84
80
|
const isProd = process.env.NODE_ENV === "production";
|
|
@@ -177,11 +173,10 @@ function routerPlugin({ pagesDir = "pages", serverEntry = "server/index.ts", api
|
|
|
177
173
|
const manifestPath = path.join(viteConfigRoot, "../dist/client/.vite/manifest.json");
|
|
178
174
|
return `export default ${fs.readFileSync(manifestPath, "utf8")}`;
|
|
179
175
|
}
|
|
180
|
-
if (id === resolvedVirtualEntryClientId) return `
|
|
181
|
-
import { routes, errorRoute } from '${virtualModuleId}';
|
|
176
|
+
if (id === resolvedVirtualEntryClientId) return `import { routes, errorRoute } from '${virtualModuleId}';
|
|
182
177
|
import { onRenderClient } from '${virtualAdapterId}';
|
|
183
|
-
|
|
184
|
-
|
|
178
|
+
const { default: render } = await onRenderClient();
|
|
179
|
+
render({ routes, errorRoute });`;
|
|
185
180
|
if (id === resolvedVirtualSetupId) return `
|
|
186
181
|
import { routes, errorRoute, config } from '${virtualModuleId}';
|
|
187
182
|
import { setVikeState } from 'vike-lite/__internal/server';
|
|
@@ -243,9 +238,6 @@ function routerPlugin({ pagesDir = "pages", serverEntry = "server/index.ts", api
|
|
|
243
238
|
}
|
|
244
239
|
});
|
|
245
240
|
};
|
|
246
|
-
},
|
|
247
|
-
handleHotUpdate(ctx) {
|
|
248
|
-
if (/\.(css|scss|sass|less|styl|stylus)$/.test(ctx.file)) cssCache.delete(ctx.file);
|
|
249
241
|
}
|
|
250
242
|
};
|
|
251
243
|
}
|