Skip to content

Commit

Permalink
fix(vite-plugin-nitro): inline renderer and template on Windows for p…
Browse files Browse the repository at this point in the history
…rerendering (#1041)
  • Loading branch information
brandonroberts authored Apr 17, 2024
1 parent e89cd18 commit ac81da1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ jobs:
run: pnpm install --frozen-lockfile --prefer-offline
- name: Build
run: pnpm build
- name: Verify
run: more dist\apps\blog-app\analog\public\index.html

unit:
runs-on: ubuntu-latest
Expand Down
21 changes: 0 additions & 21 deletions packages/vite-plugin-nitro/src/lib/runtime/renderer.mjs

This file was deleted.

35 changes: 35 additions & 0 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { normalizePath } from 'vite';
import { dirname, join, relative, resolve } from 'node:path';
import { platform } from 'node:os';
import { fileURLToPath } from 'node:url';
import { readFileSync, writeFileSync } from 'node:fs';

import { buildServer } from './build-server.js';
import { buildSSRApp } from './build-ssr.js';
Expand Down Expand Up @@ -212,6 +213,40 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
}

if (ssrBuild) {
if (isWindows) {
const indexContents = readFileSync(
normalizePath(join(clientOutputPath, 'index.html')),
'utf-8'
);

// Write out the renderer manually because
// Windows doesn't resolve the aliases
// correctly in its native environment
writeFileSync(
normalizePath(rendererEntry.replace(filePrefix, '')),
`
/**
* This file is shipped as ESM for Windows support,
* as it won't resolve the renderer.ts file correctly in node.
*/
import { eventHandler } from 'h3';
// @ts-ignore
import renderer from '${ssrEntry}';
// @ts-ignore
const template = \`${indexContents}\`;
export default eventHandler(async (event) => {
const html = await renderer(event.node.req.url, template, {
req: event.node.req,
res: event.node.res,
});
return html;
});
`
);
}

nitroConfig = {
...nitroConfig,
externals: {
Expand Down

0 comments on commit ac81da1

Please sign in to comment.