From fec9f9e5a5a1fc9230118c59924c36ea6f77161d Mon Sep 17 00:00:00 2001 From: Thomas Chia Date: Mon, 31 Aug 2020 18:44:02 +0800 Subject: [PATCH] - Add API Lambda to rollup - Fixed some erroneous logic in the router, since it's now being used in the response handler - Mocked console errors in tests --- packages/libs/lambda-at-edge/package.json | 1 + packages/libs/lambda-at-edge/rollup.config.js | 30 +++++++----- packages/libs/lambda-at-edge/src/build.ts | 8 ---- .../lambda-at-edge/src/default-handler.ts | 19 +++++--- .../lambda-at-edge/tests/build/build.test.ts | 11 +---- .../default-handler-with-basepath.test.ts | 9 ++++ .../default-handler/default-handler.test.ts | 9 ++++ .../libs/lambda-at-edge/tsconfig.build.json | 6 ++- .../libs/lambda-at-edge/tsconfig.bundle.json | 6 +-- packages/libs/lambda-at-edge/yarn.lock | 46 +++++++++++++++++++ 10 files changed, 104 insertions(+), 41 deletions(-) diff --git a/packages/libs/lambda-at-edge/package.json b/packages/libs/lambda-at-edge/package.json index 618ea3762c..64ea4fcab2 100644 --- a/packages/libs/lambda-at-edge/package.json +++ b/packages/libs/lambda-at-edge/package.json @@ -43,6 +43,7 @@ "@types/path-to-regexp": "^1.7.0", "path-to-regexp": "^6.1.0", "rollup": "^2.26.6", + "rollup-plugin-node-externals": "^2.2.0", "ts-loader": "^7.0.5", "typescript": "^3.9.6" }, diff --git a/packages/libs/lambda-at-edge/rollup.config.js b/packages/libs/lambda-at-edge/rollup.config.js index a9f963c4fa..a4a7f2fa83 100644 --- a/packages/libs/lambda-at-edge/rollup.config.js +++ b/packages/libs/lambda-at-edge/rollup.config.js @@ -1,26 +1,32 @@ import commonjs from "@rollup/plugin-commonjs"; import typescript from "@rollup/plugin-typescript"; import { nodeResolve } from "@rollup/plugin-node-resolve"; +import externals from "rollup-plugin-node-externals"; -export default { - input: "./src/default-handler.ts", +const LOCAL_EXTERNALS = [ + "./manifest.json", + "./routes-manifest.json", + "./prerender-manifest.json" +]; +const NPM_EXTERNALS = ["aws-lambda", "aws-sdk/clients/s3"]; + +const generateConfig = (filename) => ({ + input: `./src/${filename}.ts`, output: { - file: "dist/default-handler.js", + file: `./dist/${filename}.js`, format: "cjs" }, plugins: [ commonjs(), + externals({ + exclude: "@sls-next/next-aws-cloudfront" + }), nodeResolve(), typescript({ tsconfig: "tsconfig.bundle.json" }) ], - external: [ - "util", - "aws-lambda", - "./manifest.json", - "aws-sdk/clients/s3", - "./routes-manifest.json", - "./prerender-manifest.json" - ] -}; + external: [...NPM_EXTERNALS, ...LOCAL_EXTERNALS] +}); + +export default ["default-handler", "api-handler"].map(generateConfig); diff --git a/packages/libs/lambda-at-edge/src/build.ts b/packages/libs/lambda-at-edge/src/build.ts index a1680a50b6..b310d94b24 100644 --- a/packages/libs/lambda-at-edge/src/build.ts +++ b/packages/libs/lambda-at-edge/src/build.ts @@ -237,14 +237,6 @@ class Builder { require.resolve("@sls-next/lambda-at-edge/dist/api-handler.js"), join(this.outputDir, API_LAMBDA_CODE_DIR, "index.js") ), - fse.copy( - require.resolve("@sls-next/next-aws-cloudfront"), - join( - this.outputDir, - API_LAMBDA_CODE_DIR, - "node_modules/@sls-next/next-aws-cloudfront/index.js" - ) - ), fse.copy( join(this.serverlessDir, "pages/api"), join(this.outputDir, API_LAMBDA_CODE_DIR, "pages/api") diff --git a/packages/libs/lambda-at-edge/src/default-handler.ts b/packages/libs/lambda-at-edge/src/default-handler.ts index 28041f4ce3..4e6f120bfa 100644 --- a/packages/libs/lambda-at-edge/src/default-handler.ts +++ b/packages/libs/lambda-at-edge/src/default-handler.ts @@ -108,10 +108,12 @@ const router = ( manifest: OriginRequestDefaultHandlerManifest ): ((uri: string) => string) => { const { - pages: { ssr, html } + pages: { ssr, html }, + publicFiles } = manifest; const allDynamicRoutes = { ...ssr.dynamic, ...html.dynamic }; + const allNonDynamicRoutes = { ...ssr.nonDynamic, ...html.nonDynamic }; return (uri: string): string => { let normalisedUri = uri; @@ -120,15 +122,18 @@ const router = ( normalisedUri = uri .replace(`/_next/data/${manifest.buildId}`, "") .replace(".json", ""); + } + // Normalise to "/" for index + normalisedUri = ["/index", ""].includes(normalisedUri) + ? "/" + : normalisedUri; - // Normalise to "/" for index data request - normalisedUri = ["/index", ""].includes(normalisedUri) - ? "/" - : normalisedUri; + if (allNonDynamicRoutes[normalisedUri]) { + return allNonDynamicRoutes[normalisedUri]; } - if (ssr.nonDynamic[normalisedUri]) { - return ssr.nonDynamic[normalisedUri]; + if (publicFiles[normalisedUri]) { + return publicFiles[normalisedUri]; } for (const route in allDynamicRoutes) { diff --git a/packages/libs/lambda-at-edge/tests/build/build.test.ts b/packages/libs/lambda-at-edge/tests/build/build.test.ts index b5b88f5334..5fb2b0763a 100644 --- a/packages/libs/lambda-at-edge/tests/build/build.test.ts +++ b/packages/libs/lambda-at-edge/tests/build/build.test.ts @@ -194,7 +194,7 @@ describe("Builder Tests", () => { describe("API Handler Artefact Files", () => { it("copies build files", async () => { - expect.assertions(3); + expect.assertions(2); const files = await fse.readdir( join(outputDir, `${API_LAMBDA_CODE_DIR}`) @@ -203,18 +203,9 @@ describe("Builder Tests", () => { join(outputDir, `${API_LAMBDA_CODE_DIR}/pages`) ); - const compatLayerIncluded = await fse.pathExists( - join( - outputDir, - `${API_LAMBDA_CODE_DIR}/node_modules/@sls-next/next-aws-cloudfront/index.js` - ) - ); - - expect(compatLayerIncluded).toEqual(true); expect(files).toEqual([ "index.js", "manifest.json", - "node_modules", "pages", "routes-manifest.json" ]); diff --git a/packages/libs/lambda-at-edge/tests/default-handler/default-handler-with-basepath.test.ts b/packages/libs/lambda-at-edge/tests/default-handler/default-handler-with-basepath.test.ts index 047d609918..67b65a5bd3 100644 --- a/packages/libs/lambda-at-edge/tests/default-handler/default-handler-with-basepath.test.ts +++ b/packages/libs/lambda-at-edge/tests/default-handler/default-handler-with-basepath.test.ts @@ -32,6 +32,15 @@ const mockPageRequire = (mockPagePath: string): void => { }; describe("Lambda@Edge", () => { + let consoleWarnSpy: jest.SpyInstance; + + beforeEach(() => { + consoleWarnSpy = jest.spyOn(console, "error").mockReturnValue(); + }); + + afterEach(() => { + consoleWarnSpy.mockRestore(); + }); describe.each` trailingSlash ${false} diff --git a/packages/libs/lambda-at-edge/tests/default-handler/default-handler.test.ts b/packages/libs/lambda-at-edge/tests/default-handler/default-handler.test.ts index ef012fe9b8..3a527fb16f 100644 --- a/packages/libs/lambda-at-edge/tests/default-handler/default-handler.test.ts +++ b/packages/libs/lambda-at-edge/tests/default-handler/default-handler.test.ts @@ -36,6 +36,15 @@ const mockPageRequire = (mockPagePath: string): void => { }; describe("Lambda@Edge", () => { + let consoleWarnSpy: jest.SpyInstance; + + beforeEach(() => { + consoleWarnSpy = jest.spyOn(console, "error").mockReturnValue(); + }); + + afterEach(() => { + consoleWarnSpy.mockRestore(); + }); describe.each` trailingSlash ${false} diff --git a/packages/libs/lambda-at-edge/tsconfig.build.json b/packages/libs/lambda-at-edge/tsconfig.build.json index 408578d054..b1de996030 100644 --- a/packages/libs/lambda-at-edge/tsconfig.build.json +++ b/packages/libs/lambda-at-edge/tsconfig.build.json @@ -5,5 +5,9 @@ "removeComments": true }, "include": ["./src/"], - "exclude": ["node_modules", "./src/default-handler.ts"] + "exclude": [ + "node_modules", + "./src/api-handler.ts", + "./src/default-handler.ts" + ] } diff --git a/packages/libs/lambda-at-edge/tsconfig.bundle.json b/packages/libs/lambda-at-edge/tsconfig.bundle.json index eac46a2a6e..efe611bfeb 100644 --- a/packages/libs/lambda-at-edge/tsconfig.bundle.json +++ b/packages/libs/lambda-at-edge/tsconfig.bundle.json @@ -6,9 +6,9 @@ "target": "es2019", "moduleResolution": "node", "noImplicitAny": true, - "sourceMap": true, + "sourceMap": false, "strict": true, "allowJs": true }, - "include": ["./src/default-handler.ts"] -} + "include": ["./src/default-handler.ts", "./src/api-handler.ts"] +} \ No newline at end of file diff --git a/packages/libs/lambda-at-edge/yarn.lock b/packages/libs/lambda-at-edge/yarn.lock index ee732acfae..44eeb8e2d3 100644 --- a/packages/libs/lambda-at-edge/yarn.lock +++ b/packages/libs/lambda-at-edge/yarn.lock @@ -421,6 +421,14 @@ fill-range@^7.0.1: dependencies: to-regex-range "^5.0.1" +find-up@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19" + integrity sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw== + dependencies: + locate-path "^5.0.0" + path-exists "^4.0.0" + fs-extra@^9.0.1: version "9.0.1" resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc" @@ -635,6 +643,13 @@ loader-utils@^1.0.2: emojis-list "^3.0.0" json5 "^1.0.1" +locate-path@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0" + integrity sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g== + dependencies: + p-locate "^4.1.0" + lodash.includes@^4.3.0: version "4.3.0" resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f" @@ -860,6 +875,30 @@ osenv@^0.1.4: os-homedir "^1.0.0" os-tmpdir "^1.0.0" +p-limit@^2.2.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1" + integrity sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w== + dependencies: + p-try "^2.0.0" + +p-locate@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07" + integrity sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A== + dependencies: + p-limit "^2.2.0" + +p-try@^2.0.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6" + integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ== + +path-exists@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3" + integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w== + path-is-absolute@^1.0.0: version "1.0.1" resolved "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" @@ -945,6 +984,13 @@ rimraf@^2.6.1: dependencies: glob "^7.1.3" +rollup-plugin-node-externals@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-node-externals/-/rollup-plugin-node-externals-2.2.0.tgz#23946e8c0fdd0e321cc3f225d4cebc3d819d17c4" + integrity sha512-WM7TtQ76GdsLceEGmZzQzn1afj8JgOQT5VLs1Y9RMqowM/8eK2mBj/Lv7hoE833U75QsUZIRirYUtFatu51RJA== + dependencies: + find-up "^4.1.0" + rollup-pluginutils@^2.8.2: version "2.8.2" resolved "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"