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:
@@ -15,7 +15,7 @@ export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
|
||||
isDev = env.command === "serve";
|
||||
},
|
||||
applyToEnvironment(env) {
|
||||
return env.name === "client";
|
||||
return env.config.consumer === "client";
|
||||
},
|
||||
configResolved(config) {
|
||||
clientEntry = pathWithRoot(options.clientEntry, config.root);
|
||||
|
||||
@@ -17,7 +17,7 @@ export function clientSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
name: "fresh:client-snapshot",
|
||||
sharedDuringBuild: true,
|
||||
applyToEnvironment(env) {
|
||||
return env.name === "client";
|
||||
return env.config.consumer === "client";
|
||||
},
|
||||
|
||||
async config(cfg, env) {
|
||||
|
||||
@@ -64,7 +64,9 @@ export function deno(): Plugin {
|
||||
external: true,
|
||||
};
|
||||
}
|
||||
const loader = options?.ssr ? ssrLoader : browserLoader;
|
||||
const loader = this.environment.config.consumer === "server"
|
||||
? ssrLoader
|
||||
: browserLoader;
|
||||
|
||||
const original = id;
|
||||
|
||||
@@ -154,8 +156,10 @@ export function deno(): Plugin {
|
||||
// ignore
|
||||
}
|
||||
},
|
||||
async load(id, options) {
|
||||
const loader = options?.ssr ? ssrLoader : browserLoader;
|
||||
async load(id) {
|
||||
const loader = this.environment.config.consumer === "server"
|
||||
? ssrLoader
|
||||
: browserLoader;
|
||||
|
||||
if (isDenoSpecifier(id)) {
|
||||
const { type, specifier } = parseDenoSpecifier(id);
|
||||
@@ -168,7 +172,7 @@ export function deno(): Plugin {
|
||||
const code = new TextDecoder().decode(result.code);
|
||||
|
||||
const maybeJsx = babelTransform({
|
||||
ssr: !!options?.ssr,
|
||||
ssr: this.environment.config.consumer === "server",
|
||||
media: result.mediaType,
|
||||
code,
|
||||
id: specifier,
|
||||
@@ -212,7 +216,7 @@ export function deno(): Plugin {
|
||||
const code = new TextDecoder().decode(result.code);
|
||||
|
||||
const maybeJsx = babelTransform({
|
||||
ssr: !!options?.ssr,
|
||||
ssr: this.environment.config.consumer === "server",
|
||||
media: result.mediaType,
|
||||
id,
|
||||
code,
|
||||
@@ -230,10 +234,10 @@ export function deno(): Plugin {
|
||||
filter: {
|
||||
id: JSX_REG,
|
||||
},
|
||||
async handler(_, id, options) {
|
||||
async handler(_, id) {
|
||||
// This transform is a hack to be able to re-use Deno's precompile
|
||||
// jsx transform.
|
||||
if (!options?.ssr) {
|
||||
if (this.environment.name === "client") {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { DevEnvironment, Plugin } from "vite";
|
||||
import * as path from "@std/path";
|
||||
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";
|
||||
|
||||
export function devServer(): Plugin[] {
|
||||
@@ -118,7 +118,7 @@ export function devServer(): Plugin[] {
|
||||
{
|
||||
name: "fresh:server_hmr",
|
||||
applyToEnvironment(env) {
|
||||
return env.name === "ssr";
|
||||
return env.config.consumer === "server";
|
||||
},
|
||||
hotUpdate(options) {
|
||||
const clientMod = options.server.environments.client.moduleGraph
|
||||
|
||||
@@ -26,9 +26,9 @@ export function patches(): Plugin {
|
||||
filter: {
|
||||
id: JS_REG,
|
||||
},
|
||||
handler(code, id, options) {
|
||||
handler(code, id) {
|
||||
const presets = [];
|
||||
if (!options?.ssr && JSX_REG.test(id)) {
|
||||
if (this.environment.config.consumer === "client" && JSX_REG.test(id)) {
|
||||
presets.push([babelReact, {
|
||||
runtime: "automatic",
|
||||
importSource: "preact",
|
||||
@@ -40,7 +40,7 @@ export function patches(): Plugin {
|
||||
const env = isDev ? "development" : "production";
|
||||
|
||||
const plugins: babel.PluginItem[] = [
|
||||
codeEvalPlugin(options?.ssr ? "ssr" : "client", env),
|
||||
codeEvalPlugin(this.environment.config.consumer, env),
|
||||
cjsPlugin,
|
||||
removePolyfills,
|
||||
jsxComments,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -34,7 +34,7 @@ export function serverEntryPlugin(
|
||||
name: "fresh:server_entry",
|
||||
sharedDuringBuild: true,
|
||||
applyToEnvironment(env) {
|
||||
return env.name === "ssr";
|
||||
return env.config.consumer === "server";
|
||||
},
|
||||
config(_, env) {
|
||||
isDev = env.command === "serve";
|
||||
@@ -77,7 +77,7 @@ export function serverEntryPlugin(
|
||||
});
|
||||
|
||||
code += `
|
||||
|
||||
|
||||
export function registerStaticFile(prepared) {
|
||||
snapshot.staticFiles.set(prepared.name, {
|
||||
name: prepared.name,
|
||||
|
||||
@@ -48,7 +48,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
name: "fresh:server-snapshot",
|
||||
sharedDuringBuild: true,
|
||||
applyToEnvironment(env) {
|
||||
return env.name === "ssr";
|
||||
return env.config.consumer === "server";
|
||||
},
|
||||
config(_, env) {
|
||||
isDev = env.command === "serve";
|
||||
@@ -348,7 +348,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
filter: {
|
||||
id: /\.(css|less|sass|scss)(\?.*)?$/,
|
||||
},
|
||||
handler(_code, id, _options) {
|
||||
handler(_code, id) {
|
||||
if (server) {
|
||||
const ssrGraph = server.environments.ssr.moduleGraph;
|
||||
const mod = ssrGraph.getModuleById(id);
|
||||
@@ -452,7 +452,7 @@ export default ${JSON.stringify(route.css)}
|
||||
name: "fresh-route-css-build-ssr",
|
||||
sharedDuringBuild: true,
|
||||
applyToEnvironment(env) {
|
||||
return env.name === "ssr";
|
||||
return env.config.consumer === "server";
|
||||
},
|
||||
async writeBundle(_, bundle) {
|
||||
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 */
|
||||
export type ImportCheck = (
|
||||
id: string,
|
||||
env: "ssr" | "client",
|
||||
env: string,
|
||||
) => ImportCheckDiagnostic | void;
|
||||
|
||||
export interface CheckImportOptions {
|
||||
@@ -26,7 +26,7 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
|
||||
function check(
|
||||
options: CheckImportOptions,
|
||||
id: string,
|
||||
env: "ssr" | "client",
|
||||
env: "server" | "client",
|
||||
): ImportCheckDiagnostic | undefined {
|
||||
for (let i = 0; i < options.checks.length; i++) {
|
||||
const check = options.checks[i];
|
||||
@@ -61,7 +61,7 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
|
||||
/[\\/]node_modules[\\/]/,
|
||||
],
|
||||
},
|
||||
async handler(id, importer, options) {
|
||||
async handler(id, importer) {
|
||||
if (
|
||||
importer &&
|
||||
(importer.startsWith("\0") || importer.includes("node_modules") ||
|
||||
@@ -69,24 +69,28 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const env = options.ssr ? "ssr" : "client";
|
||||
|
||||
let result: ImportCheckDiagnostic | undefined;
|
||||
if (id.startsWith(".")) {
|
||||
const resolved = await this.resolve(id, importer, options);
|
||||
const resolved = await this.resolve(id, importer);
|
||||
|
||||
if (resolved !== null) {
|
||||
const key = `${env}::${resolved.id}::${importer}`;
|
||||
const key =
|
||||
`${this.environment.config.consumer}::${resolved.id}::${importer}`;
|
||||
if (!seen.has(key)) {
|
||||
result = check(pluginOptions, resolved.id, env);
|
||||
result = check(
|
||||
pluginOptions,
|
||||
resolved.id,
|
||||
this.environment.config.consumer,
|
||||
);
|
||||
}
|
||||
|
||||
seen.add(key);
|
||||
}
|
||||
} else {
|
||||
const key = `${env}::${id}::${importer}`;
|
||||
const key = `${this.environment.config.consumer}::${id}::${importer}`;
|
||||
if (!seen.has(key)) {
|
||||
result = check(pluginOptions, id, env);
|
||||
result = check(pluginOptions, id, this.environment.config.consumer);
|
||||
}
|
||||
seen.add(key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user