This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda-at-edge): add bundledLambdas option for bundling them wit…
…h esbuild
- Loading branch information
Showing
9 changed files
with
401 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Runtime as LambdaRuntime } from "@aws-cdk/aws-lambda/lib/runtime"; | ||
import readDirectoryFiles from "@sls-next/core/dist/build/lib/readDirectoryFiles"; | ||
import type { ServerlessComponentInputs } from "@sls-next/serverless-component/types"; | ||
import { build } from "esbuild"; | ||
import glob from "fast-glob"; | ||
import fse from "fs-extra"; | ||
import path from "path"; | ||
|
||
const TARGET_FILE = "bundle.js"; | ||
|
||
type Runtime = NonNullable<ServerlessComponentInputs["runtime"]>; | ||
|
||
const getTarget = (input?: string): string => { | ||
switch (input) { | ||
case LambdaRuntime.NODEJS_14_X.name: | ||
return "node14"; | ||
case LambdaRuntime.NODEJS_10_X.name: | ||
return "node10"; | ||
case LambdaRuntime.NODEJS_12_X.name: | ||
default: | ||
return "node12"; | ||
} | ||
}; | ||
|
||
export const bundleLambda = async ( | ||
outputDir: string, | ||
handler: string, | ||
runtime: Runtime | null | ||
) => { | ||
const target = path.join(outputDir, handler); | ||
const index = path.join(target, "index.js"); | ||
const outfile = path.join(target, TARGET_FILE); | ||
|
||
const pathExists = await fse.pathExists(index); | ||
if (!pathExists) { | ||
throw `Failed to bundle \`${handler}\`, file \`${index}\` not found...`; | ||
} | ||
|
||
try { | ||
await build({ | ||
bundle: true, | ||
entryPoints: [index], | ||
external: ["sharp"], | ||
format: "cjs", | ||
legalComments: "none" /** Handler code is not distributed */, | ||
minify: true, | ||
outfile, | ||
platform: "node", | ||
target: getTarget( | ||
runtime | ||
? typeof runtime === "string" | ||
? runtime | ||
: runtime[handler as keyof typeof runtime] | ||
: undefined | ||
) | ||
}); | ||
} catch (error) { | ||
throw `Esbuild failed to bundle \`${handler}\`.`; | ||
} | ||
|
||
const outputFiles = await readDirectoryFiles(target, [ | ||
outfile, | ||
"**/BUILD_ID" | ||
]); | ||
|
||
// Remove all output files after bundling | ||
await Promise.all( | ||
Array.from(new Set(outputFiles)).map((file) => fse.remove(file.path)) | ||
); | ||
|
||
const emptyDirectories = await readDirectoryFiles( | ||
target, | ||
[outfile, "**/BUILD_ID"], | ||
false | ||
); | ||
|
||
// Remove all empty leftover directories | ||
await Promise.all( | ||
emptyDirectories.map((dir) => | ||
fse | ||
.remove(dir.path) | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
.catch(() => {}) | ||
) | ||
); | ||
}; |
53 changes: 53 additions & 0 deletions
53
...ages/libs/lambda-at-edge/tests/integration/with-empty-next-config/bundled-lambdas.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { remove, pathExists } from "fs-extra"; | ||
import path from "path"; | ||
import { getNextBinary } from "../../test-utils"; | ||
import os from "os"; | ||
import Builder from "../../../src/build"; | ||
|
||
jest.unmock("execa"); | ||
|
||
describe("Next.js 12 bundled lambdas", () => { | ||
const nextBinary = getNextBinary(); | ||
const fixtureDir = path.join(__dirname, "./fixture"); | ||
let outputDir: string; | ||
|
||
beforeAll(async () => { | ||
outputDir = path.join( | ||
os.tmpdir(), | ||
new Date().getUTCMilliseconds().toString(), | ||
"slsnext-test-build" | ||
); | ||
|
||
console.log("outputDir:", outputDir); | ||
|
||
const builder = new Builder(fixtureDir, outputDir, { | ||
cwd: fixtureDir, | ||
cmd: nextBinary, | ||
args: ["build"], | ||
bundledLambdas: true | ||
}); | ||
|
||
await builder.build(); | ||
}); | ||
|
||
afterAll(() => { | ||
return Promise.all( | ||
[".next"].map((file) => remove(path.join(fixtureDir, file))) | ||
); | ||
}); | ||
|
||
it("does not copy node_modules to default lambda artefact", async () => { | ||
const exists = await pathExists( | ||
path.join(outputDir, "default-lambda/node_modules") | ||
); | ||
expect(exists).toBe(false); | ||
}); | ||
|
||
it("copies bundle.js to default lambda artefact", async () => { | ||
const exists = await pathExists( | ||
path.join(outputDir, "default-lambda/bundle.js") | ||
); | ||
|
||
expect(exists).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.