Skip to content

Commit 1bdb6f0

Browse files
committed
add copyFiles
1 parent 8392e22 commit 1bdb6f0

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ CMD ["node", "index.mjs"]
329329
`,
330330
);
331331
}
332+
333+
if (fnOptions.copyFiles) {
334+
buildHelper.copyCustomFiles(fnOptions.copyFiles, outPackagePath);
335+
}
332336
}
333337

334338
function shouldGenerateDockerfile(options: FunctionOptions) {

packages/open-next/src/build/edge/createEdgeBundle.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import { openNextReplacementPlugin } from "../../plugins/replacement.js";
2323
import { openNextResolvePlugin } from "../../plugins/resolve.js";
2424
import { getCrossPlatformPathRegex } from "../../utils/regex.js";
2525
import { type BuildOptions, isEdgeRuntime } from "../helper.js";
26-
import { copyOpenNextConfig, esbuildAsync } from "../helper.js";
26+
import {
27+
copyCustomFiles,
28+
copyOpenNextConfig,
29+
esbuildAsync,
30+
} from "../helper.js";
2731

2832
type Override = OverrideOptions & {
2933
originResolver?: LazyLoadedOverride<OriginResolver> | IncludedOriginResolver;
@@ -221,6 +225,10 @@ export async function generateEdgeBundle(
221225
name,
222226
additionalPlugins,
223227
});
228+
229+
if (fnOptions.copyFiles) {
230+
copyCustomFiles(fnOptions.copyFiles, outputDir);
231+
}
224232
}
225233

226234
/**

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import url from "node:url";
66
import type { BuildOptions as ESBuildOptions } from "esbuild";
77
import { build as buildAsync, buildSync } from "esbuild";
88
import type {
9+
CopyFile,
910
DefaultOverrideOptions,
1011
OpenNextConfig,
1112
} from "types/open-next.js";
@@ -440,3 +441,30 @@ export async function isEdgeRuntime(
440441
export function getPackagePath(options: BuildOptions) {
441442
return path.relative(options.monorepoRoot, options.appBuildOutputPath);
442443
}
444+
445+
/**
446+
* Copy files that are specified in the `copyFiles` property of the OpenNext config into the output directory.
447+
*
448+
* @param copyFiles - Array of files to copy. Each file should have a `srcPath` and `dstPath` property.
449+
* @param outputPath - Path to the output directory.
450+
*/
451+
export function copyCustomFiles(copyFiles: CopyFile[], outputPath: string) {
452+
copyFiles.forEach(({ srcPath, dstPath }) => {
453+
if (!fs.existsSync(srcPath)) {
454+
logger.warn(
455+
`${srcPath} was not found. Make sure this file exists. Can be a relative path to the app directory or an absolute path.`,
456+
);
457+
return;
458+
}
459+
460+
// Create the destination directory if it doesn't exist
461+
const fullDestPath = path.join(outputPath, dstPath);
462+
const destDir = path.dirname(fullDestPath);
463+
if (!fs.existsSync(destDir)) {
464+
fs.mkdirSync(destDir, { recursive: true });
465+
}
466+
467+
fs.copyFileSync(srcPath, fullDestPath);
468+
logger.debug(`Copied ${srcPath} to ${fullDestPath}`);
469+
});
470+
}

packages/open-next/src/types/open-next.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ export interface DefaultFunctionOptions<
290290
install?: InstallOptions;
291291
}
292292

293+
/**
294+
* @srcPath The path to the file to copy. Can be absolute or relative path.
295+
* @dstPath The path to the destination in the server function. Must be a relative path.
296+
*/
297+
export interface CopyFile {
298+
srcPath: string;
299+
dstPath: string;
300+
}
301+
293302
export interface FunctionOptions extends DefaultFunctionOptions {
294303
/**
295304
* Runtime used
@@ -313,6 +322,20 @@ export interface FunctionOptions extends DefaultFunctionOptions {
313322
* @deprecated This is not supported in 14.2+
314323
*/
315324
experimentalBundledNextServer?: boolean;
325+
/**
326+
* Manually copy files into the server function after the build.
327+
* @copyFiles The files to copy. Is an array of objects with srcPath and dstPath.
328+
* @example
329+
* ```ts
330+
* copyFiles: [
331+
* {
332+
* srcPath: 'relativefile.txt',
333+
* dstPath: '/this/is/a/folder.txt',
334+
* },
335+
* ]
336+
* ```
337+
*/
338+
copyFiles?: CopyFile[];
316339
}
317340

318341
export type RouteTemplate =

0 commit comments

Comments
 (0)