Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

fix(lambda-at-edge): copy next-i18next files to regeneration handler #1653

Merged
merged 3 commits into from
Sep 8, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ class Builder {
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR, "routes-manifest.json")
),
this.runThirdPartyIntegrations(
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR)
join(this.outputDir, DEFAULT_LAMBDA_CODE_DIR),
join(this.outputDir, REGENERATION_LAMBDA_CODE_DIR)
)
]);
}
Expand Down Expand Up @@ -824,11 +825,22 @@ class Builder {
/**
* Run additional integrations for third-party libraries such as next-i18next.
* These are usually needed to add additional files into the lambda, etc.
* @param outputLambdaDir
* @param defaultLambdaDir
* @param regenerationLambdaDir
*/
async runThirdPartyIntegrations(outputLambdaDir: string): Promise<void> {
async runThirdPartyIntegrations(
defaultLambdaDir: string,
regenerationLambdaDir: string
): Promise<void> {
await Promise.all([
new NextI18nextIntegration(this.nextConfigDir, outputLambdaDir).execute()
new NextI18nextIntegration(
this.nextConfigDir,
defaultLambdaDir
).execute(),
new NextI18nextIntegration(
this.nextConfigDir,
regenerationLambdaDir
).execute()
]);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { join } from "path";
import fse from "fs-extra";

/**
* This class allows one to integrate third party libraries by copying them to a specific Lambda directory.
* Extend from this, implement the execute() method, and keep it generic enough so it can be reused across platforms.
*/
export abstract class ThirdPartyIntegrationBase {
nextConfigDir: string;
outputLambdaDir: string;
outputHandlerDir: string;

constructor(nextConfigDir: string, outputLambdaDir: string) {
constructor(nextConfigDir: string, outputHandlerDir: string) {
this.nextConfigDir = nextConfigDir;
this.outputLambdaDir = outputLambdaDir;
this.outputHandlerDir = outputHandlerDir;
}

abstract execute(): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class NextI18nextIntegration extends ThirdPartyIntegrationBase {
async execute(): Promise<void> {
if (await this.isPackagePresent("next-i18next")) {
const localeSrc = join(this.nextConfigDir, "public", "locales");
const localeDest = join(this.outputLambdaDir, "public", "locales");
const localeDest = join(this.outputHandlerDir, "public", "locales");

if (await fse.pathExists(localeSrc)) {
await fse.copy(localeSrc, localeDest, { recursive: true });
Expand All @@ -20,11 +20,14 @@ export class NextI18nextIntegration extends ThirdPartyIntegrationBase {
"next-i18next.config.js"
);
const nextI18nextConfigDest = join(
this.outputLambdaDir,
this.outputHandlerDir,
"next-i18next.config.js"
);

if (await fse.pathExists(nextI18nextConfigSrc)) {
if (
(await fse.pathExists(nextI18nextConfigSrc)) &&
(await fse.pathExists(this.outputHandlerDir))
) {
await fse.copy(nextI18nextConfigSrc, nextI18nextConfigDest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import execa from "execa";
import Builder, {
DEFAULT_LAMBDA_CODE_DIR,
API_LAMBDA_CODE_DIR,
IMAGE_LAMBDA_CODE_DIR
IMAGE_LAMBDA_CODE_DIR,
REGENERATION_LAMBDA_CODE_DIR
} from "../../src/build";
import { cleanupDir } from "../test-utils";
import {
Expand Down Expand Up @@ -79,5 +80,30 @@ describe("Builder Tests (with third party integrations)", () => {
expect(localesFiles).toEqual(expect.arrayContaining(["de", "en"]));
});
});

describe("Regeneration Handler Third Party Files", () => {
it("next-i18next files are copied", async () => {
expect(
await fse.pathExists(
join(
outputDir,
`${REGENERATION_LAMBDA_CODE_DIR}`,
"next-i18next.config.js"
)
)
).toBe(true);

const localesFiles = await fse.readdir(
join(
outputDir,
`${REGENERATION_LAMBDA_CODE_DIR}`,
"public",
"locales"
)
);

expect(localesFiles).toEqual(expect.arrayContaining(["de", "en"]));
});
});
});
});
10 changes: 2 additions & 8 deletions packages/libs/lambda-at-edge/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1389,14 +1389,8 @@
picomatch "^2.2.2"

"@sls-next/core@link:../core":
version "3.4.0-alpha.0"
dependencies:
"@hapi/accept" "^5.0.1"
cookie "^0.4.1"
jsonwebtoken "^8.5.1"
next "^11.1.2"
path-to-regexp "^6.1.0"
regex-parser "^2.2.10"
version "0.0.0"
uid ""

"@tsconfig/node10@^1.0.7":
version "1.0.8"
Expand Down