Skip to content

Commit

Permalink
feat: cli arg to disable minification (#89)
Browse files Browse the repository at this point in the history
* feat: cli arg to disable minification

* rename disableMinification to noMinify

* comment

* tidy
  • Loading branch information
james-elicx authored Oct 11, 2024
1 parent 450efc7 commit 6acf0fd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changeset/nine-impalas-wave.md
Original file line number Diff line number Diff line change
@@ -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`.
12 changes: 9 additions & 3 deletions packages/cloudflare/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -19,6 +20,10 @@ export function getArgs(): {
type: "string",
short: "o",
},
noMinify: {
type: "boolean",
default: false,
},
},
allowPositionals: false,
});
Expand All @@ -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,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { rm } from "node:fs/promises";
* @param projectOpts The options for the project
*/
export async function build(projectOpts: ProjectOptions): Promise<void> {
if (!projectOpts.skipBuild) {
if (!projectOpts.skipNextBuild) {
// Build the next app
await buildNextjsApp(projectOpts.sourceDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function patchCache(code: string, config: Config): Promise<string>
outfile: cacheHandlerOutputFile,
format: "esm",
target: "esnext",
minify: true,
minify: config.build.shouldMinify,
define: {
"process.env.__OPENNEXT_KV_BINDING_NAME": `"${config.cache.kvBindingName}"`,
},
Expand Down
9 changes: 7 additions & 2 deletions packages/cloudflare/src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
};

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/cloudflare/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

0 comments on commit 6acf0fd

Please sign in to comment.