Skip to content

Commit bfbb207

Browse files
RihanArfanpi0
andauthored
feat(vite): auto-detect client entry (#4059)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
1 parent 5ccf672 commit bfbb207

5 files changed

Lines changed: 48 additions & 38 deletions

File tree

examples/vite-ssr-preact/vite.config.mjs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,4 @@ import preact from "@preact/preset-vite";
44

55
export default defineConfig({
66
plugins: [nitro(), preact()],
7-
environments: {
8-
client: {
9-
build: {
10-
rollupOptions: {
11-
input: "./src/entry-client.tsx",
12-
},
13-
},
14-
},
15-
},
167
});

examples/vite-ssr-react/vite.config.mjs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@ import react from "@vitejs/plugin-react";
44

55
export default defineConfig({
66
plugins: [nitro(), react()],
7-
environments: {
8-
client: {
9-
build: { rollupOptions: { input: "./src/entry-client.tsx" } },
10-
},
11-
},
127
});

examples/vite-ssr-solid/vite.config.mjs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,4 @@ import { nitro } from "nitro/vite";
55
export default defineConfig({
66
plugins: [solid({ ssr: true }), nitro()],
77
esbuild: { jsx: "preserve", jsxImportSource: "solid-js" },
8-
environments: {
9-
ssr: {
10-
build: { rollupOptions: { input: "./src/entry-server.tsx" } },
11-
},
12-
client: {
13-
build: { rollupOptions: { input: "./src/entry-client.tsx" } },
14-
},
15-
},
168
});

examples/vite-ssr-vue-router/vite.config.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import { nitro } from "nitro/vite";
55

66
export default defineConfig((_env) => ({
77
plugins: [patchVueExclude(vue(), /\?assets/), devtoolsJson(), nitro()],
8-
environments: {
9-
client: { build: { rollupOptions: { input: "./app/entry-client.ts" } } },
10-
ssr: { build: { rollupOptions: { input: "./app/entry-server.ts" } } },
11-
},
128
}));
139

1410
// Workaround https://github.com/vitejs/vite-plugin-vue/issues/677

src/build/vite/plugin.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,50 @@ function nitroEnv(ctx: NitroPluginContext): VitePlugin {
9696
...createServiceEnvironments(ctx),
9797
nitro: createNitroEnvironment(ctx),
9898
};
99-
environments.client = {
100-
consumer: userConfig.environments?.client?.consumer ?? "client",
101-
build: {
102-
rollupOptions: {
103-
input:
104-
userConfig.environments?.client?.build?.rollupOptions?.input ??
105-
useNitro(ctx).options.renderer?.template,
106-
},
107-
},
108-
};
109-
debug("[env] Environments:", Object.keys(environments).join(", "));
99+
100+
let clientEntry: string | undefined;
101+
let clientEntryConfigured = !!getEntry(
102+
userConfig.environments?.client?.build?.rolldownOptions?.input ||
103+
userConfig.environments?.client?.build?.rollupOptions?.input
104+
);
105+
if (!clientEntryConfigured) {
106+
const rendererTemplate = useNitro(ctx).options.renderer?.template;
107+
if (rendererTemplate) {
108+
// Use Nitro renderer template as client entry
109+
clientEntry = rendererTemplate;
110+
ctx.nitro!.logger.info(
111+
`Using Nitro renderer template \`${prettyPath(rendererTemplate)}\` as vite client entry.`
112+
);
113+
} else {
114+
// Auto-detect client entry
115+
clientEntry = resolveModulePath("./entry-client", {
116+
try: true,
117+
extensions: DEFAULT_EXTENSIONS,
118+
from: ["app", "src", ""].flatMap((d) =>
119+
[ctx.nitro!.options.rootDir, ...ctx.nitro!.options.scanDirs].map(
120+
(s) => join(s, d) + "/"
121+
)
122+
),
123+
});
124+
if (clientEntry) {
125+
ctx.nitro!.logger.info(`Using \`${prettyPath(clientEntry)}\` as vite client entry.`);
126+
}
127+
}
128+
}
129+
if (clientEntry) {
130+
environments.client = {
131+
consumer: userConfig.environments?.client?.consumer ?? "client",
132+
build: clientEntryConfigured
133+
? undefined
134+
: {
135+
rollupOptions: {
136+
input: clientEntry ? { index: clientEntry } : undefined,
137+
},
138+
},
139+
};
140+
debug("[env] Environments:", Object.keys(environments).join(", "));
141+
}
142+
110143
return {
111144
environments,
112145
};
@@ -380,7 +413,10 @@ async function setupNitroContext(
380413
ctx.nitro!.logger.info(`Using \`${prettyPath(ssrEntry)}\` as vite ssr entry.`);
381414
}
382415
} else {
383-
let ssrEntry = getEntry(userConfig.environments.ssr.build?.rollupOptions?.input);
416+
let ssrEntry = getEntry(
417+
userConfig.environments.ssr.build?.rolldownOptions?.input ||
418+
userConfig.environments.ssr.build?.rollupOptions?.input
419+
);
384420
if (typeof ssrEntry === "string") {
385421
ssrEntry =
386422
resolveModulePath(ssrEntry, {

0 commit comments

Comments
 (0)