Repository: remix-run/react-router
File: packages/react-router-dev/vite/plugin.ts (TypeScript)

Code before:

            let reactRouterManifest = await getReactRouterManifestForDev();
            let reactRouterManifestString = jsesc(reactRouterManifest, {
              es6: true,
            });

            return `window.__remixManifest=${reactRouterManifestString};`;
          }
        }
      },
    },
    {
      name: "react-router-define-route",
      enforce: "pre",
      async transform(code, id, options) {
        if (id.endsWith(BUILD_CLIENT_ROUTE_QUERY_STRING)) return;
        if (!code.includes("defineRoute")) return; // temporary back compat

        if (options?.ssr) return; // TODO: why is `ctx` undefined otherwise?
        let route = getRoute(ctx.reactRouterConfig, id);
        if (!route) return defineRoute.assertNotImported(code);

        defineRoute.transform(code);
      },
    },
    {
      name: "react-router-dot-server",
      enforce: "pre",
      async resolveId(id, importer, options) {
        if (options?.ssr) return;

        let isResolving = options?.custom?.["react-router-dot-server"] ?? false;
        if (isResolving) return;
        options.custom = { ...options.custom, "react-router-dot-server": true };
        let resolved = await this.resolve(id, importer, options);
        if (!resolved) return;

        let serverFileRE = /\.server(\.[cm]?[jt]sx?)?$/;
        let serverDirRE = /\/\.server\//;
        let isDotServer =
          serverFileRE.test(resolved!.id) || serverDirRE.test(resolved!.id);
        if (!isDotServer) return;

        if (!importer) return;
        if (viteCommand !== "build" && importer.endsWith(".html")) {
          // Vite has a special `index.html` importer for `resolveId` within `transformRequest`
          // https://github.com/vitejs/vite/blob/5684fcd8d27110d098b3e1c19d851f44251588f1/packages/vite/src/node/server/transformRequest.ts#L158
          // https://github.com/vitejs/vite/blob/5684fcd8d27110d098b3e1c19d851f44251588f1/packages/vite/src/node/server/pluginContainer.ts#L668
          return;
        }


Review comment (suggestion):
In the next PR, `transform` will remove `serverLoader` + `serverAction` from the AST and then return the modified code to this Vite transform. In this PR, its just an assertion/compile-time check function

Code after:

            let reactRouterManifest = await getReactRouterManifestForDev();
            let reactRouterManifestString = jsesc(reactRouterManifest, {
              es6: true,
            });

            return `window.__remixManifest=${reactRouterManifestString};`;
          }
        }
      },
    },
    {
      name: "react-router-define-route",
      enforce: "pre",
      async transform(code, id, options) {
        if (options?.ssr) return;

        if (id.endsWith(BUILD_CLIENT_ROUTE_QUERY_STRING)) return;

        let route = getRoute(ctx.reactRouterConfig, id);
        if (!route && code.includes("defineRoute")) {
          return defineRoute.assertNotImported(code);
        }

        if (!code.includes("defineRoute")) return; // temporary back compat, remove once old style routes are unsupported
        defineRoute.transform(code);
      },
    },
    {
      name: "react-router-dot-server",
      enforce: "pre",
      async resolveId(id, importer, options) {
        if (options?.ssr) return;

        let isResolving = options?.custom?.["react-router-dot-server"] ?? false;
        if (isResolving) return;
        options.custom = { ...options.custom, "react-router-dot-server": true };
        let resolved = await this.resolve(id, importer, options);
        if (!resolved) return;

        let serverFileRE = /\.server(\.[cm]?[jt]sx?)?$/;
        let serverDirRE = /\/\.server\//;
        let isDotServer =
          serverFileRE.test(resolved!.id) || serverDirRE.test(resolved!.id);
        if (!isDotServer) return;

        if (!importer) return;
        if (viteCommand !== "build" && importer.endsWith(".html")) {
          // Vite has a special `index.html` importer for `resolveId` within `transformRequest`
          // https://github.com/vitejs/vite/blob/5684fcd8d27110d098b3e1c19d851f44251588f1/packages/vite/src/node/server/transformRequest.ts#L158
          // https://github.com/vitejs/vite/blob/5684fcd8d27110d098b3e1c19d851f44251588f1/packages/vite/src/node/server/pluginContainer.ts#L668