feat: support custom server entry (#3216)
This commit is contained in:
@@ -31,6 +31,8 @@ const builder = new Builder({
|
||||
// The root directory of the project. All other paths will be resolved
|
||||
// against this if they're relative. (Default: `Deno.cwd()`)
|
||||
root?: string;
|
||||
// The path to your server entry point. (Default: `<root>/main.ts`)
|
||||
serverEntry?: string;
|
||||
// Where to write generated files when doing a production build.
|
||||
// (default: `<root>/_fresh/`)
|
||||
outDir?: string;
|
||||
|
||||
@@ -75,6 +75,14 @@ export interface BuildOptions {
|
||||
* @default "routes"
|
||||
*/
|
||||
routeDir?: string;
|
||||
/**
|
||||
* The entrypoint for your server.
|
||||
*
|
||||
* This can be an absolute path, a file URL or a relative path.
|
||||
* Relative paths are resolved against the `root` option.
|
||||
* @default "main.ts"
|
||||
*/
|
||||
serverEntry?: string;
|
||||
/**
|
||||
* File paths which should be ignored when crawling the file system.
|
||||
*/
|
||||
@@ -109,6 +117,7 @@ export class Builder<State = any> {
|
||||
|
||||
constructor(options?: BuildOptions) {
|
||||
const root = parseDirPath(options?.root ?? ".", Deno.cwd());
|
||||
const serverEntry = parseDirPath(options?.serverEntry ?? "main.ts", root);
|
||||
const outDir = parseDirPath(options?.outDir ?? "_fresh", root);
|
||||
const staticDir = parseDirPath(options?.staticDir ?? "static", root);
|
||||
const islandDir = parseDirPath(options?.islandDir ?? "islands", root);
|
||||
@@ -119,6 +128,7 @@ export class Builder<State = any> {
|
||||
this.#transformer = new FileTransformer(fsAdapter, root);
|
||||
|
||||
this.config = {
|
||||
serverEntry,
|
||||
target: options?.target ?? ["chrome99", "firefox99", "safari15"],
|
||||
root,
|
||||
outDir,
|
||||
|
||||
@@ -503,6 +503,33 @@ export const app = new App()
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Builder - custom server entry",
|
||||
fn: async () => {
|
||||
const root = path.join(import.meta.dirname!, "..", "..");
|
||||
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
|
||||
const tmp = _tmp.dir;
|
||||
|
||||
await writeFiles(tmp, {
|
||||
"other.ts": `import { App, staticFiles } from "fresh";
|
||||
export const app = new App()
|
||||
.get("/", () => new Response("ok"));`,
|
||||
});
|
||||
|
||||
await new Builder({ root: tmp, serverEntry: "other.ts" }).build();
|
||||
|
||||
await withChildProcessServer(
|
||||
{ cwd: tmp, args: ["serve", "-A", "--port=0", "_fresh/server.js"] },
|
||||
async (address) => {
|
||||
const res = await fetch(`${address}`);
|
||||
expect(await res.text()).toEqual("ok");
|
||||
},
|
||||
);
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "Builder - dev server error_overlay ignores _app and _layout",
|
||||
fn: async () => {
|
||||
|
||||
@@ -366,9 +366,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
|
||||
path.join(outDir, "server.js"),
|
||||
generateServerEntry({
|
||||
root: appPath,
|
||||
serverEntry: path
|
||||
.join(appPath, "main.ts")
|
||||
.replaceAll(/[\\/]+/g, "/"),
|
||||
serverEntry: pathToSpec(outDir, this.#config.serverEntry),
|
||||
snapshotSpecifier: "./snapshot.js",
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -10,6 +10,7 @@ Deno.test({
|
||||
await using _tmp = await withTmpDir();
|
||||
const tmp = _tmp.dir;
|
||||
const config: ResolvedBuildConfig = {
|
||||
serverEntry: "main.ts",
|
||||
root: tmp,
|
||||
mode: "development",
|
||||
buildId: "",
|
||||
|
||||
Reference in New Issue
Block a user