Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remix: change middleware to transform #62

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-tools-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@navita/vite-plugin': patch
---

change from middleware to transform for compatability with Shopify Hydrogen
48 changes: 21 additions & 27 deletions packages/vite-plugin/src/remix.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
import * as crypto from "node:crypto";
import type { Plugin, ViteDevServer } from "vite";
import type { Plugin } from "vite";
import type { Options } from "./index";
import { getRenderer, navita, VIRTUAL_MODULE_ID } from "./index";

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

export function navitaRemix(options?: Options): Plugin[] {
let server: ViteDevServer;
let isProduction = false;

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

return [
navitaVite,
{
name: 'navita-remix',
configureServer(_server) {
server = _server;

server.middlewares.use(async function middleware(_req, _res, next) {
try {
const build = await server.ssrLoadModule(
'virtual:remix/server-build',
);

const { module } = build.routes.root;

// We modify the root module, to automatically include the CSS
// when running the dev server.
build.routes.root.module = {
...module,
links: () => [
...module.links(),
{ rel: 'stylesheet', href: `/${VIRTUAL_MODULE_ID}` },
],
};
} catch(e) {
console.error(e);
}
configResolved(config) {
isProduction = config.mode === 'production';
},
transform(code, id) {
if (isProduction || id !== remixServerBuildId) {
return;
}

next();
});
return `${code}\n${remixServerBuildExtension}`;
},
renderChunk(_, chunk) {
if (chunk.name === "root") {
Expand Down Expand Up @@ -74,3 +58,13 @@ export function navitaRemix(options?: Options): Plugin[] {
}
];
}

const remixServerBuildExtension = `
routes.root.module = {
...route0,
links: () => [
...(route0.links ? route0.links() : []),
{ rel: 'stylesheet', href: '/${VIRTUAL_MODULE_ID}' },
],
};
`;
Loading