chore: simplify jsx config handling (#1973)

This commit is contained in:
Marvin Hagemeister
2023-10-23 13:11:27 +02:00
committed by GitHub
parent 98e1cee698
commit 7ad4610e3a
4 changed files with 41 additions and 77 deletions
+10 -13
View File
@@ -16,16 +16,12 @@ export interface EsbuildBuilderOptions {
/** The path to the deno.json / deno.jsonc config file. */ /** The path to the deno.json / deno.jsonc config file. */
configPath: string; configPath: string;
/** The JSX configuration. */ /** The JSX configuration. */
jsxConfig: JSXConfig; jsx?: string;
jsxImportSource?: string;
target: string | string[]; target: string | string[];
absoluteWorkingDir: string; absoluteWorkingDir: string;
} }
export interface JSXConfig {
jsx: "react" | "react-jsx";
jsxImportSource?: string;
}
export class EsbuildBuilder implements Builder { export class EsbuildBuilder implements Builder {
#options: EsbuildBuilderOptions; #options: EsbuildBuilderOptions;
@@ -83,8 +79,14 @@ export class EsbuildBuilder implements Builder {
sourcemap: opts.dev ? "linked" : false, sourcemap: opts.dev ? "linked" : false,
...minifyOptions, ...minifyOptions,
jsx: JSX_RUNTIME_MODE[opts.jsxConfig.jsx], jsx: opts.jsx === "react"
jsxImportSource: opts.jsxConfig.jsxImportSource, ? "transform"
: opts.jsx === "react-native" || opts.jsx === "preserve"
? "preserve"
: !opts.jsxImportSource
? "transform"
: "automatic",
jsxImportSource: opts.jsxImportSource ?? "preact",
absWorkingDir, absWorkingDir,
outdir: ".", outdir: ".",
@@ -126,11 +128,6 @@ export class EsbuildBuilder implements Builder {
} }
} }
const JSX_RUNTIME_MODE = {
"react": "transform",
"react-jsx": "automatic",
} as const;
function buildIdPlugin(buildId: string): Plugin { function buildIdPlugin(buildId: string): Plugin {
const file = import.meta.resolve("../runtime/build_id.ts"); const file = import.meta.resolve("../runtime/build_id.ts");
const url = new URL(file); const url = new URL(file);
-4
View File
@@ -28,9 +28,7 @@ Deno.test("esbuild snapshot with cwd=Deno.cwd()", async () => {
entrypoints: { entrypoints: {
main: mainEntry, main: mainEntry,
}, },
jsxConfig: {
jsx: "react-jsx", jsx: "react-jsx",
},
target: "es2020", target: "es2020",
}); });
@@ -50,9 +48,7 @@ Deno.test({
entrypoints: { entrypoints: {
main: mainEntry, main: mainEntry,
}, },
jsxConfig: {
jsx: "react-jsx", jsx: "react-jsx",
},
target: "es2020", target: "es2020",
}); });
-1
View File
@@ -2,7 +2,6 @@ export {
EsbuildBuilder, EsbuildBuilder,
type EsbuildBuilderOptions, type EsbuildBuilderOptions,
EsbuildSnapshot, EsbuildSnapshot,
type JSXConfig,
} from "./esbuild.ts"; } from "./esbuild.ts";
export { AotSnapshot } from "./aot_snapshot.ts"; export { AotSnapshot } from "./aot_snapshot.ts";
export interface Builder { export interface Builder {
+29 -57
View File
@@ -24,12 +24,7 @@ import {
SELF, SELF,
} from "../runtime/csp.ts"; } from "../runtime/csp.ts";
import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts"; import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
import { import { Builder, BuildSnapshot, EsbuildBuilder } from "../build/mod.ts";
Builder,
BuildSnapshot,
EsbuildBuilder,
JSXConfig,
} from "../build/mod.ts";
import { setAllIslands } from "./rendering/preact_hooks.ts"; import { setAllIslands } from "./rendering/preact_hooks.ts";
import { getCodeFrame } from "./code_frame.ts"; import { getCodeFrame } from "./code_frame.ts";
import { getInternalFreshState } from "./config.ts"; import { getInternalFreshState } from "./config.ts";
@@ -59,45 +54,39 @@ export type FromManifestConfig = FreshConfig & {
export async function getServerContext(state: InternalFreshState) { export async function getServerContext(state: InternalFreshState) {
const { config, denoJson, denoJsonPath: configPath } = state; const { config, denoJson, denoJsonPath: configPath } = state;
// Restore snapshot if available
let snapshot: BuildSnapshot | null = null;
if (state.loadSnapshot) {
snapshot = await loadAotSnapshot(config);
}
denoJson.compilerOptions ??= {};
let jsx: "react" | "react-jsx";
switch (denoJson.compilerOptions.jsx) {
case "react":
case undefined:
jsx = "react";
break;
case "react-jsx":
jsx = "react-jsx";
break;
default:
throw new Error("Unknown jsx option: " + denoJson.compilerOptions.jsx);
}
const jsxConfig: JSXConfig = {
jsx,
jsxImportSource: denoJson.compilerOptions.jsxImportSource,
};
const extractResult = await extractRoutes(state);
if (config.dev) { if (config.dev) {
// Ensure that debugging hooks are set up for SSR rendering // Ensure that debugging hooks are set up for SSR rendering
await import("preact/debug"); await import("preact/debug");
} }
const extractResult = await extractRoutes(state);
// Restore snapshot if available
let snapshot: Builder | BuildSnapshot | Promise<BuildSnapshot> | null = null;
if (state.loadSnapshot) {
const loadedSnapshot = await loadAotSnapshot(config);
if (loadedSnapshot !== null) snapshot = loadedSnapshot;
}
const finalSnapshot = snapshot ?? new EsbuildBuilder({
buildID: BUILD_ID,
entrypoints: collectEntrypoints(
config.dev,
extractResult.islands,
config.plugins,
),
configPath,
dev: config.dev,
jsx: denoJson.compilerOptions?.jsx,
jsxImportSource: denoJson.compilerOptions?.jsxImportSource,
target: state.config.build.target,
absoluteWorkingDir: Deno.cwd(),
});
return new ServerContext( return new ServerContext(
state, state,
extractResult, extractResult,
configPath, finalSnapshot,
jsxConfig,
snapshot,
); );
} }
@@ -112,31 +101,14 @@ export class ServerContext {
constructor( constructor(
state: InternalFreshState, state: InternalFreshState,
extractResult: FsExtractResult, extractResult: FsExtractResult,
configPath: string, snapshot: Builder | BuildSnapshot | Promise<BuildSnapshot>,
jsxConfig: JSXConfig,
snapshot: BuildSnapshot | null = null,
) { ) {
this.#state = state; this.#state = state;
this.#extractResult = extractResult; this.#extractResult = extractResult;
this.#renderFn = state.config.render; this.#renderFn = state.config.render;
this.#plugins = state.config.plugins; this.#plugins = state.config.plugins;
this.#dev = state.config.dev;
const dev = state.config.dev; this.#builder = snapshot;
this.#dev = dev;
this.#builder = snapshot ?? new EsbuildBuilder({
buildID: BUILD_ID,
entrypoints: collectEntrypoints(
dev,
extractResult.islands,
this.#plugins,
),
configPath,
dev,
jsxConfig,
target: state.config.build.target,
absoluteWorkingDir: Deno.cwd(),
});
} }
/** /**