Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional Pages Functions options for internal builds #223

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-fans-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Add ability to compile a directory other than `functions` for `wrangler pages functions build`.
5 changes: 5 additions & 0 deletions .changeset/young-timers-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Add `--output-config-path` option to `wrangler pages functions build` which writes a config file describing the `functions` folder.
87 changes: 54 additions & 33 deletions packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from "assert";
import type { BuilderCallback } from "yargs";
import { join } from "path";
import { tmpdir } from "os";
import { existsSync, lstatSync, readFileSync } from "fs";
import { existsSync, lstatSync, readFileSync, writeFileSync } from "fs";
import { execSync, spawn } from "child_process";
import { URL } from "url";
import { getType } from "mime";
Expand Down Expand Up @@ -642,6 +642,7 @@ const RUNNING_BUILDERS: BuildResult[] = [];

async function buildFunctions({
scriptPath,
outputConfigPath,
functionsDirectory,
minify = false,
sourcemap = false,
Expand All @@ -650,6 +651,7 @@ async function buildFunctions({
onEnd,
}: {
scriptPath: string;
outputConfigPath?: string;
functionsDirectory: string;
minify?: boolean;
sourcemap?: boolean;
Expand All @@ -668,6 +670,13 @@ async function buildFunctions({
baseURL: "/",
});

if (outputConfigPath) {
writeFileSync(
outputConfigPath,
JSON.stringify({ ...config, baseURL: "/" }, null, 2)
);
}

await writeRoutesModule({
config,
srcDir: functionsDirectory,
Expand Down Expand Up @@ -949,49 +958,61 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
)
.command("functions", "Cloudflare Pages Functions", (yargs) =>
yargs.command(
"build",
"build [directory]",
"Compile a folder of Cloudflare Pages Functions into a single Worker",
(yargs) =>
yargs.options({
"script-path": {
type: "string",
default: "_worker.js",
description: "The location of the output Worker script",
},
minify: {
type: "boolean",
default: false,
description: "Minify the output Worker script",
},
sourcemap: {
type: "boolean",
default: false,
description: "Generate a sourcemap for the output Worker script",
},
"fallback-service": {
yargs
.positional("directory", {
type: "string",
default: "ASSETS",
description:
"The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
},
watch: {
type: "boolean",
default: false,
description:
"Watch for changes to the functions and automatically rebuild the Worker script",
},
}),
default: "functions",
description: "The directory of Pages Functions",
})
.options({
"script-path": {
type: "string",
default: "_worker.js",
description: "The location of the output Worker script",
},
"output-config-path": {
type: "string",
description: "The location for the output config file",
},
minify: {
type: "boolean",
default: false,
description: "Minify the output Worker script",
},
sourcemap: {
type: "boolean",
default: false,
description:
"Generate a sourcemap for the output Worker script",
},
"fallback-service": {
type: "string",
default: "ASSETS",
description:
"The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
},
watch: {
type: "boolean",
default: false,
description:
"Watch for changes to the functions and automatically rebuild the Worker script",
},
}),
async ({
directory,
"script-path": scriptPath,
"output-config-path": outputConfigPath,
minify,
sourcemap,
fallbackService,
}) => {
const functionsDirectory = "./functions";

await buildFunctions({
scriptPath,
functionsDirectory,
outputConfigPath,
functionsDirectory: directory,
minify,
sourcemap,
fallbackService,
Expand Down