Skip to content

Commit 7e8f462

Browse files
committed
add helper function
1 parent b7a8953 commit 7e8f462

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

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

+7-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { buildSync } from "esbuild";
66
import type { OpenNextConfig } from "types/open-next.js";
77

88
import logger from "../logger.js";
9-
import { DEV_CONFIG } from "./constant.js";
9+
import { buildDevConfig } from "./helper.js";
1010
import { validateConfig } from "./validateConfig.js";
1111

1212
/**
@@ -77,25 +77,19 @@ export function compileOpenNextConfigNode(
7777
const outputPath = path.join(outputDir, "open-next.config.mjs");
7878
logger.debug("Compiling open-next.config.ts for Node.", outputPath);
7979

80+
if (dev) {
81+
buildDevConfig(outputPath, externals);
82+
return outputPath;
83+
}
84+
8085
//Check if open-next.config.ts exists
8186
if (!fs.existsSync(sourcePath)) {
8287
//Create a simple open-next.config.mjs file
8388
logger.debug("Cannot find open-next.config.ts. Using default config.");
8489
fs.writeFileSync(outputPath, "export default { default: { } };");
8590
} else {
8691
buildSync({
87-
...(dev
88-
? {
89-
stdin: {
90-
contents: DEV_CONFIG,
91-
resolveDir: process.cwd(),
92-
loader: "ts",
93-
sourcefile: "open-next.config.ts",
94-
},
95-
}
96-
: {
97-
entryPoints: [sourcePath],
98-
}),
92+
entryPoints: [sourcePath],
9993
outfile: outputPath,
10094
bundle: true,
10195
format: "esm",

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

+26
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+
}

0 commit comments

Comments
 (0)