Skip to content

Commit ab14d10

Browse files
authored
refactor(vite): replace old environment APIs (#3634)
I was perusing the code whilst investigating #3606 and noticed that, despite the release post claiming that Fresh had ["fully embrace[d]"](https://deno.com/blog/fresh-and-vite#:~:text=fully%20embraces) the Vite Environment API, it was still using `ssrLoadModule`. Once I started looking, I noticed a bunch of other code that wasn't future-proof (env-wise, not rolldown-wise, which'll bring it's own swath of deprecations[^1]), so I updated them as well. The newly added source map tests from #3624 currently fail again because the ModuleRunner API at the heart of the Environment API relies on [`process.setSourceMapsEnabled`](https://docs.deno.com/api/node/process/~/Process.setSourceMapsEnabled), which is [currently set to no-op](denoland/deno#22993) in Deno. [^1]: Separately tried to try out rolldown-vite but it seems that Deno doesn't support aliasing dependencies whatsoever? (I know there's not official support, but I was going to try to edit the lockfile to hack around it; however, it seems that there's not alias support in there at all?)
1 parent 0acebce commit ab14d10

File tree

14 files changed

+71
-62
lines changed

14 files changed

+71
-62
lines changed

deno.lock

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

packages/plugin-vite/demo/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export default defineConfig({
1111
}),
1212
tailwind(),
1313
],
14+
future: "warn",
1415
});

packages/plugin-vite/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@babel/preset-react": "npm:@babel/preset-react@^7.27.1",
2626
"@deno/loader": "jsr:@deno/loader@^0.3.10",
2727
"@marvinh-test/import-json": "jsr:@marvinh-test/import-json@^0.0.1",
28-
"@mjackson/node-fetch-server": "npm:@mjackson/node-fetch-server@^0.7.0",
28+
"@remix-run/node-fetch-server": "npm:@remix-run/node-fetch-server@^0.12.0",
2929
"@prefresh/vite": "npm:@prefresh/vite@^2.4.8",
3030
"@radix-ui/themes": "npm:@radix-ui/themes@^3.2.1",
3131
"@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12",

packages/plugin-vite/src/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,6 @@ async function loadEnvFile(envPath: string) {
256256
try {
257257
await stdLoadEnv({ envPath, export: true });
258258
} catch {
259-
// Ignoe
259+
// Ignore
260260
}
261261
}

