Skip to content

Commit fa38fd6

Browse files
authored
fix(fullstack): fix asEntry option (#1174)
1 parent 6499543 commit fa38fd6

File tree

6 files changed

+59
-37
lines changed

6 files changed

+59
-37
lines changed

packages/fullstack/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ This is a proposal to introduce a new API to allow non-client environment to acc
77
Currently, it is prototyped in my package `@hiogawa/vite-plugin-fullstack` and it provides `import.meta.vite.assets` function with a following signature:
88

99
```ts
10-
function assets({ import?: string, environment?: string }): {
10+
function assets({
11+
import?: string,
12+
environment?: string,
13+
asEntry?: boolean,
14+
}): {
1115
entry?: string; // script for <script type="module" src=...>
1216
js: { href: string, ... }[]; // dependency chunks for <link rel="modulepreload" href=... />
1317
css: { href: string, ... }[]; // dependency css for <link rel="stylesheet" href=... />
@@ -24,6 +28,7 @@ function renderHtml() {
2428
const assets = import.meta.vite.assets({
2529
entry: "./client.js",
2630
environment: "client",
31+
asEntry: true,
2732
});
2833
const head = `
2934
<script type="module" src=${JSON.stringify(assets.entry)}></script>
@@ -99,11 +104,6 @@ export default defineConfig({
99104
client: {
100105
build: {
101106
outDir: "./dist/client",
102-
rollupOptions: {
103-
input: {
104-
index: "./src/entry.client.tsx",
105-
},
106-
},
107107
},
108108
},
109109
ssr: {

packages/fullstack/examples/basic/src/entry.server.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,11 @@ async function handler(_request: Request): Promise<Response> {
1111
}
1212

1313
function Root() {
14-
// Impot client entry assets on server.
15-
// It doesn't support dynamically adding build entry,
16-
// so `build.rollupOption.input` still needs to be manually written.
17-
// This will include client js entry and its dependencies.
18-
// > { entry: string, js: { href: string, ... }[], css: { href: string, ... }[] }
1914
const clientAssets = import.meta.vite.assets({
2015
import: "./entry.client.tsx",
2116
environment: "client",
17+
asEntry: true,
2218
});
23-
24-
// By default, `import` and `environment` are inferred as
25-
// current module and current environment, which in this case is,
26-
// > { import: "./entry.server.tsx", environment: "ssr" }
27-
// This will include only server css assets.
28-
// > { css: { href: string, ... }[] }
2919
const serverAssets = import.meta.vite.assets();
3020
const assets = mergeAssets(clientAssets, serverAssets);
3121

packages/fullstack/examples/basic/vite.config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ export default defineConfig((_env) => ({
1818
client: {
1919
build: {
2020
outDir: "./dist/client",
21-
rollupOptions: {
22-
input: {
23-
index: "./src/entry.client.tsx",
24-
},
25-
},
21+
},
22+
// excplit rollupOptions.input is not necessary,
23+
// but `optimizeDeps.entries` is desired to set.
24+
optimizeDeps: {
25+
entries: ["./src/entry.client.tsx"],
2626
},
2727
},
2828
ssr: {
@@ -39,8 +39,8 @@ export default defineConfig((_env) => ({
3939
},
4040
builder: {
4141
async buildApp(builder) {
42-
// NOTE:
43-
// dynamically adding entry is supported only when building ssr -> client.
42+
// NOTE
43+
// dynamically adding entry is supported only when building ssr -> client
4444
await builder.build(builder.environments["ssr"]!);
4545
await builder.build(builder.environments["client"]!);
4646
},

packages/fullstack/src/plugin.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
toAssetsVirtual,
2222
} from "./plugins/shared";
2323
import {
24+
createVirtualPlugin,
2425
getEntrySource,
2526
hashString,
2627
normalizeRelativePath,
@@ -31,7 +32,14 @@ import {
3132
} from "./plugins/vite-utils";
3233

3334
type FullstackPluginOptions = {
35+
/**
36+
* @default true
37+
*/
3438
serverHandler?: boolean;
39+
// /**
40+
// * @default ["ssr"]
41+
// */
42+
// serverEnvironments?: string[];
3543
};
3644

3745
type ImportAssetsMeta = {
@@ -53,13 +61,13 @@ export function serverHandlerPlugin(
5361
return [
5462
{
5563
name: "fullstack:server-handler",
64+
apply: () => pluginOpts?.serverHandler !== false,
5665
config(userConfig, _env) {
5766
return {
5867
appType: userConfig.appType ?? "custom",
5968
};
6069
},
6170
configureServer(server) {
62-
if (pluginOpts?.serverHandler === false) return;
6371
assert(isRunnableDevEnvironment(server.environments.ssr));
6472
const environment = server.environments.ssr;
6573
const runner = environment.runner;
@@ -138,7 +146,7 @@ export function assetsPlugin(_pluginOpts?: FullstackPluginOptions): Plugin[] {
138146
const options: Required<ImportAssetsOptions> = {
139147
import: id,
140148
environment: this.environment.name,
141-
clientEntry: false,
149+
asEntry: false,
142150
universal: false,
143151
};
144152
if (argCode) {
@@ -150,7 +158,7 @@ export function assetsPlugin(_pluginOpts?: FullstackPluginOptions): Plugin[] {
150158
import: options.import,
151159
importer: id,
152160
environment: options.environment,
153-
entry: options.clientEntry ? "1" : "",
161+
entry: options.asEntry ? "1" : "",
154162
});
155163
const hash = hashString(importSource);
156164
const importedName = `__assets_${hash}`;
@@ -262,9 +270,6 @@ export function assetsPlugin(_pluginOpts?: FullstackPluginOptions): Plugin[] {
262270
},
263271
buildStart() {
264272
// dynamically add client entry during build
265-
// TODO: this still requires having at least one `rollupOptions.input`
266-
// since Vite expects `index.html` by default
267-
// (just add empty virtual by default)
268273
if (
269274
this.environment.mode == "build" &&
270275
this.environment.name === "client"
@@ -347,6 +352,29 @@ export function assetsPlugin(_pluginOpts?: FullstackPluginOptions): Plugin[] {
347352
},
348353
},
349354
},
355+
// ensure at least one client build input to prevent Vite
356+
// from looking for index.html and breaking build
357+
{
358+
...createVirtualPlugin("fullstack/client-fallback", () => "export {}"),
359+
configEnvironment: {
360+
order: "post",
361+
handler(name, config, _env) {
362+
if (name === "client") {
363+
if (!config.build?.rollupOptions?.input) {
364+
return {
365+
build: {
366+
rollupOptions: {
367+
input: {
368+
__fallback: "virtual:fullstack/client-fallback",
369+
},
370+
},
371+
},
372+
};
373+
}
374+
}
375+
},
376+
},
377+
},
350378
patchViteClientPlugin(),
351379
patchVueScopeCssHmr(),
352380
];

packages/fullstack/src/plugins/utils.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ export function createVirtualPlugin(
4444
name = "virtual:" + name;
4545
return {
4646
name: `rsc:virtual-${name}`,
47-
resolveId(source, _importer, _options) {
48-
return source === name ? "\0" + name : undefined;
47+
resolveId: {
48+
handler(source, _importer, _options) {
49+
return source === name ? "\0" + name : undefined;
50+
},
4951
},
50-
load(id, options) {
51-
if (id === "\0" + name) {
52-
return (load as Function).apply(this, [id, options]);
53-
}
52+
load: {
53+
handler(id, options) {
54+
if (id === "\0" + name) {
55+
return (load as Function).apply(this, [id, options]);
56+
}
57+
},
5458
},
5559
};
5660
}

packages/fullstack/types/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ImportAssetsOptions = {
1414
import?: string;
1515
environment?: string; // TODO: can remove?
1616
universal?: boolean;
17-
clientEntry?: boolean; // TODO: dynamically add entry during build
17+
asEntry?: boolean;
1818
};
1919

2020
export type ImportAssetsResult = {

0 commit comments

Comments
 (0)