Skip to content

Commit 1859baa

Browse files
authored
remix: change middleware to transform (#62)
* remove middleware in favor of transform * changeset
1 parent 9cf7922 commit 1859baa

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

.changeset/thick-tools-walk.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@navita/vite-plugin': patch
3+
---
4+
5+
change from middleware to transform for compatability with Shopify Hydrogen

packages/vite-plugin/src/remix.ts

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,29 @@
11
import * as crypto from "node:crypto";
2-
import type { Plugin, ViteDevServer } from "vite";
2+
import type { Plugin } from "vite";
33
import type { Options } from "./index";
44
import { getRenderer, navita, VIRTUAL_MODULE_ID } from "./index";
55

6+
const remixServerBuildId = '\0virtual:remix/server-build';
67
let cssFileName: string;
78

89
export function navitaRemix(options?: Options): Plugin[] {
9-
let server: ViteDevServer;
10+
let isProduction = false;
1011

1112
const { renderChunk, ...navitaVite } = navita(options);
1213

1314
return [
1415
navitaVite,
1516
{
1617
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+
}
4025

41-
next();
42-
});
26+
return `${code}\n${remixServerBuildExtension}`;
4327
},
4428
renderChunk(_, chunk) {
4529
if (chunk.name === "root") {
@@ -74,3 +58,13 @@ export function navitaRemix(options?: Options): Plugin[] {
7458
}
7559
];
7660
}
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

Comments
 (0)