packages/plugin-vite/src/plugins/client_entry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
1515
isDev = env.command === "serve";
1616
},
1717
applyToEnvironment(env) {
18-
return env.name === "client";
18+
return env.config.consumer === "client";
1919
},
2020
configResolved(config) {
2121
clientEntry = pathWithRoot(options.clientEntry, config.root);

packages/plugin-vite/src/plugins/client_snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function clientSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
1717
name: "fresh:client-snapshot",
1818
sharedDuringBuild: true,
1919
applyToEnvironment(env) {
20-
return env.name === "client";
20+
return env.config.consumer === "client";
2121
},
2222

2323
async config(cfg, env) {

packages/plugin-vite/src/plugins/deno.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export function deno(): Plugin {
6464
external: true,
6565
};
6666
}
67-
const loader = options?.ssr ? ssrLoader : browserLoader;
67+
const loader = this.environment.config.consumer === "server"
68+
? ssrLoader
69+
: browserLoader;
6870

6971
const original = id;
7072

@@ -154,8 +156,10 @@ export function deno(): Plugin {
154156
// ignore
155157
}
156158
},
157-
async load(id, options) {
158-
const loader = options?.ssr ? ssrLoader : browserLoader;
159+
async load(id) {
160+
const loader = this.environment.config.consumer === "server"
161+
? ssrLoader
162+
: browserLoader;
159163

160164
if (isDenoSpecifier(id)) {
161165
const { type, specifier } = parseDenoSpecifier(id);
@@ -168,7 +172,7 @@ export function deno(): Plugin {
168172
const code = new TextDecoder().decode(result.code);
169173

170174
const maybeJsx = babelTransform({
171-
ssr: !!options?.ssr,
175+
ssr: this.environment.config.consumer === "server",
172176
media: result.mediaType,
173177
code,
174178
id: specifier,
@@ -212,7 +216,7 @@ export function deno(): Plugin {
212216
const code = new TextDecoder().decode(result.code);
213217

214218
const maybeJsx = babelTransform({
215-
ssr: !!options?.ssr,
219+
ssr: this.environment.config.consumer === "server",
216220
media: result.mediaType,
217221
id,
218222
code,
@@ -230,10 +234,10 @@ export function deno(): Plugin {
230234
filter: {
231235
id: JSX_REG,
232236
},
233-
async handler(_, id, options) {
237+
async handler(_, id) {
234238
// This transform is a hack to be able to re-use Deno's precompile
235239
// jsx transform.
236-
if (!options?.ssr) {
240+
if (this.environment.name === "client") {
237241
return;
238242
}
239243

packages/plugin-vite/src/plugins/dev_server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { DevEnvironment, Plugin } from "vite";
22
import * as path from "@std/path";
33
import { ASSET_CACHE_BUST_KEY } from "fresh/internal";
4-
import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
4+
import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
55
import { hashCode } from "../shared.ts";
66

77
export function devServer(): Plugin[] {
@@ -118,7 +118,7 @@ export function devServer(): Plugin[] {
118118
{
119119
name: "fresh:server_hmr",
120120
applyToEnvironment(env) {
121-
return env.name === "ssr";
121+
return env.config.consumer === "server";
122122
},
123123
hotUpdate(options) {
124124
const clientMod = options.server.environments.client.moduleGraph

packages/plugin-vite/src/plugins/patches.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export function patches(): Plugin {
2626
filter: {
2727
id: JS_REG,
2828
},
29-
handler(code, id, options) {
29+
handler(code, id) {
3030
const presets = [];
31-
if (!options?.ssr && JSX_REG.test(id)) {
31+
if (this.environment.config.consumer === "client" && JSX_REG.test(id)) {
3232
presets.push([babelReact, {
3333
runtime: "automatic",
3434
importSource: "preact",
@@ -40,7 +40,7 @@ export function patches(): Plugin {
4040
const env = isDev ? "development" : "production";
4141

4242
const plugins: babel.PluginItem[] = [
43-
codeEvalPlugin(options?.ssr ? "ssr" : "client", env),
43+
codeEvalPlugin(this.environment.config.consumer, env),
4444
cjsPlugin,
4545
removePolyfills,
4646
jsxComments,

packages/plugin-vite/src/plugins/patches/code_eval.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { PluginObj, PluginPass, types } from "@babel/core";
33
const APPLY_PG_QUIRKS = "applyPgQuirks";
44

55
export function codeEvalPlugin(
6-
env: "ssr" | "client",
6+
env: "server" | "client",
77
mode: string,
88
) {
99
return (
@@ -41,7 +41,7 @@ export function codeEvalPlugin(
4141

4242
function evaluateExpr(
4343
t: typeof types,
44-
env: "client" | "ssr",
44+
env: "server" | "client",
4545
mode: string,
4646
node: types.Node,
4747
state: PluginPass,
@@ -76,9 +76,9 @@ function evaluateExpr(
7676
node.right.value === "undefined"
7777
) {
7878
if (node.operator === "==" || node.operator === "===") {
79-
return env !== "ssr";
79+
return env !== "server";
8080
} else if (node.operator === "!=" || node.operator === "!==") {
81-
return env === "ssr";
81+
return env === "server";
8282
}
8383
} else if (
8484
// Workaround for npm:pg
@@ -125,7 +125,7 @@ function evaluateExpr(
125125
if (result !== null) return result;
126126
} else if (
127127
// Check: process.foo === "bar"
128-
env === "ssr" && t.isMemberExpression(node.left) &&
128+
env === "server" && t.isMemberExpression(node.left) &&
129129
t.isIdentifier(node.left.object) && node.left.object.name === "process" &&
130130
t.isIdentifier(node.left.property) &&
131131
!PROCESS_PROPERTIES.has(node.left.property.name)

0 commit comments

Comments
 (0)