diff --git a/src/plugins/prerender-plugin.js b/src/plugins/prerender-plugin.js index a4c2101..5b907de 100644 --- a/src/plugins/prerender-plugin.js +++ b/src/plugins/prerender-plugin.js @@ -303,8 +303,10 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere for (const output of Object.keys(bundle)) { if (!output.endsWith('.js') || bundle[output].type !== 'chunk') continue; + const assetPath = path.join(tmpDir, output); + await fs.mkdir(path.dirname(assetPath), { recursive: true }); await fs.writeFile( - path.join(tmpDir, path.basename(output)), + assetPath, /** @type {OutputChunk} */ (bundle[output]).code, ); @@ -372,7 +374,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere let prerender; try { const m = await import( - `file://${path.join(tmpDir, path.basename(prerenderEntry.fileName))}` + `file://${path.join(tmpDir, prerenderEntry.fileName)}` ); prerender = m.prerender; } catch (e) { diff --git a/tests/fixtures/named-chunks/index.html b/tests/fixtures/named-chunks/index.html new file mode 100644 index 0000000..3d387f8 --- /dev/null +++ b/tests/fixtures/named-chunks/index.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tests/fixtures/named-chunks/src/index.js b/tests/fixtures/named-chunks/src/index.js new file mode 100644 index 0000000..1ac8dcb --- /dev/null +++ b/tests/fixtures/named-chunks/src/index.js @@ -0,0 +1,5 @@ +import('./split-chunk.js'); + +export async function prerender() { + return `

Simple Test Result

`; +} diff --git a/tests/fixtures/named-chunks/src/split-chunk.js b/tests/fixtures/named-chunks/src/split-chunk.js new file mode 100644 index 0000000..5953f88 --- /dev/null +++ b/tests/fixtures/named-chunks/src/split-chunk.js @@ -0,0 +1,3 @@ +globalThis.splitChunk = function splitChunk() { + console.log('splitChunk'); +} diff --git a/tests/fixtures/named-chunks/vite.config.js b/tests/fixtures/named-chunks/vite.config.js new file mode 100644 index 0000000..4f19296 --- /dev/null +++ b/tests/fixtures/named-chunks/vite.config.js @@ -0,0 +1,6 @@ +import { defineConfig } from 'vite'; +import { vitePrerenderPlugin } from 'vite-prerender-plugin'; + +export default defineConfig({ + plugins: [vitePrerenderPlugin()], +}); diff --git a/tests/index.test.js b/tests/index.test.js index f5fa7a6..ac60293 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -73,4 +73,33 @@ test('Should support comment nodes in returned HTML', async () => { assert.match(prerenderedHtml, ''); }); +test('Should support custom output filenames', async () => { + await loadFixture('named-chunks', env); + await writeConfig(env.tmp.path, ` + import { defineConfig } from 'vite'; + import { vitePrerenderPlugin } from 'vite-prerender-plugin'; + + export default defineConfig({ + build: { + rollupOptions: { + output: { + chunkFileNames: 'chunks/[name].[hash].js', + } + } + }, + plugins: [vitePrerenderPlugin()], + }); + `); + await viteBuild(env.tmp.path); + + let message = ''; + try { + await viteBuild(env.tmp.path); + } catch (error) { + message = error.message; + } + + assert.match(message, ''); +}); + test.run();