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
6 changes: 4 additions & 2 deletions src/plugins/prerender-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/named-chunks/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<script prerender type="module" src="/src/index.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions tests/fixtures/named-chunks/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import('./split-chunk.js');

export async function prerender() {
return `<h1>Simple Test Result</h1>`;
}
3 changes: 3 additions & 0 deletions tests/fixtures/named-chunks/src/split-chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
globalThis.splitChunk = function splitChunk() {
console.log('splitChunk');
}
6 changes: 6 additions & 0 deletions tests/fixtures/named-chunks/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'vite';
import { vitePrerenderPlugin } from 'vite-prerender-plugin';

export default defineConfig({
plugins: [vitePrerenderPlugin()],
});
29 changes: 29 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,33 @@ test('Should support comment nodes in returned HTML', async () => {
assert.match(prerenderedHtml, '<!-- With Input HTML Comment -->');
});

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();