|
1 | 1 | import * as crypto from "node:crypto";
|
2 |
| -import type { Plugin, ViteDevServer } from "vite"; |
| 2 | +import type { Plugin } from "vite"; |
3 | 3 | import type { Options } from "./index";
|
4 | 4 | import { getRenderer, navita, VIRTUAL_MODULE_ID } from "./index";
|
5 | 5 |
|
| 6 | +const remixServerBuildId = '\0virtual:remix/server-build'; |
6 | 7 | let cssFileName: string;
|
7 | 8 |
|
8 | 9 | export function navitaRemix(options?: Options): Plugin[] {
|
9 |
| - let server: ViteDevServer; |
| 10 | + let isProduction = false; |
10 | 11 |
|
11 | 12 | const { renderChunk, ...navitaVite } = navita(options);
|
12 | 13 |
|
13 | 14 | return [
|
14 | 15 | navitaVite,
|
15 | 16 | {
|
16 | 17 | name: 'navita-remix',
|
17 |
| - configureServer(_server) { |
18 |
| - server = _server; |
19 |
| - |
20 |
| - server.middlewares.use(async function middleware(_req, _res, next) { |
21 |
| - try { |
22 |
| - const build = await server.ssrLoadModule( |
23 |
| - 'virtual:remix/server-build', |
24 |
| - ); |
25 |
| - |
26 |
| - const { module } = build.routes.root; |
27 |
| - |
28 |
| - // We modify the root module, to automatically include the CSS |
29 |
| - // when running the dev server. |
30 |
| - build.routes.root.module = { |
31 |
| - ...module, |
32 |
| - links: () => [ |
33 |
| - ...module.links(), |
34 |
| - { rel: 'stylesheet', href: `/${VIRTUAL_MODULE_ID}` }, |
35 |
| - ], |
36 |
| - }; |
37 |
| - } catch(e) { |
38 |
| - console.error(e); |
39 |
| - } |
| 18 | + configResolved(config) { |
| 19 | + isProduction = config.mode === 'production'; |
| 20 | + }, |
| 21 | + transform(code, id) { |
| 22 | + if (isProduction || id !== remixServerBuildId) { |
| 23 | + return; |
| 24 | + } |
40 | 25 |
|
41 |
| - next(); |
42 |
| - }); |
| 26 | + return `${code}\n${remixServerBuildExtension}`; |
43 | 27 | },
|
44 | 28 | renderChunk(_, chunk) {
|
45 | 29 | if (chunk.name === "root") {
|
@@ -74,3 +58,13 @@ export function navitaRemix(options?: Options): Plugin[] {
|
74 | 58 | }
|
75 | 59 | ];
|
76 | 60 | }
|
| 61 | + |
| 62 | +const remixServerBuildExtension = ` |
| 63 | + routes.root.module = { |
| 64 | + ...route0, |
| 65 | + links: () => [ |
| 66 | + ...(route0.links ? route0.links() : []), |
| 67 | + { rel: 'stylesheet', href: '/${VIRTUAL_MODULE_ID}' }, |
| 68 | + ], |
| 69 | + }; |
| 70 | +`; |
0 commit comments