Skip to content

Commit

Permalink
feat(lambda-at-edge): add baseDir and resolve build options to server…
Browse files Browse the repository at this point in the history
…less-trace (#779)

* Add baseDir and resolve build options to serverless-trace

This PR makes two changes to the `lambda-at-edge` builder to better
support Yarn v2 projects when using the `serverless-trace` target.

1. Adds a `baseDir` build option to specify the base directory to search
   for `node_modules`. Currently, the builder sets this to `process.cwd()`,
   but Yarn v2 often hoists dependencies across multiple workspaces so
   that they can be shared. Without this change, all dependencies from
   ancestor directories are omitted, leading to import errors at runtime.
2. Adds a `resolve` build option which allows projects to specify their
   own custom resolvers, such as when supporting Yarn v2's PnP mode.
   This leverages the resolver hook added in vercel/nft#153, which also
   required upgrading from `@zeit/node-file-trace` to the latest version
   of `@vercel/nft`. Note that even after this change, Yarn v2's PnP mode
   is still incompatible with the builder since the builder ends up
   collapsing all of the PnP dependencies into a single `node_modules`
   directory. This causes multiple versions of a package to clobber one
   another.

Co-authored-by: Daniel <danielconde9@gmail.com>
  • Loading branch information
ramosbugs and danielcondemarin authored Nov 11, 2020
1 parent 3ff7563 commit 8f978bb
Show file tree
Hide file tree
Showing 8 changed files with 527 additions and 486 deletions.
2 changes: 1 addition & 1 deletion packages/libs/lambda-at-edge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "1.0.0-rc.3",
"@zeit/node-file-trace": "^0.6.5",
"@vercel/nft": "^0.9.3",
"cookie": "^0.4.1",
"execa": "^4.0.2",
"fs-extra": "^9.0.1",
Expand Down
33 changes: 25 additions & 8 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import nodeFileTrace, { NodeFileTraceReasons } from "@zeit/node-file-trace";
import { nodeFileTrace, NodeFileTraceReasons } from "@vercel/nft";
import execa from "execa";
import fse from "fs-extra";
import { join } from "path";
Expand All @@ -21,6 +21,7 @@ import readDirectoryFiles from "./lib/readDirectoryFiles";
import filterOutDirectories from "./lib/filterOutDirectories";
import { PrerenderManifest } from "next/dist/build";
import { Item } from "klaw";
import { Job } from "@vercel/nft/out/node-file-trace";

export const DEFAULT_LAMBDA_CODE_DIR = "default-lambda";
export const API_LAMBDA_CODE_DIR = "api-lambda";
Expand All @@ -38,6 +39,13 @@ type BuildOptions = {
enableHTTPCompression?: boolean;
handler?: string;
authentication?: { username: string; password: string } | undefined;
resolve?: (
id: string,
parent: string,
job: Job,
cjsResolve: boolean
) => string | string[];
baseDir?: string;
};

const defaultBuildOptions = {
Expand All @@ -50,7 +58,9 @@ const defaultBuildOptions = {
domainRedirects: {},
minifyHandlers: false,
enableHTTPCompression: true,
authentication: undefined
authentication: undefined,
resolve: undefined,
baseDir: process.cwd()
};

class Builder {
Expand Down Expand Up @@ -127,7 +137,8 @@ class Builder {
copyLambdaHandlerDependencies(
fileList: string[],
reasons: NodeFileTraceReasons,
handlerDirectory: string
handlerDirectory: string,
base: string
): Promise<void>[] {
return fileList
.filter((file) => {
Expand All @@ -146,7 +157,7 @@ class Builder {
);
})
.map((filePath: string) => {
const resolvedFilePath = path.resolve(filePath);
const resolvedFilePath = path.resolve(join(base, filePath));
const dst = normalizeNodeModules(
path.relative(this.serverlessDir, resolvedFilePath)
);
Expand Down Expand Up @@ -248,14 +259,17 @@ class Builder {
path.join(this.serverlessDir, pageFile)
);

const base = this.buildOptions.baseDir || process.cwd();
const { fileList, reasons } = await nodeFileTrace(ssrPages, {
base: process.cwd()
base,
resolve: this.buildOptions.resolve
});

copyTraces = this.copyLambdaHandlerDependencies(
fileList,
reasons,
DEFAULT_LAMBDA_CODE_DIR
DEFAULT_LAMBDA_CODE_DIR,
base
);
}

Expand Down Expand Up @@ -347,14 +361,17 @@ class Builder {
path.join(this.serverlessDir, pageFile)
);

const base = this.buildOptions.baseDir || process.cwd();
const { fileList, reasons } = await nodeFileTrace(apiPages, {
base: process.cwd()
base,
resolve: this.buildOptions.resolve
});

copyTraces = this.copyLambdaHandlerDependencies(
fileList,
reasons,
API_LAMBDA_CODE_DIR
API_LAMBDA_CODE_DIR,
base
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "serverless-trace-with-dynamic-import-typescript-fixture",
"version": "1.0.0",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "serverless-trace-with-dynamic-import-fixture",
"version": "1.0.0",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "serverless-trace-fixture",
"version": "1.0.0",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("Serverless Trace", () => {
let fseRemoveSpy: jest.SpyInstance;

beforeEach(async () => {
outputDir = os.tmpdir();
outputDir = path.join(os.tmpdir(), `${Date.now()}`);

fseRemoveSpy = jest.spyOn(fse, "remove").mockImplementation(() => {
return;
Expand Down
12 changes: 6 additions & 6 deletions packages/libs/lambda-at-edge/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,10 @@
dependencies:
"@types/node" "*"

"@zeit/node-file-trace@^0.6.5":
version "0.6.5"
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==
"@vercel/nft@^0.9.3":
version "0.9.3"
resolved "https://registry.yarnpkg.com/@vercel/nft/-/nft-0.9.3.tgz#d27d6173a9036d37a497511d84273fcb03d6c483"
integrity sha512-hbRTSKzHt6fTzTKDjZR5fXNMIOAS3/Ffhw96RM4M5+ZnDkxb+OFr6lNIhFibLxxvp27aCh/r/RqPZqj2EBgrlg==
dependencies:
acorn "^7.1.1"
acorn-class-fields "^0.3.2"
Expand All @@ -779,7 +779,7 @@
acorn-numeric-separator "^0.3.0"
acorn-static-class-features "^0.2.1"
bindings "^1.4.0"
estree-walker "^0.6.0"
estree-walker "^0.6.1"
glob "^7.1.3"
graceful-fs "^4.1.15"
micromatch "^4.0.2"
Expand Down Expand Up @@ -1052,7 +1052,7 @@ escape-string-regexp@^1.0.5:
resolved "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=

estree-walker@^0.6.0, estree-walker@^0.6.1:
estree-walker@^0.6.1:
version "0.6.1"
resolved "https://registry.npmjs.org/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==
Expand Down
Loading

0 comments on commit 8f978bb

Please sign in to comment.