diff --git a/.changeset/curly-cherries-tell.md b/.changeset/curly-cherries-tell.md new file mode 100644 index 0000000..08f9f3e --- /dev/null +++ b/.changeset/curly-cherries-tell.md @@ -0,0 +1,5 @@ +--- +'@hono/vite-ssg': patch +--- + +Preserve the SSR server instance during page rendering diff --git a/packages/ssg/src/ssg.ts b/packages/ssg/src/ssg.ts index cae6b9a..939e4bf 100644 --- a/packages/ssg/src/ssg.ts +++ b/packages/ssg/src/ssg.ts @@ -56,7 +56,6 @@ export const ssgBuild = (options?: SSGOptions): Plugin => { build: { ssr: true }, }) const module = await server.ssrLoadModule(entry) - server.close() const app = module['default'] as Hono @@ -84,6 +83,8 @@ export const ssgBuild = (options?: SSGOptions): Plugin => { { dir: outDir } ) + server.close() + if (!result.success) { throw result.error } diff --git a/packages/ssg/test/app.ts b/packages/ssg/test/app.ts index acd0ffc..71da5a5 100644 --- a/packages/ssg/test/app.ts +++ b/packages/ssg/test/app.ts @@ -6,4 +6,10 @@ app.get('/', (c) => { return c.html('

Hello!

') }) +app.get('/dynamic-import', async (c) => { + // @ts-expect-error + const module = await import('./sample.js') + return c.text('Dynamic import works: ' + module.default) +}) + export default app diff --git a/packages/ssg/test/sample.js b/packages/ssg/test/sample.js new file mode 100644 index 0000000..cef26fb --- /dev/null +++ b/packages/ssg/test/sample.js @@ -0,0 +1 @@ +export default "sample!"; diff --git a/packages/ssg/test/ssg.test.ts b/packages/ssg/test/ssg.test.ts index 6673de2..c9a0cae 100644 --- a/packages/ssg/test/ssg.test.ts +++ b/packages/ssg/test/ssg.test.ts @@ -9,6 +9,7 @@ describe('ssgPlugin', () => { const entryFile = './test/app.ts' const outDir = path.resolve(testDir, 'dist') const outputFile = path.resolve(outDir, 'index.html') + const outputFileWithDynamicImport = path.resolve(outDir, 'dynamic-import.txt') beforeAll(() => { fs.mkdirSync(testDir, { recursive: true }) @@ -38,6 +39,11 @@ describe('ssgPlugin', () => { const output = fs.readFileSync(outputFile, 'utf-8') expect(output).toBe('

Hello!

') + expect(fs.existsSync(outputFileWithDynamicImport)).toBe(true) + + const outputDynamicImport = fs.readFileSync(outputFileWithDynamicImport, 'utf-8') + expect(outputDynamicImport).toBe('Dynamic import works: sample!') + // Should not output files corresponding to a virtual entry expect(fs.existsSync(path.resolve(outDir, 'assets'))).toBe(false) })