Skip to content

Commit

Permalink
- Add API Lambda to rollup
Browse files Browse the repository at this point in the history
- Fixed some erroneous logic in the router, since it's now being used in the response handler
- Mocked console errors in tests
  • Loading branch information
thchia committed Aug 31, 2020
1 parent 791276c commit fec9f9e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/libs/lambda-at-edge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
30 changes: 18 additions & 12 deletions packages/libs/lambda-at-edge/rollup.config.js
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 0 additions & 8 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 12 additions & 7 deletions packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
11 changes: 1 addition & 10 deletions packages/libs/lambda-at-edge/tests/build/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -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"
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 5 additions & 1 deletion packages/libs/lambda-at-edge/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
6 changes: 3 additions & 3 deletions packages/libs/lambda-at-edge/tsconfig.bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
46 changes: 46 additions & 0 deletions packages/libs/lambda-at-edge/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fec9f9e

Please sign in to comment.