Skip to content

Commit bc4d5ac

Browse files
committed
chore: migrate off of options.ssr
1 parent 80d82c9 commit bc4d5ac

File tree

10 files changed

+46
-38
lines changed

10 files changed

+46
-38
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function runTest(
66
options: {
77
input: string;
88
expected: string;
9-
env: "client" | "ssr";
9+
env: "client" | "server";
1010
mode: "development" | "production";
1111
},
1212
) {
@@ -45,7 +45,7 @@ Deno.test("code eval - resolve runtime conditional detection", () => {
4545
});
4646

4747
runTest({
48-
env: "ssr",
48+
env: "server",
4949
mode: "development",
5050
input: `if (typeof process === 'undefined') {
5151
module.exports = require('./browser.js');
@@ -56,7 +56,7 @@ Deno.test("code eval - resolve runtime conditional detection", () => {
5656
});
5757

5858
runTest({
59-
env: "ssr",
59+
env: "server",
6060
mode: "development",
6161
input: `if (typeof process !== 'undefined') {
6262
module.exports = require('./node.js');
@@ -93,7 +93,7 @@ Deno.test("code eval - resolve process.env.NODE_ENV", () => {
9393

9494
Deno.test("code eval - npm:debug", () => {
9595
runTest({
96-
env: "ssr",
96+
env: "server",
9797
mode: "development",
9898
input:
9999
`if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
@@ -107,7 +107,7 @@ Deno.test("code eval - npm:debug", () => {
107107

108108
Deno.test("code eval - npm:pg", () => {
109109
runTest({
110-
env: "ssr",
110+
env: "server",
111111
mode: "development",
112112
input: `if (typeof process.env.NODE_PG_FORCE_NATIVE !== "undefined") {
113113
module.exports = require('./native.js');
@@ -120,7 +120,7 @@ Deno.test("code eval - npm:pg", () => {
120120

121121
Deno.test("code eval - npm:pg #2", () => {
122122
runTest({
123-
env: "ssr",
123+
env: "server",
124124
mode: "development",
125125
input:
126126
`const useLegacyCrypto = parseInt(process.versions && process.versions.node && process.versions.node.split('.')[0]) < 15

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function serverEntryPlugin(
3434
name: "fresh:server_entry",
3535
sharedDuringBuild: true,
3636
applyToEnvironment(env) {
37-
return env.name === "ssr";
37+
return env.config.consumer === "server";
3838
},
3939
config(_, env) {
4040
isDev = env.command === "serve";
@@ -77,7 +77,7 @@ export function serverEntryPlugin(
7777
});
7878

7979
code += `
80-
80+
8181
export function registerStaticFile(prepared) {
8282
snapshot.staticFiles.set(prepared.name, {
8383
name: prepared.name,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
4848
name: "fresh:server-snapshot",
4949
sharedDuringBuild: true,
5050
applyToEnvironment(env) {
51-
return env.name === "ssr";
51+
return env.config.consumer === "server";
5252
},
5353
config(_, env) {
5454
isDev = env.command === "serve";
@@ -348,7 +348,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
348348
filter: {
349349
id: /\.(css|less|sass|scss)(\?.*)?$/,
350350
},
351-
handler(_code, id, _options) {
351+
handler(_code, id) {
352352
if (server) {
353353
const ssrGraph = server.environments.ssr.moduleGraph;
354354
const mod = ssrGraph.getModuleById(id);
@@ -452,7 +452,7 @@ export default ${JSON.stringify(route.css)}
452452
name: "fresh-route-css-build-ssr",
453453
sharedDuringBuild: true,
454454
applyToEnvironment(env) {
455-
return env.name === "ssr";
455+
return env.config.consumer === "server";
456456
},
457457
async writeBundle(_, bundle) {
458458
const asset = bundle[".vite/manifest.json"];

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface ImportCheckDiagnostic {
1515
/** A check whether or not an import is valid or not for an environment */
1616
export type ImportCheck = (
1717
id: string,
18-
env: "ssr" | "client",
18+
env: string,
1919
) => ImportCheckDiagnostic | void;
2020

2121
export interface CheckImportOptions {
@@ -26,7 +26,7 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
2626
function check(
2727
options: CheckImportOptions,
2828
id: string,
29-
env: "ssr" | "client",
29+
env: "server" | "client",
3030
): ImportCheckDiagnostic | undefined {
3131
for (let i = 0; i < options.checks.length; i++) {
3232
const check = options.checks[i];
@@ -61,32 +61,36 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin {
6161
/[\\/]node_modules[\\/]/,
6262
],
6363
},
64-
async handler(id, importer, options) {
64+
async handler(id, importer) {
6565
if (
6666
importer &&
6767
(importer.startsWith("\0") || importer.includes("node_modules") ||
6868
importer.includes("deno::"))
6969
) {
7070
return;
7171
}
72-
const env = options.ssr ? "ssr" : "client";
7372

7473
let result: ImportCheckDiagnostic | undefined;
7574
if (id.startsWith(".")) {
76-
const resolved = await this.resolve(id, importer, options);
75+
const resolved = await this.resolve(id, importer);
7776

7877
if (resolved !== null) {
79-
const key = `${env}::${resolved.id}::${importer}`;
78+
const key =
79+
`${this.environment.config.consumer}::${resolved.id}::${importer}`;
8080
if (!seen.has(key)) {
81-
result = check(pluginOptions, resolved.id, env);
81+
result = check(
82+
pluginOptions,
83+
resolved.id,
84+
this.environment.config.consumer,
85+
);
8286
}
8387

8488
seen.add(key);
8589
}
8690
} else {
87-
const key = `${env}::${id}::${importer}`;
91+
const key = `${this.environment.config.consumer}::${id}::${importer}`;
8892
if (!seen.has(key)) {
89-
result = check(pluginOptions, id, env);
93+
result = check(pluginOptions, id, this.environment.config.consumer);
9094
}
9195
seen.add(key);
9296
}

0 commit comments

Comments
 (0)