From a979d55feac1bdd340ec2b56710691837399183d Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Mon, 10 Jan 2022 13:06:48 +0000 Subject: [PATCH] Add additional Pages Functions options for internal builds (#223) * Add Pages Functions config output option * Add ability to compile directory other than functions for wrangler pages functions build --- .changeset/eight-fans-drum.md | 5 ++ .changeset/young-timers-talk.md | 5 ++ packages/wrangler/src/pages.tsx | 87 ++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 .changeset/eight-fans-drum.md create mode 100644 .changeset/young-timers-talk.md diff --git a/.changeset/eight-fans-drum.md b/.changeset/eight-fans-drum.md new file mode 100644 index 000000000000..1d273cbd469e --- /dev/null +++ b/.changeset/eight-fans-drum.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Add ability to compile a directory other than `functions` for `wrangler pages functions build`. diff --git a/.changeset/young-timers-talk.md b/.changeset/young-timers-talk.md new file mode 100644 index 000000000000..d05a8b57370c --- /dev/null +++ b/.changeset/young-timers-talk.md @@ -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. diff --git a/packages/wrangler/src/pages.tsx b/packages/wrangler/src/pages.tsx index 32dca0464d5a..56b622efa8d3 100644 --- a/packages/wrangler/src/pages.tsx +++ b/packages/wrangler/src/pages.tsx @@ -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"; @@ -642,6 +642,7 @@ const RUNNING_BUILDERS: BuildResult[] = []; async function buildFunctions({ scriptPath, + outputConfigPath, functionsDirectory, minify = false, sourcemap = false, @@ -650,6 +651,7 @@ async function buildFunctions({ onEnd, }: { scriptPath: string; + outputConfigPath?: string; functionsDirectory: string; minify?: boolean; sourcemap?: boolean; @@ -668,6 +670,13 @@ async function buildFunctions({ baseURL: "/", }); + if (outputConfigPath) { + writeFileSync( + outputConfigPath, + JSON.stringify({ ...config, baseURL: "/" }, null, 2) + ); + } + await writeRoutesModule({ config, srcDir: functionsDirectory, @@ -949,50 +958,62 @@ export const pages: BuilderCallback = (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,