feat: add build.target option (#1798)

This commit is contained in:
Marvin Hagemeister
2023-09-20 11:05:46 +02:00
committed by GitHub
parent 3fd8fe89d3
commit 0c37b5f9b9
11 changed files with 106 additions and 1 deletions
+2 -1
View File
@@ -20,6 +20,7 @@ export interface EsbuildBuilderOptions {
configPath: string;
/** The JSX configuration. */
jsxConfig: JSXConfig;
target: string | string[];
}
export interface JSXConfig {
@@ -55,7 +56,7 @@ export class EsbuildBuilder implements Builder {
entryPoints: opts.entrypoints,
platform: "browser",
target: ["chrome99", "firefox99", "safari15"],
target: this.#options.target,
format: "esm",
bundle: true,
+1
View File
@@ -61,6 +61,7 @@ export async function getFreshConfigWithDefaults(
manifest,
build: {
outDir: "",
target: opts.build?.target ?? ["chrome99", "firefox99", "safari15"],
},
plugins: opts.plugins ?? [],
staticDir: "",
+3
View File
@@ -382,6 +382,7 @@ export async function getServerContext(opts: InternalFreshOptions) {
jsxConfig,
opts.dev,
opts.router ?? DEFAULT_ROUTER_OPTIONS,
opts.build.target,
snapshot,
);
}
@@ -416,6 +417,7 @@ export class ServerContext {
jsxConfig: JSXConfig,
dev: boolean,
routerOptions: RouterOptions,
target: string | string[],
snapshot: BuildSnapshot | null = null,
) {
this.#routes = routes;
@@ -435,6 +437,7 @@ export class ServerContext {
configPath,
dev: this.#dev,
jsxConfig,
target,
});
this.#routerOptions = routerOptions;
}
+8
View File
@@ -32,6 +32,13 @@ export interface FreshOptions {
* This can be an absolute path, a file URL or a relative path.
*/
outDir?: string;
/**
* This sets the target environment for the generated code. Newer
* language constructs will be transformed to match the specified
* support range. See https://esbuild.github.io/api/#target
* @default {"es2022"}
*/
target?: string | string[];
};
render?: RenderFunction;
plugins?: Plugin[];
@@ -47,6 +54,7 @@ export interface InternalFreshOptions {
manifest: Manifest;
build: {
outDir: string;
target: string | string[];
};
render?: RenderFunction;
plugins: Plugin[];
+31
View File
@@ -229,3 +229,34 @@ Deno.test(
});
},
);
Deno.test("pass target options", async () => {
const fixture = path.join(Deno.cwd(), "tests", "fixture_build_target");
const out = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
path.join(fixture, "dev.ts"),
"build",
],
env: {
GITHUB_SHA: "__BUILD_ID__",
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
FRESH_TEST_TARGET: "es2015",
},
stdin: "null",
stdout: "piped",
stderr: "piped",
}).output();
const { stdout, stderr } = getStdOutput(out);
const txt = await Deno.readTextFile(
path.join(fixture, "_fresh", "island-counter_default.js"),
);
assertNotMatch(
txt,
/\?\?/,
`Asset contained ?? despite target es2015\n\n${stdout}\n${stderr}`,
);
});
+15
View File
@@ -0,0 +1,15 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts", {
build: {
target: Deno.env.get("FRESH_TEST_TARGET"),
},
});
+18
View File
@@ -0,0 +1,18 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
@@ -0,0 +1,4 @@
export default function Counter(props: { text?: string }) {
const text = props.text ?? "check output";
return <h1>{text}</h1>;
}
+10
View File
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
await start(routes);
@@ -0,0 +1,5 @@
import Counter from "../islands/Counter.tsx";
export default function Home() {
return <Counter />;
}