vike-lite 1.0.16 → 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.
Files changed (3) hide show
  1. package/README.md +4 -3
  2. package/dist/vite.mjs +12 -24
  3. 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
- #### Hooks
30
+ ### 🪝 Hooks
30
31
 
31
- ### renderPage
32
+ #### renderPage
32
33
  ```ts
33
34
  // /server/index.ts
34
35
  import { cors } from 'hono/cors'
package/dist/vite.mjs CHANGED
@@ -64,19 +64,17 @@ async function injectFOUCStyles(server, html) {
64
64
  const clientEnv = server.environments.client;
65
65
  for (const mod of ssrEnv.moduleGraph.idToModuleMap.values()) {
66
66
  if (!(mod.file && /\.(css|scss|sass|less|styl|stylus)($|\?)/.test(mod.file))) continue;
67
- const url = mod.url.split("?", 1)[0];
67
+ const url = mod.url.split("?", 1)[0] + "?direct";
68
68
  try {
69
- const result = await clientEnv.transformRequest(url + "?direct");
69
+ const result = await clientEnv.transformRequest(url);
70
70
  if (result?.code) styles.add(result.code);
71
- } catch {}
71
+ } catch (e) {}
72
72
  }
73
73
  if (styles.size === 0) return html;
74
+ const cssContent = Array.from(styles).join("");
74
75
  const headEndIndex = html.lastIndexOf("</head>");
75
- const bodyEndIndex = html.lastIndexOf("</body>");
76
- let cssContent = "";
77
- for (const s of styles) cssContent += s;
78
- const inlineCss = `<style type="text/css" data-vite-dev-fouc>${cssContent}</style>`;
79
- 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)}`;
80
78
  }
81
79
  function routerPlugin({ pagesDir = "pages", serverEntry = "server/index.ts", apiPrefix = "/api" } = {}) {
82
80
  const isProd = process.env.NODE_ENV === "production";
@@ -175,11 +173,10 @@ function routerPlugin({ pagesDir = "pages", serverEntry = "server/index.ts", api
175
173
  const manifestPath = path.join(viteConfigRoot, "../dist/client/.vite/manifest.json");
176
174
  return `export default ${fs.readFileSync(manifestPath, "utf8")}`;
177
175
  }
178
- if (id === resolvedVirtualEntryClientId) return `
179
- import { routes, errorRoute } from '${virtualModuleId}';
176
+ if (id === resolvedVirtualEntryClientId) return `import { routes, errorRoute } from '${virtualModuleId}';
180
177
  import { onRenderClient } from '${virtualAdapterId}';
181
- onRenderClient().then((module) => { module.default({ routes, errorRoute })});
182
- `;
178
+ const { default: render } = await onRenderClient();
179
+ render({ routes, errorRoute });`;
183
180
  if (id === resolvedVirtualSetupId) return `
184
181
  import { routes, errorRoute, config } from '${virtualModuleId}';
185
182
  import { setVikeState } from 'vike-lite/__internal/server';
@@ -192,24 +189,15 @@ function routerPlugin({ pagesDir = "pages", serverEntry = "server/index.ts", api
192
189
  if (id === resolvedVirtualEntryServerId) {
193
190
  const normalizedServerEntry = path.join(viteConfigRoot, serverEntry).replaceAll("\\", "/");
194
191
  return `import '${virtualSetupId}';
195
- export * from '${normalizedServerEntry}'
196
- export { default } from '${normalizedServerEntry}'`;
192
+ export * from '${normalizedServerEntry}';
193
+ export { default } from '${normalizedServerEntry}';`;
197
194
  }
198
195
  },
199
196
  configureServer(server) {
200
197
  return () => {
201
198
  server.middlewares.use(async (req, res, next) => {
202
199
  try {
203
- const ssrEnv = server.environments.ssr;
204
- const { routes, errorRoute, config } = await ssrEnv.runner.import(virtualModuleId);
205
- const { setVikeState } = await ssrEnv.runner.import("vike-lite/__internal/server");
206
- setVikeState({
207
- routes,
208
- errorRoute,
209
- config
210
- });
211
- const absoluteServerEntry = path.join(viteConfigRoot, serverEntry);
212
- const { default: app } = await ssrEnv.runner.import(absoluteServerEntry);
200
+ const { default: app } = await server.environments.ssr.runner.import(resolvedVirtualEntryServerId);
213
201
  const headers = new Headers();
214
202
  for (const [key, value] of Object.entries(req.headers)) {
215
203
  if (key.startsWith(":")) continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike-lite",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "./dist/index.mjs",