diff --git a/.changeset/nine-impalas-wave.md b/.changeset/nine-impalas-wave.md new file mode 100644 index 00000000..492320ae --- /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. 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 deed6b9a..1760ba48 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -3,11 +3,12 @@ import { parseArgs } from "node:util"; import { resolve } from "node:path"; export function getArgs(): { - skipBuild: boolean; + skipNextBuild: boolean; outputDir?: string; + minify: boolean; } { const { - values: { skipBuild, output }, + values: { skipBuild, output, noMinify }, } = parseArgs({ options: { skipBuild: { @@ -19,6 +20,10 @@ export function getArgs(): { type: "string", short: "o", }, + noMinify: { + type: "boolean", + default: false, + }, }, allowPositionals: false, }); @@ -31,7 +36,8 @@ 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/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..59256e8a 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: { @@ -63,7 +65,8 @@ export function getConfig(projectOpts: ProjectOptions): Config { return { build: { timestamp: Date.now(), - skipNextBuild: !!projectOpts.skipBuild, + skipNextBuild: projectOpts.skipNextBuild, + shouldMinify: projectOpts.minify, }, paths: { @@ -100,7 +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; }; /** diff --git a/packages/cloudflare/src/cli/index.ts b/packages/cloudflare/src/cli/index.ts index 05da5e27..3c40e1d2 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 { skipNextBuild, outputDir, minify } = getArgs(); await build({ sourceDir: nextAppDir, outputDir: resolve(outputDir ?? nextAppDir, ".worker-next"), - skipBuild, + skipNextBuild, + minify, });