Skip to content

Commit

Permalink
refactor: move code
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jul 2, 2024
1 parent 1c03847 commit 8616d44
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 66 deletions.
34 changes: 2 additions & 32 deletions packages/react-server-next/src/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
import { vitePluginReactServer } from "@hiogawa/react-server/plugin";
import {
vitePluginLogger,
vitePluginSsrMiddleware,
} from "@hiogawa/vite-plugin-ssr-middleware";
import react from "@vitejs/plugin-react";
import type { Plugin, PluginOption } from "vite";
import type { PluginOption } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import { type AdapterType, adapterPlugin } from "./adapters";
import { appFaviconPlugin } from "./meta";

export default function vitePluginReactServerNext(options?: {
plugins?: PluginOption[];
Expand Down Expand Up @@ -57,33 +57,3 @@ export default function vitePluginReactServerNext(options?: {
},
];
}

/** @todo https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons */
function appFaviconPlugin(): Plugin {
// not sure what exactly Next.js does.
// for now, let's do quick workaround for app/favicon.ico
return {
name: appFaviconPlugin.name,
apply: (_config, env) => env.command === "serve" || !env.isSsrBuild,
configureServer(server) {
if (existsSync("app/favicon.ico")) {
server.middlewares.use((req, _res, next) => {
const url = new URL(req.url || "", "https://tmp.local");
if (url.pathname === "/favicon.ico") {
req.url = "/app/favicon.ico";
}
next();
});
}
},
generateBundle() {
if (existsSync("app/favicon.ico")) {
this.emitFile({
type: "asset",
fileName: "favicon.ico",
source: readFileSync("app/favicon.ico"),
});
}
},
};
}
32 changes: 32 additions & 0 deletions packages/react-server-next/src/vite/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { existsSync, readFileSync } from "fs";
import type { Plugin } from "vite";

/** @todo https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons */
export function appFaviconPlugin(): Plugin {
// not sure what exactly Next.js does.
// for now, let's do quick workaround for app/favicon.ico
return {
name: appFaviconPlugin.name,
apply: (_config, env) => env.command === "serve" || !env.isSsrBuild,
configureServer(server) {
if (existsSync("app/favicon.ico")) {
server.middlewares.use((req, _res, next) => {
const url = new URL(req.url || "", "https://tmp.local");
if (url.pathname === "/favicon.ico") {
req.url = "/app/favicon.ico";
}
next();
});
}
},
generateBundle() {
if (existsSync("app/favicon.ico")) {
this.emitFile({
type: "asset",
fileName: "favicon.ico",
source: readFileSync("app/favicon.ico"),
});
}
},
};
}
41 changes: 7 additions & 34 deletions packages/react-server/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
ENTRY_CLIENT_WRAPPER,
ENTRY_REACT_SERVER_WRAPPER,
createVirtualPlugin,
validateImportPlugin,
vitePluginSilenceDirectiveBuildWarning,
} from "./utils";

Expand Down Expand Up @@ -192,7 +193,9 @@ export function vitePluginReactServer(options?: {
),

validateImportPlugin({
"client-only": `'client-only' is included in server build`,
"client-only":
manager.buildType === "scan" ||
`'client-only' is included in server build`,
"server-only": true,
}),

Expand Down Expand Up @@ -412,7 +415,9 @@ export function vitePluginReactServer(options?: {
: []),
validateImportPlugin({
"client-only": true,
"server-only": `'server-only' is included in client build`,
"server-only":
manager.buildType === "scan" ||
`'server-only' is included in client build`,
}),
createVirtualPlugin(ENTRY_CLIENT_WRAPPER.slice("virtual:".length), () => {
// dev
Expand All @@ -439,35 +444,3 @@ export function vitePluginReactServer(options?: {
}),
];
}

// https://github.com/vercel/next.js/blob/90f564d376153fe0b5808eab7b83665ee5e08aaf/packages/next/src/build/webpack-config.ts#L1249-L1280
// https://github.com/pcattori/vite-env-only/blob/68a0cc8546b9a37c181c0b0a025eb9b62dbedd09/src/deny-imports.ts
// https://github.com/sveltejs/kit/blob/84298477a014ec471839adf7a4448d91bc7949e4/packages/kit/src/exports/vite/index.js#L513
function validateImportPlugin(entries: Record<string, string | true>): Plugin {
return {
name: validateImportPlugin.name,
enforce: "pre",
resolveId(source, importer, options) {
const entry = entries[source];
if (entry) {
// skip validation during optimizeDeps scan since for now
// we want to allow going through server/client boundary loosely
if (
entry === true ||
manager.buildType === "scan" ||
("scan" in options && options.scan)
) {
return "\0virtual:validate-import";
}
throw new Error(entry + ` (importer: ${importer ?? "unknown"})`);
}
return;
},
load(id, _options) {
if (id === "\0virtual:validate-import") {
return "export {}";
}
return;
},
};
}
30 changes: 30 additions & 0 deletions packages/react-server/src/plugin/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,33 @@ export function vitePluginSilenceDirectiveBuildWarning(): Plugin {
},
};
}

// https://github.com/vercel/next.js/blob/90f564d376153fe0b5808eab7b83665ee5e08aaf/packages/next/src/build/webpack-config.ts#L1249-L1280
// https://github.com/pcattori/vite-env-only/blob/68a0cc8546b9a37c181c0b0a025eb9b62dbedd09/src/deny-imports.ts
// https://github.com/sveltejs/kit/blob/84298477a014ec471839adf7a4448d91bc7949e4/packages/kit/src/exports/vite/index.js#L513
export function validateImportPlugin(
entries: Record<string, string | true>,
): Plugin {
return {
name: validateImportPlugin.name,
enforce: "pre",
resolveId(source, importer, options) {
const entry = entries[source];
if (entry) {
// skip validation during optimizeDeps scan since for now
// we want to allow going through server/client boundary loosely
if (entry === true || ("scan" in options && options.scan)) {
return "\0virtual:validate-import";
}
throw new Error(entry + ` (importer: ${importer ?? "unknown"})`);
}
return;
},
load(id, _options) {
if (id === "\0virtual:validate-import") {
return "export {}";
}
return;
},
};
}

0 comments on commit 8616d44

Please sign in to comment.