Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda-at-edge): for serverless-trace target, exclude files that … #758

Merged
merged 4 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we could prevent FS from copying if src == dest but I think this should work for now.

if (file.endsWith(".ts") || file.endsWith(".tsx")) {
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