Skip to content

Commit

Permalink
Add additional Pages Functions options for internal builds (#223)
Browse files Browse the repository at this point in the history
* Add Pages Functions config output option

* Add ability to compile directory other than functions for wrangler pages functions build
  • Loading branch information
GregBrimble authored Jan 10, 2022
1 parent b901bf7 commit a979d55
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
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,50 +958,62 @@ 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,
watch,
}) => {
const functionsDirectory = "./functions";

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

0 comments on commit a979d55

Please sign in to comment.