Skip to content

Commit

Permalink
feat(lambda-at-edge): copy additional JS files to handlers (#1970)
Browse files Browse the repository at this point in the history
  • Loading branch information
dphang committed Oct 27, 2021
1 parent 6e27aa9 commit e7c81ef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
27 changes: 25 additions & 2 deletions packages/libs/lambda-at-edge/src/build.ts
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
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

0 comments on commit e7c81ef

Please sign in to comment.