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

Commit 1b33e72

Browse files
authored
feat(lambda-at-edge): add minifyHandlers input to minify handler code using Terser (#659)
1 parent 7d9dbe2 commit 1b33e72

File tree

8 files changed

+407
-195
lines changed

8 files changed

+407
-195
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ The fourth cache behaviour handles next API requests `api/*`.
422422
| publicDirectoryCache | `boolean\|object` | `true` | Customize the `public`/`static` folder asset caching policy. Assigning an object with `value` and/or `test` lets you customize the caching policy and the types of files being cached. Assigning false disables caching |
423423
| useServerlessTraceTarget | `boolean` | `false` | Use the experimental-serverless-trace target to build your next app. This is the same build target that Vercel Now uses. See this [RFC](https://github.com/vercel/next.js/pull/8246) for details. |
424424
| logLambdaExecutionTimes | `boolean` | `false` | Logs to CloudWatch the default handler performance metrics. |
425+
| minifyHandlers | `boolean` | `false` | Use minified handlers to reduce code size. |
425426

426427
Custom inputs can be configured like this:
427428

packages/libs/lambda-at-edge/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/path-to-regexp": "^1.7.0",
4444
"rollup": "^2.26.6",
4545
"rollup-plugin-node-externals": "^2.2.0",
46+
"rollup-plugin-terser": "^7.0.2",
4647
"rollup-plugin-typescript2": "^0.27.2",
4748
"ts-loader": "^7.0.5",
4849
"typescript": "^3.9.6"

packages/libs/lambda-at-edge/rollup.config.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import typescript from "rollup-plugin-typescript2";
33
import { nodeResolve } from "@rollup/plugin-node-resolve";
44
import externals from "rollup-plugin-node-externals";
55
import json from "@rollup/plugin-json";
6+
import { terser } from "rollup-plugin-terser";
67

78
const LOCAL_EXTERNALS = [
89
"./manifest.json",
@@ -11,10 +12,10 @@ const LOCAL_EXTERNALS = [
1112
];
1213
const NPM_EXTERNALS = ["aws-lambda", "aws-sdk/clients/s3"];
1314

14-
const generateConfig = (filename) => ({
15-
input: `./src/${filename}.ts`,
15+
const generateConfig = (input) => ({
16+
input: `./src/${input.filename}.ts`,
1617
output: {
17-
file: `./dist/${filename}.js`,
18+
file: `./dist/${input.filename}${input.minify ? ".min" : ""}.js`,
1819
format: "cjs"
1920
},
2021
plugins: [
@@ -26,10 +27,22 @@ const generateConfig = (filename) => ({
2627
nodeResolve(),
2728
typescript({
2829
tsconfig: "tsconfig.bundle.json"
29-
})
30+
}),
31+
input.minify
32+
? terser({
33+
compress: true,
34+
mangle: true,
35+
output: { comments: false } // Remove all comments, which is fine as the handler code is not distributed.
36+
})
37+
: undefined
3038
],
3139
external: [...NPM_EXTERNALS, ...LOCAL_EXTERNALS],
3240
inlineDynamicImports: true
3341
});
3442

35-
export default ["default-handler", "api-handler"].map(generateConfig);
43+
export default [
44+
{ filename: "default-handler", minify: false },
45+
{ filename: "default-handler", minify: true },
46+
{ filename: "api-handler", minify: false },
47+
{ filename: "api-handler", minify: true }
48+
].map(generateConfig);

packages/libs/lambda-at-edge/src/build.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type BuildOptions = {
2929
useServerlessTraceTarget?: boolean;
3030
logLambdaExecutionTimes?: boolean;
3131
domainRedirects?: { [key: string]: string };
32+
minifyHandlers?: boolean;
3233
};
3334

3435
const defaultBuildOptions = {
@@ -38,7 +39,8 @@ const defaultBuildOptions = {
3839
cmd: "./node_modules/.bin/next",
3940
useServerlessTraceTarget: false,
4041
logLambdaExecutionTimes: false,
41-
domainRedirects: {}
42+
domainRedirects: {},
43+
minifyHandlers: false
4244
};
4345

4446
class Builder {
@@ -179,6 +181,26 @@ class Builder {
179181
await fse.writeFile(destination, JSON.stringify(routesManifest));
180182
}
181183

184+
/**
185+
* Process and copy handler code. This allows minifying it before copying to Lambda package.
186+
* @param handlerType
187+
* @param destination
188+
* @param shouldMinify
189+
*/
190+
async processAndCopyHandler(
191+
handlerType: "api-handler" | "default-handler",
192+
destination: string,
193+
shouldMinify: boolean
194+
) {
195+
const source = require.resolve(
196+
`@sls-next/lambda-at-edge/dist/${handlerType}${
197+
shouldMinify ? ".min" : ""
198+
}.js`
199+
);
200+
201+
await fse.copy(source, destination);
202+
}
203+
182204
async buildDefaultLambda(
183205
buildManifest: OriginRequestDefaultHandlerManifest
184206
): Promise<void[]> {
@@ -223,9 +245,10 @@ class Builder {
223245

224246
return Promise.all([
225247
...copyTraces,
226-
fse.copy(
227-
require.resolve("@sls-next/lambda-at-edge/dist/default-handler.js"),
228-
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR, "index.js")
248+
this.processAndCopyHandler(
249+
"default-handler",
250+
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR, "index.js"),
251+
!!this.buildOptions.minifyHandlers
229252
),
230253
fse.writeJson(
231254
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR, "manifest.json"),
@@ -302,9 +325,10 @@ class Builder {
302325

303326
return Promise.all([
304327
...copyTraces,
305-
fse.copy(
306-
require.resolve("@sls-next/lambda-at-edge/dist/api-handler.js"),
307-
join(this.outputDir, API_LAMBDA_CODE_DIR, "index.js")
328+
this.processAndCopyHandler(
329+
"api-handler",
330+
join(this.outputDir, API_LAMBDA_CODE_DIR, "index.js"),
331+
!!this.buildOptions.minifyHandlers
308332
),
309333
fse.copy(
310334
join(this.serverlessDir, "pages/api"),
@@ -453,7 +477,7 @@ class Builder {
453477
}
454478
}
455479

456-
async build(debugMode: boolean): Promise<void> {
480+
async build(debugMode?: boolean): Promise<void> {
457481
const { cmd, args, cwd, env, useServerlessTraceTarget } = Object.assign(
458482
defaultBuildOptions,
459483
this.buildOptions

0 commit comments

Comments
 (0)