Skip to content

Commit 94eacca

Browse files
authored
fix: Bail out & skip plugin during Vite's SSR builds (#15)
* fix: Bail out & skip plugin during Vite's SSR builds * fix: Begin to support Vite's Environment API & plugins that use it * test: Expound upon environment tests
1 parent 5606ac4 commit 94eacca

File tree

15 files changed

+1240
-217
lines changed

15 files changed

+1240
-217
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
"stack-trace": "^1.0.0-pre2"
3838
},
3939
"devDependencies": {
40+
"@cloudflare/vite-plugin": "^1.0.8",
4041
"@types/node": "^20.11.16",
4142
"dedent": "^1.5.3",
4243
"magic-string": "^0.30.6",
4344
"prettier": "^2.8.8",
4445
"prettier-config-rschristian": "^0.1.1",
4546
"tmp-promise": "^3.0.3",
4647
"uvu": "^0.5.6",
47-
"vite": "^5.0.12"
48+
"vite": "^6.3.1"
4849
},
4950
"prettier": "prettier-config-rschristian"
5051
}

pnpm-lock.yaml

Lines changed: 1123 additions & 212 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/prerender-plugin.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function serializeElement(element) {
7474
export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrerenderRoutes } = {}) {
7575
let viteConfig = {};
7676
let userEnabledSourceMaps;
77+
let ssrBuild = false;
7778

7879
/** @type {import('./types.d.ts').PrerenderedRoute[]} */
7980
let routes = [];
@@ -124,9 +125,19 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
124125
name: 'vite-prerender-plugin',
125126
apply: 'build',
126127
enforce: 'post',
128+
applyToEnvironment(environment) {
129+
return environment.name == 'client';
130+
},
127131
// Vite is pretty inconsistent with how it resolves config options, both
128132
// hooks are needed to set their respective options. ¯\_(ツ)_/¯
129133
config(config) {
134+
// Only required for Vite 5 and older. In 6+, this is handled by the
135+
// Environment API (`applyToEnvironment`)
136+
if (config.build?.ssr) {
137+
ssrBuild = true
138+
return;
139+
}
140+
130141
userEnabledSourceMaps = !!config.build?.sourcemap;
131142

132143
if (!config.customLogger) {
@@ -161,6 +172,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
161172
config.build.sourcemap = true;
162173
},
163174
configResolved(config) {
175+
if (ssrBuild) return;
164176
// We're only going to alter the chunking behavior in the default cases, where the user and/or
165177
// other plugins haven't already configured this. It'd be impossible to avoid breakages otherwise.
166178
if (
@@ -181,7 +193,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
181193
viteConfig = config;
182194
},
183195
async options(opts) {
184-
if (!opts.input) return;
196+
if (ssrBuild || !opts.input) return;
185197
if (!prerenderScript) {
186198
prerenderScript = await getPrerenderScriptFromHTML(opts.input);
187199
}
@@ -197,6 +209,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
197209
},
198210
// Injects window checks into Vite's preload helper & modulepreload polyfill
199211
transform(code, id) {
212+
if (ssrBuild) return;
200213
if (id.includes(preloadHelperId)) {
201214
// Injects a window check into Vite's preload helper, instantly resolving
202215
// the module rather than attempting to add a <link> to the document.
@@ -238,6 +251,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
238251
}
239252
},
240253
async generateBundle(_opts, bundle) {
254+
if (ssrBuild) return;
241255
// @ts-ignore
242256
globalThis.location = {};
243257
// @ts-ignore

tests/environments.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
4+
import { setupTest, teardownTest, loadFixture, viteBuild, viteBuildCli } from './lib/lifecycle.js';
5+
6+
let env;
7+
test.before.each(async () => {
8+
env = await setupTest();
9+
});
10+
11+
test.after.each(async () => {
12+
await teardownTest(env);
13+
});
14+
15+
test('Should skip plugin during SSR build', async () => {
16+
await loadFixture('environments/ssr', env);
17+
18+
let message = '';
19+
try {
20+
await viteBuild(env.tmp.path);
21+
} catch (error) {
22+
message = error.message;
23+
}
24+
25+
assert.match(message, '');
26+
});
27+
28+
test('Should skip plugin during SSR build (CLI)', async () => {
29+
await loadFixture('simple', env);
30+
const output = await viteBuildCli(env.tmp.path, ['--ssr', 'src/index.js']);
31+
await output.done;
32+
33+
assert.equal(output.stderr, []);
34+
});
35+
36+
test('Should skip plugin during non-client environment build (Cloudflare plugin)', async () => {
37+
await loadFixture('environments/cloudflare', env);
38+
const output = await viteBuildCli(env.tmp.path);
39+
await output.done;
40+
41+
assert.equal(output.stderr, []);
42+
});
43+
44+
test.run();
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function prerender() {
2+
return `<h1>Simple Test Result</h1>`;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Server is running...");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'vite';
2+
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
3+
import { cloudflare } from '@cloudflare/vite-plugin';
4+
5+
console.log('Config is used');
6+
7+
export default defineConfig({
8+
plugins: [cloudflare(), vitePrerenderPlugin()],
9+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "my-worker"
2+
compatibility_date = "2023-03-01"
3+
4+
main = "src/server.js"
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>

0 commit comments

Comments
 (0)