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
|
// The root directory of the project. All other paths will be resolved
|
||||||
// against this if they're relative. (Default: `Deno.cwd()`)
|
// against this if they're relative. (Default: `Deno.cwd()`)
|
||||||
root?: string;
|
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.
|
// Where to write generated files when doing a production build.
|
||||||
// (default: `<root>/_fresh/`)
|
// (default: `<root>/_fresh/`)
|
||||||
outDir?: string;
|
outDir?: string;
|
||||||
|
|||||||
@@ -75,6 +75,14 @@ export interface BuildOptions {
|
|||||||
* @default "routes"
|
* @default "routes"
|
||||||
*/
|
*/
|
||||||
routeDir?: string;
|
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.
|
* File paths which should be ignored when crawling the file system.
|
||||||
*/
|
*/
|
||||||
@@ -109,6 +117,7 @@ export class Builder<State = any> {
|
|||||||
|
|
||||||
constructor(options?: BuildOptions) {
|
constructor(options?: BuildOptions) {
|
||||||
const root = parseDirPath(options?.root ?? ".", Deno.cwd());
|
const root = parseDirPath(options?.root ?? ".", Deno.cwd());
|
||||||
|
const serverEntry = parseDirPath(options?.serverEntry ?? "main.ts", root);
|
||||||
const outDir = parseDirPath(options?.outDir ?? "_fresh", root);
|
const outDir = parseDirPath(options?.outDir ?? "_fresh", root);
|
||||||
const staticDir = parseDirPath(options?.staticDir ?? "static", root);
|
const staticDir = parseDirPath(options?.staticDir ?? "static", root);
|
||||||
const islandDir = parseDirPath(options?.islandDir ?? "islands", root);
|
const islandDir = parseDirPath(options?.islandDir ?? "islands", root);
|
||||||
@@ -119,6 +128,7 @@ export class Builder<State = any> {
|
|||||||
this.#transformer = new FileTransformer(fsAdapter, root);
|
this.#transformer = new FileTransformer(fsAdapter, root);
|
||||||
|
|
||||||
this.config = {
|
this.config = {
|
||||||
|
serverEntry,
|
||||||
target: options?.target ?? ["chrome99", "firefox99", "safari15"],
|
target: options?.target ?? ["chrome99", "firefox99", "safari15"],
|
||||||
root,
|
root,
|
||||||
outDir,
|
outDir,
|
||||||
|
|||||||
@@ -503,6 +503,33 @@ export const app = new App()
|
|||||||
sanitizeResources: false,
|
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({
|
Deno.test({
|
||||||
name: "Builder - dev server error_overlay ignores _app and _layout",
|
name: "Builder - dev server error_overlay ignores _app and _layout",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
@@ -366,9 +366,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
|
|||||||
path.join(outDir, "server.js"),
|
path.join(outDir, "server.js"),
|
||||||
generateServerEntry({
|
generateServerEntry({
|
||||||
root: appPath,
|
root: appPath,
|
||||||
serverEntry: path
|
serverEntry: pathToSpec(outDir, this.#config.serverEntry),
|
||||||
.join(appPath, "main.ts")
|
|
||||||
.replaceAll(/[\\/]+/g, "/"),
|
|
||||||
snapshotSpecifier: "./snapshot.js",
|
snapshotSpecifier: "./snapshot.js",
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ Deno.test({
|
|||||||
await using _tmp = await withTmpDir();
|
await using _tmp = await withTmpDir();
|
||||||
const tmp = _tmp.dir;
|
const tmp = _tmp.dir;
|
||||||
const config: ResolvedBuildConfig = {
|
const config: ResolvedBuildConfig = {
|
||||||
|
serverEntry: "main.ts",
|
||||||
root: tmp,
|
root: tmp,
|
||||||
mode: "development",
|
mode: "development",
|
||||||
buildId: "",
|
buildId: "",
|
||||||
|
|||||||
Reference in New Issue
Block a user