From 0ad33dfe48aa974ac316479120cb9a7146f23a47 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 10 Oct 2024 22:47:24 +0100 Subject: [PATCH 1/4] feat: cli arg to disable minification --- .changeset/nine-impalas-wave.md | 7 +++++++ packages/cloudflare/src/cli/args.ts | 7 ++++++- .../src/cli/build/patches/investigated/patch-cache.ts | 2 +- packages/cloudflare/src/cli/config.ts | 5 +++++ packages/cloudflare/src/cli/index.ts | 3 ++- 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 .changeset/nine-impalas-wave.md diff --git a/.changeset/nine-impalas-wave.md b/.changeset/nine-impalas-wave.md new file mode 100644 index 00000000..320f29a2 --- /dev/null +++ b/.changeset/nine-impalas-wave.md @@ -0,0 +1,7 @@ +--- +"@opennextjs/cloudflare": minor +--- + +feat: cli arg to disable minification + +The cache handler currently forces minification. There is now a CLI arg to disable minification for the build. At the moment, this only applies to the cache handler but may be used for other parts of the build in the future when minification is introduced to them. diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index deed6b9a..2c1a2aa3 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -5,9 +5,10 @@ import { resolve } from "node:path"; export function getArgs(): { skipBuild: boolean; outputDir?: string; + disableMinification?: boolean; } { const { - values: { skipBuild, output }, + values: { skipBuild, output, disableMinification }, } = parseArgs({ options: { skipBuild: { @@ -19,6 +20,9 @@ export function getArgs(): { type: "string", short: "o", }, + disableMinification: { + type: "boolean", + }, }, allowPositionals: false, }); @@ -32,6 +36,7 @@ export function getArgs(): { return { outputDir, skipBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), + disableMinification, }; } diff --git a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts index d1ea1b01..9e12225d 100644 --- a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts +++ b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts @@ -26,7 +26,7 @@ export async function patchCache(code: string, config: Config): Promise outfile: cacheHandlerOutputFile, format: "esm", target: "esnext", - minify: true, + minify: config.build.shouldMinify, define: { "process.env.__OPENNEXT_KV_BINDING_NAME": `"${config.cache.kvBindingName}"`, }, diff --git a/packages/cloudflare/src/cli/config.ts b/packages/cloudflare/src/cli/config.ts index c6fb233a..d00955b8 100644 --- a/packages/cloudflare/src/cli/config.ts +++ b/packages/cloudflare/src/cli/config.ts @@ -9,6 +9,8 @@ export type Config = { timestamp: number; // Whether to skip building the Next.js app or not skipNextBuild: boolean; + // Whether minification should be enabled or not + shouldMinify: boolean; }; paths: { @@ -64,6 +66,7 @@ export function getConfig(projectOpts: ProjectOptions): Config { build: { timestamp: Date.now(), skipNextBuild: !!projectOpts.skipBuild, + shouldMinify: !projectOpts.disableMinification, }, paths: { @@ -101,6 +104,8 @@ export type ProjectOptions = { outputDir: string; // Whether the Next.js build should be skipped (i.e. if the `.next` dir is already built) skipBuild?: boolean; + // Whether minification of the worker should be disabled + disableMinification?: boolean; }; /** diff --git a/packages/cloudflare/src/cli/index.ts b/packages/cloudflare/src/cli/index.ts index 05da5e27..6a5a4169 100644 --- a/packages/cloudflare/src/cli/index.ts +++ b/packages/cloudflare/src/cli/index.ts @@ -16,10 +16,11 @@ if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`) process.exit(1); } -const { skipBuild, outputDir } = getArgs(); +const { skipBuild, outputDir, disableMinification } = getArgs(); await build({ sourceDir: nextAppDir, outputDir: resolve(outputDir ?? nextAppDir, ".worker-next"), skipBuild, + disableMinification, }); From c17c24271cba43a145f426f8d35075f8a9e7f871 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 11 Oct 2024 08:41:35 +0100 Subject: [PATCH 2/4] rename disableMinification to noMinify --- packages/cloudflare/src/cli/args.ts | 8 ++++---- packages/cloudflare/src/cli/config.ts | 4 ++-- packages/cloudflare/src/cli/index.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 2c1a2aa3..87fc57ec 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -5,10 +5,10 @@ import { resolve } from "node:path"; export function getArgs(): { skipBuild: boolean; outputDir?: string; - disableMinification?: boolean; + noMinify?: boolean; } { const { - values: { skipBuild, output, disableMinification }, + values: { skipBuild, output, noMinify }, } = parseArgs({ options: { skipBuild: { @@ -20,7 +20,7 @@ export function getArgs(): { type: "string", short: "o", }, - disableMinification: { + noMinify: { type: "boolean", }, }, @@ -36,7 +36,7 @@ export function getArgs(): { return { outputDir, skipBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), - disableMinification, + noMinify, }; } diff --git a/packages/cloudflare/src/cli/config.ts b/packages/cloudflare/src/cli/config.ts index d00955b8..88298aab 100644 --- a/packages/cloudflare/src/cli/config.ts +++ b/packages/cloudflare/src/cli/config.ts @@ -66,7 +66,7 @@ export function getConfig(projectOpts: ProjectOptions): Config { build: { timestamp: Date.now(), skipNextBuild: !!projectOpts.skipBuild, - shouldMinify: !projectOpts.disableMinification, + shouldMinify: !projectOpts.noMinify, }, paths: { @@ -105,7 +105,7 @@ export type ProjectOptions = { // Whether the Next.js build should be skipped (i.e. if the `.next` dir is already built) skipBuild?: boolean; // Whether minification of the worker should be disabled - disableMinification?: boolean; + noMinify?: boolean; }; /** diff --git a/packages/cloudflare/src/cli/index.ts b/packages/cloudflare/src/cli/index.ts index 6a5a4169..b223eb1b 100644 --- a/packages/cloudflare/src/cli/index.ts +++ b/packages/cloudflare/src/cli/index.ts @@ -16,11 +16,11 @@ if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`) process.exit(1); } -const { skipBuild, outputDir, disableMinification } = getArgs(); +const { skipBuild, outputDir, noMinify } = getArgs(); await build({ sourceDir: nextAppDir, outputDir: resolve(outputDir ?? nextAppDir, ".worker-next"), skipBuild, - disableMinification, + noMinify, }); From 0dfd7026253fdd891fd5d959bce83a628d107116 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 11 Oct 2024 09:16:21 +0100 Subject: [PATCH 3/4] comment --- .changeset/nine-impalas-wave.md | 2 +- packages/cloudflare/src/cli/args.ts | 4 ++-- packages/cloudflare/src/cli/config.ts | 6 +++--- packages/cloudflare/src/cli/index.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.changeset/nine-impalas-wave.md b/.changeset/nine-impalas-wave.md index 320f29a2..492320ae 100644 --- a/.changeset/nine-impalas-wave.md +++ b/.changeset/nine-impalas-wave.md @@ -4,4 +4,4 @@ feat: cli arg to disable minification -The cache handler currently forces minification. There is now a CLI arg to disable minification for the build. At the moment, this only applies to the cache handler but may be used for other parts of the build in the future when minification is introduced to them. +The cache handler currently forces minification. There is now a CLI arg to disable minification for the build. At the moment, this only applies to the cache handler but may be used for other parts of the build in the future when minification is introduced to them. By default, minification is enabled, but can be disabled by passing `--noMinify`. diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 87fc57ec..71717766 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -5,7 +5,7 @@ import { resolve } from "node:path"; export function getArgs(): { skipBuild: boolean; outputDir?: string; - noMinify?: boolean; + minify?: boolean; } { const { values: { skipBuild, output, noMinify }, @@ -36,7 +36,7 @@ export function getArgs(): { return { outputDir, skipBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), - noMinify, + minify: !noMinify, }; } diff --git a/packages/cloudflare/src/cli/config.ts b/packages/cloudflare/src/cli/config.ts index 88298aab..61c1fa56 100644 --- a/packages/cloudflare/src/cli/config.ts +++ b/packages/cloudflare/src/cli/config.ts @@ -66,7 +66,7 @@ export function getConfig(projectOpts: ProjectOptions): Config { build: { timestamp: Date.now(), skipNextBuild: !!projectOpts.skipBuild, - shouldMinify: !projectOpts.noMinify, + shouldMinify: !!projectOpts.minify, }, paths: { @@ -104,8 +104,8 @@ export type ProjectOptions = { outputDir: string; // Whether the Next.js build should be skipped (i.e. if the `.next` dir is already built) skipBuild?: boolean; - // Whether minification of the worker should be disabled - noMinify?: boolean; + // Whether minification of the worker should be enabled + minify?: boolean; }; /** diff --git a/packages/cloudflare/src/cli/index.ts b/packages/cloudflare/src/cli/index.ts index b223eb1b..f069f61c 100644 --- a/packages/cloudflare/src/cli/index.ts +++ b/packages/cloudflare/src/cli/index.ts @@ -16,11 +16,11 @@ if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`) process.exit(1); } -const { skipBuild, outputDir, noMinify } = getArgs(); +const { skipBuild, outputDir, minify } = getArgs(); await build({ sourceDir: nextAppDir, outputDir: resolve(outputDir ?? nextAppDir, ".worker-next"), skipBuild, - noMinify, + minify, }); From 2f5cb033233dabe694d55ba3a851b51cb4db9011 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 11 Oct 2024 15:18:10 +0100 Subject: [PATCH 4/4] tidy --- packages/cloudflare/src/cli/args.ts | 7 ++++--- packages/cloudflare/src/cli/build/index.ts | 2 +- packages/cloudflare/src/cli/config.ts | 8 ++++---- packages/cloudflare/src/cli/index.ts | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 71717766..1760ba48 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -3,9 +3,9 @@ import { parseArgs } from "node:util"; import { resolve } from "node:path"; export function getArgs(): { - skipBuild: boolean; + skipNextBuild: boolean; outputDir?: string; - minify?: boolean; + minify: boolean; } { const { values: { skipBuild, output, noMinify }, @@ -22,6 +22,7 @@ export function getArgs(): { }, noMinify: { type: "boolean", + default: false, }, }, allowPositionals: false, @@ -35,7 +36,7 @@ export function getArgs(): { return { outputDir, - skipBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), + skipNextBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), minify: !noMinify, }; } diff --git a/packages/cloudflare/src/cli/build/index.ts b/packages/cloudflare/src/cli/build/index.ts index ad991951..bd0577c6 100644 --- a/packages/cloudflare/src/cli/build/index.ts +++ b/packages/cloudflare/src/cli/build/index.ts @@ -14,7 +14,7 @@ import { rm } from "node:fs/promises"; * @param projectOpts The options for the project */ export async function build(projectOpts: ProjectOptions): Promise { - if (!projectOpts.skipBuild) { + if (!projectOpts.skipNextBuild) { // Build the next app await buildNextjsApp(projectOpts.sourceDir); } diff --git a/packages/cloudflare/src/cli/config.ts b/packages/cloudflare/src/cli/config.ts index 61c1fa56..59256e8a 100644 --- a/packages/cloudflare/src/cli/config.ts +++ b/packages/cloudflare/src/cli/config.ts @@ -65,8 +65,8 @@ export function getConfig(projectOpts: ProjectOptions): Config { return { build: { timestamp: Date.now(), - skipNextBuild: !!projectOpts.skipBuild, - shouldMinify: !!projectOpts.minify, + skipNextBuild: projectOpts.skipNextBuild, + shouldMinify: projectOpts.minify, }, paths: { @@ -103,9 +103,9 @@ export type ProjectOptions = { // The directory to save the output to (defaults to the app's directory) outputDir: string; // Whether the Next.js build should be skipped (i.e. if the `.next` dir is already built) - skipBuild?: boolean; + skipNextBuild: boolean; // Whether minification of the worker should be enabled - minify?: boolean; + minify: boolean; }; /** diff --git a/packages/cloudflare/src/cli/index.ts b/packages/cloudflare/src/cli/index.ts index f069f61c..3c40e1d2 100644 --- a/packages/cloudflare/src/cli/index.ts +++ b/packages/cloudflare/src/cli/index.ts @@ -16,11 +16,11 @@ if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`) process.exit(1); } -const { skipBuild, outputDir, minify } = getArgs(); +const { skipNextBuild, outputDir, minify } = getArgs(); await build({ sourceDir: nextAppDir, outputDir: resolve(outputDir ?? nextAppDir, ".worker-next"), - skipBuild, + skipNextBuild, minify, });