Skip to content

Commit

Permalink
fix(vite-plugin-nitro): add config root fallback paths and update ren…
Browse files Browse the repository at this point in the history
…derer paths (#618)
  • Loading branch information
brandonroberts committed Aug 28, 2023
1 parent e1aa3c7 commit d07a566
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/vite-plugin-nitro/src/lib/build-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import * as path from 'path';
import { Options } from './options';

export async function buildSSRApp(config: UserConfig, options?: Options) {
const rootDir = config.root || '.';
const ssrBuildConfig = mergeConfig(config, {
build: {
ssr: true,
rollupOptions: {
input:
options?.entryServer ||
path.resolve(config.root!, './src/main.server.ts'),
options?.entryServer || path.resolve(rootDir, './src/main.server.ts'),
},
outDir: options?.ssrBuildDir || path.resolve('dist', config.root!, 'ssr'),
outDir: options?.ssrBuildDir || path.resolve('dist', rootDir, 'ssr'),
},
});

Expand Down
9 changes: 4 additions & 5 deletions packages/vite-plugin-nitro/src/lib/runtime/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
* which won't be parsed by Nitro correctly.
*/
import { eventHandler } from 'h3';
import { useStorage } from '#imports';

export default eventHandler(async (event) => {
const render = (await import('#build/../ssr/main.server.mjs'))['default'];
const template = await useStorage().getItem(`/assets/public:index.html`);
import renderer from '#analog/ssr';
import template from '#analog/index';

const html = await render(event.req.url, template);
export default eventHandler(async (event) => {
const html = await renderer(event.node.req.url, template);

return html;
});
4 changes: 4 additions & 0 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('nitro', () => {
{ ssr: true },
{
...mockNitroConfig,
alias: expect.anything(),
prerender: { routes: ['/'] },
rollupConfig: expect.anything(),
handlers: expect.anything(),
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('nitro', () => {
{
...mockNitroConfig,
prerender: { routes: ['/'] },
alias: expect.anything(),
rollupConfig: expect.anything(),
handlers: expect.anything(),
}
Expand Down Expand Up @@ -118,6 +120,7 @@ describe('nitro', () => {
{ ssr: true, ...prerenderRoutes },
{
...mockNitroConfig,
alias: expect.anything(),
rollupConfig: expect.anything(),
handlers: expect.anything(),
}
Expand Down Expand Up @@ -164,6 +167,7 @@ describe('nitro', () => {
prerender: {
routes: prerenderRoutes.prerender.routes,
},
alias: expect.anything(),
rollupConfig: expect.anything(),
handlers: expect.anything(),
}
Expand Down
15 changes: 14 additions & 1 deletion packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,22 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {

if (!ssrBuild && !isTest) {
// store the client output path for the SSR build config
clientOutputPath = path.resolve(rootDir, config.build?.outDir!);
clientOutputPath = path.resolve(
rootDir,
config.build?.outDir || 'dist/client'
);
}

nitroConfig.alias = {
'#analog/ssr': normalizePath(
path.resolve(workspaceRoot, 'dist', rootDir, 'ssr/main.server.mjs')
),
'#analog/index': normalizePath(
path.resolve(clientOutputPath, 'index.html')
),
...nitroOptions?.alias,
};

if (isBuild) {
if (isEmptyPrerenderRoutes(options)) {
nitroConfig.prerender = {};
Expand Down

0 comments on commit d07a566

Please sign in to comment.