From 60cad48e10c99f859ee723e960f5e61e52bd4a54 Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Sat, 8 Jan 2022 22:36:59 +0000 Subject: [PATCH 1/2] Add Pages Functions config output option --- .changeset/young-timers-talk.md | 5 +++++ packages/wrangler/src/pages.tsx | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .changeset/young-timers-talk.md 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 95b238e1c1b3..ef0cd88b8486 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, @@ -958,6 +967,10 @@ export const pages: BuilderCallback = (yargs) => { 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, @@ -983,6 +996,7 @@ export const pages: BuilderCallback = (yargs) => { }), async ({ "script-path": scriptPath, + "output-config-path": outputConfigPath, minify, sourcemap, fallbackService, @@ -991,6 +1005,7 @@ export const pages: BuilderCallback = (yargs) => { await buildFunctions({ scriptPath, + outputConfigPath, functionsDirectory, minify, sourcemap, From 665484ce2c947d81603a1c53335460defa5509b0 Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Mon, 10 Jan 2022 12:39:04 +0000 Subject: [PATCH 2/2] Add ability to compile directory other than functions for wrangler pages functions build --- .changeset/eight-fans-drum.md | 5 +++ packages/wrangler/src/pages.tsx | 78 ++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 .changeset/eight-fans-drum.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/packages/wrangler/src/pages.tsx b/packages/wrangler/src/pages.tsx index ef0cd88b8486..9cf72d66dc1a 100644 --- a/packages/wrangler/src/pages.tsx +++ b/packages/wrangler/src/pages.tsx @@ -958,55 +958,61 @@ 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", - }, - "output-config-path": { + yargs + .positional("directory", { 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", - }, - }), + 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, outputConfigPath, - functionsDirectory, + functionsDirectory: directory, minify, sourcemap, fallbackService,