Skip to content

Commit

Permalink
fix: use HmrContext.read instead of direct fs.readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 11, 2024
1 parent 89bfae5 commit 92e3f40
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ const getRouteManifestModuleExports = async (
const getRouteModuleExports = async (
viteChildCompiler: Vite.ViteDevServer | null,
pluginConfig: ResolvedRemixVitePluginConfig,
routeFile: string
routeFile: string,
read?: () => string | Promise<string>
): Promise<string[]> => {
if (!viteChildCompiler) {
throw new Error("Vite child compiler not found");
Expand All @@ -318,7 +319,7 @@ const getRouteModuleExports = async (

let [id, code] = await Promise.all([
resolveId(),
fse.readFile(routePath, "utf-8"),
read ? read() : fse.readFile(routePath, "utf-8"),
// pluginContainer.transform(...) fails if we don't do this first:
moduleGraph.ensureEntryFromUrl(url, ssr),
]);
Expand Down Expand Up @@ -1335,7 +1336,7 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
},
{
name: "remix-hmr-updates",
async handleHotUpdate({ server, file, modules }) {
async handleHotUpdate({ server, file, modules, read }) {
let route = getRoute(pluginConfig, file);

type ManifestRoute = Manifest["routes"][string];
Expand All @@ -1352,7 +1353,8 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
let newRouteMetadata = await getRouteMetadata(
pluginConfig,
viteChildCompiler,
route
route,
read
);

hmrEventData.route = newRouteMetadata;
Expand Down Expand Up @@ -1470,12 +1472,14 @@ function getRoute(
async function getRouteMetadata(
pluginConfig: ResolvedRemixVitePluginConfig,
viteChildCompiler: Vite.ViteDevServer | null,
route: ConfigRoute
route: ConfigRoute,
read: () => string | Promise<string>
) {
let sourceExports = await getRouteModuleExports(
viteChildCompiler,
pluginConfig,
route.file
route.file,
read,
);
if (sourceExports.length === 0) {
console.log("[getRouteMetadata:empty]", route.file);
Expand Down

0 comments on commit 92e3f40

Please sign in to comment.