Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/plugin/vite/spec/config/vite.base.config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,47 @@ describe('vite.base.config', () => {
expect(define1).toEqual(define2);
});

it('getBuildDefine:serve with custom server host', async () => {
const hosts = ['127.0.0.1', '0.0.0.0'];
const servers = await Promise.all(
forgeConfig.renderer.map(({ name }, index) =>
createServer({
publicDir: false,
server: { host: hosts[index] },
plugins: [pluginExposeRenderer(name)],
}),
),
);
let port = 5183;

for (const server of servers) {
await server.listen(port);
port++;
}

const define1 = getBuildDefine({
command: 'serve',
mode: 'development',
root: configRoot,
forgeConfig,
forgeConfigSelf: forgeConfig.build[0],
});
const define2 = {
// Custom string hosts are exposed as-is.
MAIN_WINDOW_VITE_DEV_SERVER_URL: '"http://127.0.0.1:5183"',
MAIN_WINDOW_VITE_NAME: '"main_window"',
// Wildcard hosts fall back to localhost.
SECOND_WINDOW_VITE_DEV_SERVER_URL: '"http://localhost:5184"',
SECOND_WINDOW_VITE_NAME: '"second_window"',
};

for (const server of servers) {
await server.close();
}

expect(define1).toEqual(define2);
});

describe('pluginHotRestart', () => {
let dispose: (() => void) | undefined;

Expand Down
16 changes: 15 additions & 1 deletion packages/plugin/vite/src/config/vite.base.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ export function getBuildDefine(env: ConfigEnv<'build'>) {
return define;
}

/**
* Resolve the host to use in the dev server URL exposed to the main process.
* Wildcard hosts (`true`, `0.0.0.0`, `::`) are mapped to `localhost` since the
* main process runs on the same machine and wildcard addresses are not
* reliably loadable in Chromium.
*/
function resolveDevHost(host: string | boolean | undefined): string {
if (typeof host !== 'string' || host === '0.0.0.0' || host === '::') {
return 'localhost';
}
// IPv6 literals must be bracketed in URLs.
return host.includes(':') ? `[${host}]` : host;
}

export function pluginExposeRenderer(name: string): Plugin {
const { VITE_DEV_SERVER_URL } = getDefineKeys([name])[name];

Expand All @@ -88,7 +102,7 @@ export function pluginExposeRenderer(name: string): Plugin {
const addressInfo = server.httpServer?.address() as AddressInfo;
// Expose env constant for main process use.
viteDevServerUrls[VITE_DEV_SERVER_URL] =
`http://localhost:${addressInfo?.port}`;
`http://${resolveDevHost(server.config.server.host)}:${addressInfo?.port}`;
});
},
};
Expand Down
Loading