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](https://github.com/denoland/deno/pull/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?)
This commit is contained in:
Eli
2025-12-09 13:00:36 +01:00
committed by GitHub
parent 0acebce0bb
commit ab14d1044c
14 changed files with 71 additions and 62 deletions
@@ -3,7 +3,7 @@ import type { PluginObj, PluginPass, types } from "@babel/core";
const APPLY_PG_QUIRKS = "applyPgQuirks";
export function codeEvalPlugin(
env: "ssr" | "client",
env: "server" | "client",
mode: string,
) {
return (
@@ -41,7 +41,7 @@ export function codeEvalPlugin(
function evaluateExpr(
t: typeof types,
env: "client" | "ssr",
env: "server" | "client",
mode: string,
node: types.Node,
state: PluginPass,
@@ -76,9 +76,9 @@ function evaluateExpr(
node.right.value === "undefined"
) {
if (node.operator === "==" || node.operator === "===") {
return env !== "ssr";
return env !== "server";
} else if (node.operator === "!=" || node.operator === "!==") {
return env === "ssr";
return env === "server";
}
} else if (
// Workaround for npm:pg
@@ -125,7 +125,7 @@ function evaluateExpr(
if (result !== null) return result;
} else if (
// Check: process.foo === "bar"
env === "ssr" && t.isMemberExpression(node.left) &&
env === "server" && t.isMemberExpression(node.left) &&
t.isIdentifier(node.left.object) && node.left.object.name === "process" &&
t.isIdentifier(node.left.property) &&
!PROCESS_PROPERTIES.has(node.left.property.name)
@@ -6,7 +6,7 @@ function runTest(
options: {
input: string;
expected: string;
env: "client" | "ssr";
env: "client" | "server";
mode: "development" | "production";
},
) {
@@ -45,7 +45,7 @@ Deno.test("code eval - resolve runtime conditional detection", () => {
});
runTest({
env: "ssr",
env: "server",
mode: "development",
input: `if (typeof process === 'undefined') {
module.exports = require('./browser.js');
@@ -56,7 +56,7 @@ Deno.test("code eval - resolve runtime conditional detection", () => {
});
runTest({
env: "ssr",
env: "server",
mode: "development",
input: `if (typeof process !== 'undefined') {
module.exports = require('./node.js');
@@ -93,7 +93,7 @@ Deno.test("code eval - resolve process.env.NODE_ENV", () => {
Deno.test("code eval - npm:debug", () => {
runTest({
env: "ssr",
env: "server",
mode: "development",
input:
`if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
@@ -107,7 +107,7 @@ Deno.test("code eval - npm:debug", () => {
Deno.test("code eval - npm:pg", () => {
runTest({
env: "ssr",
env: "server",
mode: "development",
input: `if (typeof process.env.NODE_PG_FORCE_NATIVE !== "undefined") {
module.exports = require('./native.js');
@@ -120,7 +120,7 @@ Deno.test("code eval - npm:pg", () => {
Deno.test("code eval - npm:pg #2", () => {
runTest({
env: "ssr",
env: "server",
mode: "development",
input:
`const useLegacyCrypto = parseInt(process.versions && process.versions.node && process.versions.node.split('.')[0]) < 15