Skip to content

Commit

Permalink
fix(lambda-at-edge): for serverless-trace target, exclude files that …
Browse files Browse the repository at this point in the history
…are not in node-modules folder (#758)
  • Loading branch information
dphang committed Nov 2, 2020
1 parent e252c2e commit 2d72f4f
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class Builder {
.filter((file) => {
// exclude "initial" files from lambda artefact. These are just the pages themselves
// which are copied over separately

// For TypeScript apps, somehow nodeFileTrace will generate filelist with TS or TSX files, we need to exclude these files to be copied
// as it ends up copying from same source to destination.
if (file.endsWith(".ts") || file.endsWith(".tsx")) {

This comment has been minimized.

Copy link
@dphang

dphang Nov 2, 2020

Author Collaborator

Woops, commit message is wrong, not sure how that happened. Should be "exclude files that are TypeScript sources"

return false;
}

return (
(!reasons[file] || reasons[file].type !== "initial") &&
file !== "package.json"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.next
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from "react";

export default () => <span>Hello World</span>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default (req: any, res: any) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ name: "John Doe" }));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dynamic from "next/dynamic";
import * as React from "react";

const DynamicComponent = dynamic(() => import("../components/hello"));

const Page = () => <DynamicComponent />;

Page.getInitialProps = () => {
// just forcing this page to be server side rendered
return {
foo: "bar"
};
};

export default Page;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { remove, readdir, pathExists } from "fs-extra";
import path from "path";
import os from "os";
import Builder from "../../../src/build";
import { getNextBinary } from "../../test-utils";
import klaw from "klaw";

jest.unmock("execa");

describe("Serverless Trace With Dynamic Import - TypeScript", () => {
const nextBinary = getNextBinary();
const fixtureDir = path.join(__dirname, "./fixture");
let outputDir: string;

beforeAll(async () => {
outputDir = path.join(os.tmpdir(), "slsnext-test-build");
const builder = new Builder(fixtureDir, outputDir, {
cwd: fixtureDir,
cmd: nextBinary,
args: ["build"],
useServerlessTraceTarget: true
});

await builder.build();
});

afterAll(() => {
return Promise.all(
[".next"].map((file) => remove(path.join(fixtureDir, file)))
);
});

it("copies node_modules to default lambda artefact", async () => {
const nodeModules = await readdir(
path.join(outputDir, "default-lambda/node_modules")
);
expect(nodeModules.length).toBeGreaterThan(5); // 5 is just an arbitrary number to ensure dependencies are being copied
});

it("copies node_modules to api lambda artefact", async () => {
const nodeModules = await readdir(
path.join(outputDir, "api-lambda/node_modules")
);
expect(nodeModules).toEqual(["next"]);
});

it("copies dynamic chunk to default lambda artefact", async () => {
const chunkFileName = (
await readdir(path.join(fixtureDir, ".next/serverless"))
).find((file) => {
return /^[\d]+\.+[\w,\s-]+\.(js)$/.test(file);
});

expect(chunkFileName).toBeDefined();

const chunkExistsInOutputBuild = await pathExists(
path.join(outputDir, "default-lambda", chunkFileName as string)
);

expect(chunkExistsInOutputBuild).toBe(true);
});

it("does not copy TypeScript sources", async () => {
const defaultLambdaFiles: string[] = [];
klaw(path.join(outputDir, "default-lambda")).on("data", (item) =>
defaultLambdaFiles.push(item.path)
);

const typeScriptFileName = defaultLambdaFiles.find((file) => {
return /^[\d]+\.+[\w,\s-]+\.(ts|tsx)$/.test(file);
});

expect(typeScriptFileName).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion packages/libs/lambda-at-edge/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@

"@zeit/node-file-trace@^0.6.5":
version "0.6.5"
resolved "https://registry.npmjs.org/@zeit/node-file-trace/-/node-file-trace-0.6.5.tgz#ffd443e4648eb88591c53b1a871a47bff651b62e"
resolved "https://registry.yarnpkg.com/@zeit/node-file-trace/-/node-file-trace-0.6.5.tgz#ffd443e4648eb88591c53b1a871a47bff651b62e"
integrity sha512-PbxtiZBU+axKtR9dU2/iQgK9+aP/ip94SqI/FCMWppmFPGlxGKHf8UnJZskFuqLZeWWzL+L+8SeipsNHATO9nw==
dependencies:
acorn "^7.1.1"
Expand Down

0 comments on commit 2d72f4f

Please sign in to comment.