Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

feat(lambda-at-edge): copy additional JS files to handlers #1970

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ class Builder {
}
}
),
this.copyJSFiles(DEFAULT_LAMBDA_CODE_DIR),
this.copyChunks(DEFAULT_LAMBDA_CODE_DIR),
fse.copy(
join(this.dotNextDir, "prerender-manifest.json"),
Expand Down Expand Up @@ -411,6 +412,7 @@ class Builder {
join(this.serverlessDir, "pages/api"),
join(this.outputDir, API_LAMBDA_CODE_DIR, "pages/api")
),
this.copyJSFiles(API_LAMBDA_CODE_DIR),
this.copyChunks(API_LAMBDA_CODE_DIR),
fse.writeJson(
join(this.outputDir, API_LAMBDA_CODE_DIR, "manifest.json"),
Expand Down Expand Up @@ -438,6 +440,7 @@ class Builder {
join(this.outputDir, REGENERATION_LAMBDA_CODE_DIR),
!!this.buildOptions.minifyHandlers
),
this.copyJSFiles(REGENERATION_LAMBDA_CODE_DIR),
this.copyChunks(REGENERATION_LAMBDA_CODE_DIR),
fse.copy(
join(this.serverlessDir, "pages"),
Expand All @@ -462,16 +465,36 @@ class Builder {
/**
* copy chunks if present and not using serverless trace
*/
copyChunks(buildDir: string): Promise<void> {
copyChunks(handlerDir: string): Promise<void> {
return !this.buildOptions.useServerlessTraceTarget &&
fse.existsSync(join(this.serverlessDir, "chunks"))
? fse.copy(
join(this.serverlessDir, "chunks"),
join(this.outputDir, buildDir, "chunks")
join(this.outputDir, handlerDir, "chunks")
)
: Promise.resolve();
}

/**
* Copy additional JS files needed such as webpack-runtime.js (new in Next.js 12)
*/
async copyJSFiles(handlerDir: string): Promise<void> {
await Promise.all([
(await fse.pathExists(join(this.serverlessDir, "webpack-api-runtime.js")))
? fse.copy(
join(this.serverlessDir, "webpack-api-runtime.js"),
join(this.outputDir, handlerDir, "webpack-api-runtime.js")
)
: Promise.resolve(),
(await fse.pathExists(join(this.serverlessDir, "webpack-runtime.js")))
? fse.copy(
join(this.serverlessDir, "webpack-runtime.js"),
join(this.outputDir, handlerDir, "webpack-runtime.js")
)
: Promise.resolve()
]);
}

/**
* Build image optimization lambda (supported by Next.js 10)
* @param buildManifest
Expand Down
8 changes: 6 additions & 2 deletions packages/libs/lambda-at-edge/tests/build/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ describe("Builder Tests", () => {
"pages",
"prerender-manifest.json",
"routes-manifest.json",
"testFile.js"
"testFile.js",
"webpack-api-runtime.js",
"webpack-runtime.js"
])
);

Expand All @@ -532,7 +534,9 @@ describe("Builder Tests", () => {
"chunks",
"pages",
"routes-manifest.json",
"testFile.js"
"testFile.js",
"webpack-api-runtime.js",
"webpack-runtime.js"
])
);
});
Expand Down