Skip to content

Commit 1522cd9

Browse files
committed
add dev command
1 parent c212ed0 commit 1522cd9

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

packages/open-next/src/build.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { spawnSync } from "node:child_process";
2+
import path from "node:path";
13
import url from "node:url";
24

35
import {
@@ -25,6 +27,7 @@ export type PublicFiles = {
2527
export async function build(
2628
openNextConfigPath?: string,
2729
nodeExternals?: string,
30+
dev = false,
2831
) {
2932
showWarningOnWindows();
3033

@@ -35,6 +38,7 @@ export async function build(
3538
baseDir,
3639
openNextConfigPath,
3740
{ nodeExternals },
41+
dev,
3842
);
3943

4044
// Initialize options
@@ -80,4 +84,24 @@ export async function build(
8084
await createWarmerBundle(options);
8185
await generateOutput(options);
8286
logger.info("OpenNext build complete.");
87+
88+
if (dev) {
89+
printHeader("Starting development server");
90+
spawnSync(
91+
"node",
92+
[
93+
path.join(
94+
options.outputDir,
95+
"server-functions",
96+
"default",
97+
"index.mjs",
98+
),
99+
],
100+
{
101+
stdio: "inherit",
102+
shell: process.platform === "win32",
103+
},
104+
);
105+
process.exit(0);
106+
}
83107
}

packages/open-next/src/build/compileConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { buildSync } from "esbuild";
66
import type { OpenNextConfig } from "types/open-next.js";
77

88
import logger from "../logger.js";
9+
import { buildDevConfig } from "./helper.js";
910
import { validateConfig } from "./validateConfig.js";
1011

1112
/**
@@ -23,6 +24,7 @@ export async function compileOpenNextConfig(
2324
baseDir: string,
2425
openNextConfigPath?: string,
2526
{ nodeExternals = "", compileEdge = false } = {},
27+
dev = false,
2628
) {
2729
const sourcePath = path.join(
2830
baseDir,
@@ -34,6 +36,7 @@ export async function compileOpenNextConfig(
3436
sourcePath,
3537
buildDir,
3638
nodeExternals.split(","),
39+
dev,
3740
);
3841

3942
// On Windows, we need to use file:// protocol to load the config file using import()
@@ -69,10 +72,16 @@ export function compileOpenNextConfigNode(
6972
sourcePath: string,
7073
outputDir: string,
7174
externals: string[],
75+
dev = false,
7276
) {
7377
const outputPath = path.join(outputDir, "open-next.config.mjs");
7478
logger.debug("Compiling open-next.config.ts for Node.", outputPath);
7579

80+
if (dev) {
81+
buildDevConfig(outputPath, externals);
82+
return outputPath;
83+
}
84+
7685
//Check if open-next.config.ts exists
7786
if (!fs.existsSync(sourcePath)) {
7887
//Create a simple open-next.config.mjs file
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
//TODO: Move all other manifest path here as well
22
export const MIDDLEWARE_TRACE_FILE = "server/middleware.js.nft.json";
33
export const INSTRUMENTATION_TRACE_FILE = "server/instrumentation.js.nft.json";
4+
5+
// This is an OpenNext config to run the default server function locally
6+
// https://opennext.js.org/aws/contribute/local_run
7+
export const DEV_CONFIG = `
8+
export default {
9+
default: {
10+
override: {
11+
wrapper: "express-dev",
12+
converter: "node",
13+
incrementalCache: "fs-dev",
14+
queue: "direct",
15+
tagCache: "fs-dev",
16+
},
17+
},
18+
imageOptimization: {
19+
override: {
20+
wrapper: "dummy",
21+
converter: "dummy",
22+
},
23+
loader: "fs-dev",
24+
install: {
25+
arch: "x64",
26+
packages: ["sharp"],
27+
},
28+
},
29+
}`;

packages/open-next/src/build/helper.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
} from "types/open-next.js";
1212

1313
import logger from "../logger.js";
14+
import { DEV_CONFIG } from "./constant.js";
1415

1516
const require = createRequire(import.meta.url);
1617
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
@@ -440,3 +441,28 @@ export async function isEdgeRuntime(
440441
export function getPackagePath(options: BuildOptions) {
441442
return path.relative(options.monorepoRoot, options.appBuildOutputPath);
442443
}
444+
445+
export function buildDevConfig(outputPath: string, externals: string[]) {
446+
buildSync({
447+
stdin: {
448+
contents: DEV_CONFIG,
449+
resolveDir: process.cwd(),
450+
loader: "ts",
451+
sourcefile: "open-next.config.ts",
452+
},
453+
outfile: outputPath,
454+
bundle: true,
455+
format: "esm",
456+
target: ["node18"],
457+
external: externals,
458+
platform: "node",
459+
banner: {
460+
js: [
461+
"import { createRequire as topLevelCreateRequire } from 'module';",
462+
"const require = topLevelCreateRequire(import.meta.url);",
463+
"import bannerUrl from 'url';",
464+
"const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));",
465+
].join(""),
466+
},
467+
});
468+
}

packages/open-next/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import { build } from "./build.js";
44

55
const command = process.argv[2];
6-
if (command !== "build") printHelp();
6+
if (command !== "build" && command !== "dev") printHelp();
77

88
const args = parseArgs();
99
if (Object.keys(args).includes("--help")) printHelp();
1010

11-
await build(args["--config-path"], args["--node-externals"]);
11+
if (command === "dev") {
12+
await build(undefined, args["--node-externals"], true);
13+
} else if (command === "build") {
14+
await build(args["--config-path"], args["--node-externals"]);
15+
}
1216

1317
function parseArgs() {
1418
return process.argv.slice(2).reduce(

0 commit comments

Comments
 (0)