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
Generated
+21 -21
View File
@@ -9,8 +9,8 @@
"jsr:@deno/esbuild-plugin@^1.2.0": "1.2.0", "jsr:@deno/esbuild-plugin@^1.2.0": "1.2.0",
"jsr:@deno/graph@0.86": "0.86.9", "jsr:@deno/graph@0.86": "0.86.9",
"jsr:@deno/graph@~0.82.3": "0.82.3", "jsr:@deno/graph@~0.82.3": "0.82.3",
"jsr:@deno/loader@~0.3.2": "0.3.9", "jsr:@deno/loader@~0.3.10": "0.3.10",
"jsr:@deno/loader@~0.3.3": "0.3.9", "jsr:@deno/loader@~0.3.3": "0.3.10",
"jsr:@marvinh-test/fresh-island@^0.0.3": "0.0.3", "jsr:@marvinh-test/fresh-island@^0.0.3": "0.0.3",
"jsr:@marvinh-test/import-json@^0.0.1": "0.0.1", "jsr:@marvinh-test/import-json@^0.0.1": "0.0.1",
"jsr:@std/assert@^1.0.14": "1.0.15", "jsr:@std/assert@^1.0.14": "1.0.15",
@@ -20,8 +20,8 @@
"jsr:@std/async@^1.0.15": "1.0.15", "jsr:@std/async@^1.0.15": "1.0.15",
"jsr:@std/bytes@^1.0.5": "1.0.6", "jsr:@std/bytes@^1.0.5": "1.0.6",
"jsr:@std/bytes@^1.0.6": "1.0.6", "jsr:@std/bytes@^1.0.6": "1.0.6",
"jsr:@std/cli@^1.0.19": "1.0.23", "jsr:@std/cli@^1.0.19": "1.0.24",
"jsr:@std/cli@^1.0.23": "1.0.23", "jsr:@std/cli@^1.0.23": "1.0.24",
"jsr:@std/collections@^1.1.2": "1.1.3", "jsr:@std/collections@^1.1.2": "1.1.3",
"jsr:@std/collections@^1.1.3": "1.1.3", "jsr:@std/collections@^1.1.3": "1.1.3",
"jsr:@std/crypto@^1.0.5": "1.0.5", "jsr:@std/crypto@^1.0.5": "1.0.5",
@@ -53,10 +53,10 @@
"jsr:@std/media-types@1": "1.1.0", "jsr:@std/media-types@1": "1.1.0",
"jsr:@std/media-types@^1.1.0": "1.1.0", "jsr:@std/media-types@^1.1.0": "1.1.0",
"jsr:@std/net@^1.0.6": "1.0.6", "jsr:@std/net@^1.0.6": "1.0.6",
"jsr:@std/path@1": "1.1.2", "jsr:@std/path@1": "1.1.3",
"jsr:@std/path@^1.0.8": "1.1.2", "jsr:@std/path@^1.0.8": "1.1.3",
"jsr:@std/path@^1.1.1": "1.1.2", "jsr:@std/path@^1.1.1": "1.1.3",
"jsr:@std/path@^1.1.2": "1.1.2", "jsr:@std/path@^1.1.2": "1.1.3",
"jsr:@std/semver@1": "1.0.6", "jsr:@std/semver@1": "1.0.6",
"jsr:@std/semver@^1.0.6": "1.0.6", "jsr:@std/semver@^1.0.6": "1.0.6",
"jsr:@std/streams@1": "1.0.13", "jsr:@std/streams@1": "1.0.13",
@@ -69,12 +69,12 @@
"jsr:@zip-js/zip-js@^2.7.52": "2.8.8", "jsr:@zip-js/zip-js@^2.7.52": "2.8.8",
"npm:@babel/core@^7.28.0": "7.28.5", "npm:@babel/core@^7.28.0": "7.28.5",
"npm:@babel/preset-react@^7.27.1": "7.28.5_@babel+core@7.28.5", "npm:@babel/preset-react@^7.27.1": "7.28.5_@babel+core@7.28.5",
"npm:@mjackson/node-fetch-server@0.7": "0.7.0",
"npm:@opentelemetry/api@^1.9.0": "1.9.0", "npm:@opentelemetry/api@^1.9.0": "1.9.0",
"npm:@preact/signals@2": "2.3.2_preact@10.27.2", "npm:@preact/signals@2": "2.3.2_preact@10.27.2",
"npm:@preact/signals@^2.2.1": "2.3.2_preact@10.27.2", "npm:@preact/signals@^2.2.1": "2.3.2_preact@10.27.2",
"npm:@prefresh/vite@^2.4.8": "2.4.11_preact@10.27.2_vite@7.1.12__@types+node@24.9.2__picomatch@4.0.3_@types+node@24.9.2", "npm:@prefresh/vite@^2.4.8": "2.4.11_preact@10.27.2_vite@7.1.12__@types+node@24.9.2__picomatch@4.0.3_@types+node@24.9.2",
"npm:@radix-ui/themes@^3.2.1": "3.2.1_react@19.1.1_react-dom@19.1.1__react@19.1.1", "npm:@radix-ui/themes@^3.2.1": "3.2.1_react@19.1.1_react-dom@19.1.1__react@19.1.1",
"npm:@remix-run/node-fetch-server@0.12": "0.12.0",
"npm:@supabase/postgrest-js@^1.21.4": "1.21.4", "npm:@supabase/postgrest-js@^1.21.4": "1.21.4",
"npm:@tailwindcss/postcss@^4.1.10": "4.1.16", "npm:@tailwindcss/postcss@^4.1.10": "4.1.16",
"npm:@tailwindcss/vite@^4.1.12": "4.1.16_vite@7.1.12__@types+node@24.9.2__picomatch@4.0.3_@types+node@24.9.2", "npm:@tailwindcss/vite@^4.1.12": "4.1.16_vite@7.1.12__@types+node@24.9.2__picomatch@4.0.3_@types+node@24.9.2",
@@ -182,8 +182,8 @@
"@deno/graph@0.86.9": { "@deno/graph@0.86.9": {
"integrity": "c4f353a695bcc5246c099602977dabc6534eacea9999a35a8cb24e807192e6a1" "integrity": "c4f353a695bcc5246c099602977dabc6534eacea9999a35a8cb24e807192e6a1"
}, },
"@deno/loader@0.3.9": { "@deno/loader@0.3.10": {
"integrity": "703d44656f7da0fa4a4a7f8a5105b5b41320821286508c2967b4252a00a2506f" "integrity": "a9c0aa44a0499e7fecef52c29fbc206c1c8f8946388f25d9d0789a23313bfd43"
}, },
"@marvinh-test/fresh-island@0.0.3": { "@marvinh-test/fresh-island@0.0.3": {
"integrity": "6d06b6009b7dfba9bba28e941e03e6ff652c4ef4f2fbfdf4b78741abd6c6c1c6", "integrity": "6d06b6009b7dfba9bba28e941e03e6ff652c4ef4f2fbfdf4b78741abd6c6c1c6",
@@ -207,8 +207,8 @@
"@std/bytes@1.0.6": { "@std/bytes@1.0.6": {
"integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a" "integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a"
}, },
"@std/cli@1.0.23": { "@std/cli@1.0.24": {
"integrity": "bf95b7a9425ba2af1ae5a6359daf58c508f2decf711a76ed2993cd352498ccca", "integrity": "b655a5beb26aa94f98add6bc8889f5fb9bc3ee2cc3fc954e151201f4c4200a5e",
"dependencies": [ "dependencies": [
"jsr:@std/internal@^1.0.12" "jsr:@std/internal@^1.0.12"
] ]
@@ -299,10 +299,10 @@
"@std/net@1.0.6": { "@std/net@1.0.6": {
"integrity": "110735f93e95bb9feb95790a8b1d1bf69ec0dc74f3f97a00a76ea5efea25500c" "integrity": "110735f93e95bb9feb95790a8b1d1bf69ec0dc74f3f97a00a76ea5efea25500c"
}, },
"@std/path@1.1.2": { "@std/path@1.1.3": {
"integrity": "c0b13b97dfe06546d5e16bf3966b1cadf92e1cc83e56ba5476ad8b498d9e3038", "integrity": "b015962d82a5e6daea980c32b82d2c40142149639968549c649031a230b1afb3",
"dependencies": [ "dependencies": [
"jsr:@std/internal@^1.0.10" "jsr:@std/internal@^1.0.12"
] ]
}, },
"@std/semver@1.0.5": { "@std/semver@1.0.5": {
@@ -737,9 +737,6 @@
"@jridgewell/sourcemap-codec" "@jridgewell/sourcemap-codec"
] ]
}, },
"@mjackson/node-fetch-server@0.7.0": {
"integrity": "sha512-un8diyEBKU3BTVj3GzlTPA1kIjCkGdD+AMYQy31Gf9JCkfoZzwgJ79GUtHrF2BN3XPNMLpubbzPcxys+a3uZEw=="
},
"@nodelib/fs.scandir@2.1.5": { "@nodelib/fs.scandir@2.1.5": {
"integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==",
"dependencies": [ "dependencies": [
@@ -1552,6 +1549,9 @@
"@redis/client" "@redis/client"
] ]
}, },
"@remix-run/node-fetch-server@0.12.0": {
"integrity": "sha512-oeg8w8aJJSuq1fCx85jCkcgTfI6On7sKwWVSO4/OW5AvTBuosAIwnuBd/LYeU/I7lYPOTW2NXhUfyfpyeexs4w=="
},
"@rollup/pluginutils@4.2.1": { "@rollup/pluginutils@4.2.1": {
"integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==",
"dependencies": [ "dependencies": [
@@ -3894,14 +3894,14 @@
}, },
"packages/plugin-vite": { "packages/plugin-vite": {
"dependencies": [ "dependencies": [
"jsr:@deno/loader@~0.3.2", "jsr:@deno/loader@~0.3.10",
"jsr:@fresh/core@2", "jsr:@fresh/core@2",
"jsr:@marvinh-test/import-json@^0.0.1", "jsr:@marvinh-test/import-json@^0.0.1",
"npm:@babel/core@^7.28.0", "npm:@babel/core@^7.28.0",
"npm:@babel/preset-react@^7.27.1", "npm:@babel/preset-react@^7.27.1",
"npm:@mjackson/node-fetch-server@0.7",
"npm:@prefresh/vite@^2.4.8", "npm:@prefresh/vite@^2.4.8",
"npm:@radix-ui/themes@^3.2.1", "npm:@radix-ui/themes@^3.2.1",
"npm:@remix-run/node-fetch-server@0.12",
"npm:@tailwindcss/vite@^4.1.12", "npm:@tailwindcss/vite@^4.1.12",
"npm:@types/babel__core@^7.20.5", "npm:@types/babel__core@^7.20.5",
"npm:@types/node@^24.1.0", "npm:@types/node@^24.1.0",
+1
View File
@@ -11,4 +11,5 @@ export default defineConfig({
}), }),
tailwind(), tailwind(),
], ],
future: "warn",
}); });
+1 -1
View File
@@ -25,7 +25,7 @@
"@babel/preset-react": "npm:@babel/preset-react@^7.27.1", "@babel/preset-react": "npm:@babel/preset-react@^7.27.1",
"@deno/loader": "jsr:@deno/loader@^0.3.10", "@deno/loader": "jsr:@deno/loader@^0.3.10",
"@marvinh-test/import-json": "jsr:@marvinh-test/import-json@^0.0.1", "@marvinh-test/import-json": "jsr:@marvinh-test/import-json@^0.0.1",
"@mjackson/node-fetch-server": "npm:@mjackson/node-fetch-server@^0.7.0", "@remix-run/node-fetch-server": "npm:@remix-run/node-fetch-server@^0.12.0",
"@prefresh/vite": "npm:@prefresh/vite@^2.4.8", "@prefresh/vite": "npm:@prefresh/vite@^2.4.8",
"@radix-ui/themes": "npm:@radix-ui/themes@^3.2.1", "@radix-ui/themes": "npm:@radix-ui/themes@^3.2.1",
"@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12", "@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12",
+1 -1
View File
@@ -256,6 +256,6 @@ async function loadEnvFile(envPath: string) {
try { try {
await stdLoadEnv({ envPath, export: true }); await stdLoadEnv({ envPath, export: true });
} catch { } catch {
// Ignoe // Ignore
} }
} }
@@ -15,7 +15,7 @@ export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
isDev = env.command === "serve"; isDev = env.command === "serve";
}, },
applyToEnvironment(env) { applyToEnvironment(env) {
return env.name === "client"; return env.config.consumer === "client";
}, },
configResolved(config) { configResolved(config) {
clientEntry = pathWithRoot(options.clientEntry, config.root); clientEntry = pathWithRoot(options.clientEntry, config.root);
@@ -17,7 +17,7 @@ export function clientSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
name: "fresh:client-snapshot", name: "fresh:client-snapshot",
sharedDuringBuild: true, sharedDuringBuild: true,
applyToEnvironment(env) { applyToEnvironment(env) {
return env.name === "client"; return env.config.consumer === "client";
}, },
async config(cfg, env) { async config(cfg, env) {
+11 -7
View File
@@ -64,7 +64,9 @@ export function deno(): Plugin {
external: true, external: true,
}; };
} }
const loader = options?.ssr ? ssrLoader : browserLoader; const loader = this.environment.config.consumer === "server"
? ssrLoader
: browserLoader;
const original = id; const original = id;
@@ -154,8 +156,10 @@ export function deno(): Plugin {
// ignore // ignore
} }
}, },
async load(id, options) { async load(id) {
const loader = options?.ssr ? ssrLoader : browserLoader; const loader = this.environment.config.consumer === "server"
? ssrLoader
: browserLoader;
if (isDenoSpecifier(id)) { if (isDenoSpecifier(id)) {
const { type, specifier } = parseDenoSpecifier(id); const { type, specifier } = parseDenoSpecifier(id);
@@ -168,7 +172,7 @@ export function deno(): Plugin {
const code = new TextDecoder().decode(result.code); const code = new TextDecoder().decode(result.code);
const maybeJsx = babelTransform({ const maybeJsx = babelTransform({
ssr: !!options?.ssr, ssr: this.environment.config.consumer === "server",
media: result.mediaType, media: result.mediaType,
code, code,
id: specifier, id: specifier,
@@ -212,7 +216,7 @@ export function deno(): Plugin {
const code = new TextDecoder().decode(result.code); const code = new TextDecoder().decode(result.code);
const maybeJsx = babelTransform({ const maybeJsx = babelTransform({
ssr: !!options?.ssr, ssr: this.environment.config.consumer === "server",
media: result.mediaType, media: result.mediaType,
id, id,
code, code,
@@ -230,10 +234,10 @@ export function deno(): Plugin {
filter: { filter: {
id: JSX_REG, id: JSX_REG,
}, },
async handler(_, id, options) { async handler(_, id) {
// This transform is a hack to be able to re-use Deno's precompile // This transform is a hack to be able to re-use Deno's precompile
// jsx transform. // jsx transform.
if (!options?.ssr) { if (this.environment.name === "client") {
return; return;
} }
@@ -1,7 +1,7 @@
import type { DevEnvironment, Plugin } from "vite"; import type { DevEnvironment, Plugin } from "vite";
import * as path from "@std/path"; import * as path from "@std/path";
import { ASSET_CACHE_BUST_KEY } from "fresh/internal"; import { ASSET_CACHE_BUST_KEY } from "fresh/internal";
import { createRequest, sendResponse } from "@mjackson/node-fetch-server"; import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
import { hashCode } from "../shared.ts"; import { hashCode } from "../shared.ts";
export function devServer(): Plugin[] { export function devServer(): Plugin[] {
@@ -118,7 +118,7 @@ export function devServer(): Plugin[] {
{ {
name: "fresh:server_hmr", name: "fresh:server_hmr",
applyToEnvironment(env) { applyToEnvironment(env) {
return env.name === "ssr"; return env.config.consumer === "server";
}, },
hotUpdate(options) { hotUpdate(options) {
const clientMod = options.server.environments.client.moduleGraph const clientMod = options.server.environments.client.moduleGraph
+3 -3
View File
@@ -26,9 +26,9 @@ export function patches(): Plugin {
filter: { filter: {
id: JS_REG, id: JS_REG,
}, },
handler(code, id, options) { handler(code, id) {
const presets = []; const presets = [];
if (!options?.ssr && JSX_REG.test(id)) { if (this.environment.config.consumer === "client" && JSX_REG.test(id)) {
presets.push([babelReact, { presets.push([babelReact, {
runtime: "automatic", runtime: "automatic",
importSource: "preact", importSource: "preact",
@@ -40,7 +40,7 @@ export function patches(): Plugin {
const env = isDev ? "development" : "production"; const env = isDev ? "development" : "production";
const plugins: babel.PluginItem[] = [ const plugins: babel.PluginItem[] = [
codeEvalPlugin(options?.ssr ? "ssr" : "client", env), codeEvalPlugin(this.environment.config.consumer, env),
cjsPlugin, cjsPlugin,
removePolyfills, removePolyfills,
jsxComments, jsxComments,
@@ -3,7 +3,7 @@ import type { PluginObj, PluginPass, types } from "@babel/core";
const APPLY_PG_QUIRKS = "applyPgQuirks"; const APPLY_PG_QUIRKS = "applyPgQuirks";
export function codeEvalPlugin( export function codeEvalPlugin(
env: "ssr" | "client", env: "server" | "client",
mode: string, mode: string,
) { ) {
return ( return (
@@ -41,7 +41,7 @@ export function codeEvalPlugin(
function evaluateExpr( function evaluateExpr(
t: typeof types, t: typeof types,
env: "client" | "ssr", env: "server" | "client",
mode: string, mode: string,
node: types.Node, node: types.Node,
state: PluginPass, state: PluginPass,
@@ -76,9 +76,9 @@ function evaluateExpr(
node.right.value === "undefined" node.right.value === "undefined"
) { ) {
if (node.operator === "==" || node.operator === "===") { if (node.operator === "==" || node.operator === "===") {
return env !== "ssr"; return env !== "server";
} else if (node.operator === "!=" || node.operator === "!==") { } else if (node.operator === "!=" || node.operator === "!==") {
return env === "ssr"; return env === "server";
} }
} else if ( } else if (
// Workaround for npm:pg // Workaround for npm:pg
@@ -125,7 +125,7 @@ function evaluateExpr(
if (result !== null) return result; if (result !== null) return result;
} else if ( } else if (
// Check: process.foo === "bar" // 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.object) && node.left.object.name === "process" &&
t.isIdentifier(node.left.property) && t.isIdentifier(node.left.property) &&
!PROCESS_PROPERTIES.has(node.left.property.name) !PROCESS_PROPERTIES.has(node.left.property.name)
@@ -6,7 +6,7 @@ function runTest(
options: { options: {
input: string; input: string;
expected: string; expected: string;
env: "client" | "ssr"; env: "client" | "server";
mode: "development" | "production"; mode: "development" | "production";
}, },
) { ) {
@@ -45,7 +45,7 @@ Deno.test("code eval - resolve runtime conditional detection", () => {
}); });
runTest({ runTest({
env: "ssr", env: "server",
mode: "development", mode: "development",
input: `if (typeof process === 'undefined') { input: `if (typeof process === 'undefined') {
module.exports = require('./browser.js'); module.exports = require('./browser.js');
@@ -56,7 +56,7 @@ Deno.test("code eval - resolve runtime conditional detection", () => {
}); });
runTest({ runTest({
env: "ssr", env: "server",
mode: "development", mode: "development",
input: `if (typeof process !== 'undefined') { input: `if (typeof process !== 'undefined') {
module.exports = require('./node.js'); module.exports = require('./node.js');
@@ -93,7 +93,7 @@ Deno.test("code eval - resolve process.env.NODE_ENV", () => {
Deno.test("code eval - npm:debug", () => { Deno.test("code eval - npm:debug", () => {
runTest({ runTest({
env: "ssr", env: "server",
mode: "development", mode: "development",
input: input:
`if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) { `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", () => { Deno.test("code eval - npm:pg", () => {
runTest({ runTest({
env: "ssr", env: "server",
mode: "development", mode: "development",
input: `if (typeof process.env.NODE_PG_FORCE_NATIVE !== "undefined") { input: `if (typeof process.env.NODE_PG_FORCE_NATIVE !== "undefined") {
module.exports = require('./native.js'); module.exports = require('./native.js');
@@ -120,7 +120,7 @@ Deno.test("code eval - npm:pg", () => {
Deno.test("code eval - npm:pg #2", () => { Deno.test("code eval - npm:pg #2", () => {
runTest({ runTest({
env: "ssr", env: "server",
mode: "development", mode: "development",
input: input:
`const useLegacyCrypto = parseInt(process.versions && process.versions.node && process.versions.node.split('.')[0]) < 15 `const useLegacyCrypto = parseInt(process.versions && process.versions.node && process.versions.node.split('.')[0]) < 15
@@ -34,7 +34,7 @@ export function serverEntryPlugin(
name: "fresh:server_entry", name: "fresh:server_entry",
sharedDuringBuild: true, sharedDuringBuild: true,
applyToEnvironment(env) { applyToEnvironment(env) {
return env.name === "ssr"; return env.config.consumer === "server";
}, },
config(_, env) { config(_, env) {
isDev = env.command === "serve"; isDev = env.command === "serve";
@@ -48,7 +48,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
name: "fresh:server-snapshot", name: "fresh:server-snapshot",
sharedDuringBuild: true, sharedDuringBuild: true,
applyToEnvironment(env) { applyToEnvironment(env) {
return env.name === "ssr"; return env.config.consumer === "server";
}, },
config(_, env) { config(_, env) {
isDev = env.command === "serve"; isDev = env.command === "serve";
@@ -348,7 +348,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
filter: { filter: {
id: /\.(css|less|sass|scss)(\?.*)?$/, id: /\.(css|less|sass|scss)(\?.*)?$/,
}, },
handler(_code, id, _options) { handler(_code, id) {
if (server) { if (server) {
const ssrGraph = server.environments.ssr.moduleGraph; const ssrGraph = server.environments.ssr.moduleGraph;
const mod = ssrGraph.getModuleById(id); const mod = ssrGraph.getModuleById(id);
@@ -452,7 +452,7 @@ export default ${JSON.stringify(route.css)}
name: "fresh-route-css-build-ssr", name: "fresh-route-css-build-ssr",
sharedDuringBuild: true, sharedDuringBuild: true,
applyToEnvironment(env) { applyToEnvironment(env) {
return env.name === "ssr"; return env.config.consumer === "server";
}, },
async writeBundle(_, bundle) { async writeBundle(_, bundle) {
const asset = bundle[".vite/manifest.json"]; const asset = bundle[".vite/manifest.json"];
@@ -15,7 +15,7 @@ export interface ImportCheckDiagnostic {
/** A check whether or not an import is valid or not for an environment */ /** A check whether or not an import is valid or not for an environment */
export type ImportCheck = ( export type ImportCheck = (
id: string, id: string,
env: "ssr" | "client", env: string,
) => ImportCheckDiagnostic | void; ) => ImportCheckDiagnostic | void;
export interface CheckImportOptions { export interface CheckImportOptions {
@@ -26,7 +26,7 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
function check( function check(
options: CheckImportOptions, options: CheckImportOptions,
id: string, id: string,
env: "ssr" | "client", env: "server" | "client",
): ImportCheckDiagnostic | undefined { ): ImportCheckDiagnostic | undefined {
for (let i = 0; i < options.checks.length; i++) { for (let i = 0; i < options.checks.length; i++) {
const check = options.checks[i]; const check = options.checks[i];
@@ -61,7 +61,7 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
/[\\/]node_modules[\\/]/, /[\\/]node_modules[\\/]/,
], ],
}, },
async handler(id, importer, options) { async handler(id, importer) {
if ( if (
importer && importer &&
(importer.startsWith("\0") || importer.includes("node_modules") || (importer.startsWith("\0") || importer.includes("node_modules") ||
@@ -69,24 +69,28 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
) { ) {
return; return;
} }
const env = options.ssr ? "ssr" : "client";
let result: ImportCheckDiagnostic | undefined; let result: ImportCheckDiagnostic | undefined;
if (id.startsWith(".")) { if (id.startsWith(".")) {
const resolved = await this.resolve(id, importer, options); const resolved = await this.resolve(id, importer);
if (resolved !== null) { if (resolved !== null) {
const key = `${env}::${resolved.id}::${importer}`; const key =
`${this.environment.config.consumer}::${resolved.id}::${importer}`;
if (!seen.has(key)) { if (!seen.has(key)) {
result = check(pluginOptions, resolved.id, env); result = check(
pluginOptions,
resolved.id,
this.environment.config.consumer,
);
} }
seen.add(key); seen.add(key);
} }
} else { } else {
const key = `${env}::${id}::${importer}`; const key = `${this.environment.config.consumer}::${id}::${importer}`;
if (!seen.has(key)) { if (!seen.has(key)) {
result = check(pluginOptions, id, env); result = check(pluginOptions, id, this.environment.config.consumer);
} }
seen.add(key); seen.add(key);
} }