Skip to content

Commit e2a7853

Browse files
authored
fix: Support custom output names (#13)
* fix: Support custom output names/paths * test: Add cases confirming behavior
1 parent 6ed657c commit e2a7853

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

src/plugins/prerender-plugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
303303
for (const output of Object.keys(bundle)) {
304304
if (!output.endsWith('.js') || bundle[output].type !== 'chunk') continue;
305305

306+
const assetPath = path.join(tmpDir, output);
307+
await fs.mkdir(path.dirname(assetPath), { recursive: true });
306308
await fs.writeFile(
307-
path.join(tmpDir, path.basename(output)),
309+
assetPath,
308310
/** @type {OutputChunk} */ (bundle[output]).code,
309311
);
310312

@@ -372,7 +374,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
372374
let prerender;
373375
try {
374376
const m = await import(
375-
`file://${path.join(tmpDir, path.basename(prerenderEntry.fileName))}`
377+
`file://${path.join(tmpDir, prerenderEntry.fileName)}`
376378
);
377379
prerender = m.prerender;
378380
} catch (e) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
</head>
6+
<body>
7+
<script prerender type="module" src="/src/index.js"></script>
8+
</body>
9+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import('./split-chunk.js');
2+
3+
export async function prerender() {
4+
return `<h1>Simple Test Result</h1>`;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
globalThis.splitChunk = function splitChunk() {
2+
console.log('splitChunk');
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from 'vite';
2+
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
3+
4+
export default defineConfig({
5+
plugins: [vitePrerenderPlugin()],
6+
});

tests/index.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,33 @@ test('Should support comment nodes in returned HTML', async () => {
7373
assert.match(prerenderedHtml, '<!-- With Input HTML Comment -->');
7474
});
7575

76+
test('Should support custom output filenames', async () => {
77+
await loadFixture('named-chunks', env);
78+
await writeConfig(env.tmp.path, `
79+
import { defineConfig } from 'vite';
80+
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
81+
82+
export default defineConfig({
83+
build: {
84+
rollupOptions: {
85+
output: {
86+
chunkFileNames: 'chunks/[name].[hash].js',
87+
}
88+
}
89+
},
90+
plugins: [vitePrerenderPlugin()],
91+
});
92+
`);
93+
await viteBuild(env.tmp.path);
94+
95+
let message = '';
96+
try {
97+
await viteBuild(env.tmp.path);
98+
} catch (error) {
99+
message = error.message;
100+
}
101+
102+
assert.match(message, '');
103+
});
104+
76105
test.run();

0 commit comments

Comments
 (0)