This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda-at-edge): detect and copy next-i18next files (#1472)
- Loading branch information
Showing
40 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Third party integrations | ||
|
||
This will support common third-party integrations such as next-i18next. The integrations here are meant to auto-detect what your project is using and copy the appropriate files to the build artifacts. |
25 changes: 25 additions & 0 deletions
25
packages/libs/lambda-at-edge/src/build/third-party/integration-base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { join } from "path"; | ||
import fse from "fs-extra"; | ||
|
||
export abstract class ThirdPartyIntegrationBase { | ||
nextConfigDir: string; | ||
outputLambdaDir: string; | ||
|
||
constructor(nextConfigDir: string, outputLambdaDir: string) { | ||
this.nextConfigDir = nextConfigDir; | ||
this.outputLambdaDir = outputLambdaDir; | ||
} | ||
|
||
abstract execute(): void; | ||
|
||
async isPackagePresent(name: string): Promise<boolean> { | ||
const packageJsonPath = join(this.nextConfigDir, "package.json"); | ||
|
||
if (await fse.pathExists(packageJsonPath)) { | ||
const packageJson = await fse.readJSON(packageJsonPath); | ||
return !!packageJson.dependencies[name]; | ||
} | ||
|
||
return false; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/libs/lambda-at-edge/src/build/third-party/next-i18next.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import fse from "fs-extra"; | ||
import { join } from "path"; | ||
import { ThirdPartyIntegrationBase } from "./integration-base"; | ||
|
||
export class NextI18nextIntegration extends ThirdPartyIntegrationBase { | ||
/** | ||
* This will copy all next-i18next files as needed to a lambda directory. | ||
*/ | ||
async execute(): Promise<void> { | ||
if (await this.isPackagePresent("next-i18next")) { | ||
const localeSrc = join(this.nextConfigDir, "public", "locales"); | ||
const localeDest = join(this.outputLambdaDir, "public", "locales"); | ||
|
||
if (await fse.pathExists(localeSrc)) { | ||
await fse.copy(localeSrc, localeDest, { recursive: true }); | ||
} | ||
|
||
const nextI18nextConfigSrc = join( | ||
this.nextConfigDir, | ||
"next-i18next.config.js" | ||
); | ||
const nextI18nextConfigDest = join( | ||
this.outputLambdaDir, | ||
"next-i18next.config.js" | ||
); | ||
|
||
if (await fse.pathExists(nextI18nextConfigSrc)) { | ||
await fse.copy(nextI18nextConfigSrc, nextI18nextConfigDest); | ||
} | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/libs/lambda-at-edge/tests/build/build-with-third-party-integrations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { join } from "path"; | ||
import fse from "fs-extra"; | ||
import execa from "execa"; | ||
import Builder, { | ||
DEFAULT_LAMBDA_CODE_DIR, | ||
API_LAMBDA_CODE_DIR, | ||
IMAGE_LAMBDA_CODE_DIR | ||
} from "../../src/build"; | ||
import { cleanupDir } from "../test-utils"; | ||
import { | ||
OriginRequestDefaultHandlerManifest, | ||
OriginRequestApiHandlerManifest, | ||
OriginRequestImageHandlerManifest | ||
} from "../../src/types"; | ||
|
||
jest.mock("execa"); | ||
|
||
describe("Builder Tests (with third party integrations)", () => { | ||
let fseRemoveSpy: jest.SpyInstance; | ||
let fseEmptyDirSpy: jest.SpyInstance; | ||
let defaultBuildManifest: OriginRequestDefaultHandlerManifest; | ||
let apiBuildManifest: OriginRequestApiHandlerManifest; | ||
let imageBuildManifest: OriginRequestImageHandlerManifest; | ||
|
||
const fixturePath = join( | ||
__dirname, | ||
"./simple-app-fixture-third-party-integrations" | ||
); | ||
const outputDir = join(fixturePath, ".test_sls_next_output"); | ||
|
||
describe("Regular build", () => { | ||
beforeEach(async () => { | ||
const mockExeca = execa as jest.Mock; | ||
mockExeca.mockResolvedValueOnce(); | ||
|
||
fseRemoveSpy = jest.spyOn(fse, "remove").mockImplementation(() => { | ||
return; | ||
}); | ||
fseEmptyDirSpy = jest.spyOn(fse, "emptyDir"); | ||
|
||
const builder = new Builder(fixturePath, outputDir, {}); | ||
await builder.build(); | ||
|
||
defaultBuildManifest = await fse.readJSON( | ||
join(outputDir, `${DEFAULT_LAMBDA_CODE_DIR}/manifest.json`) | ||
); | ||
|
||
apiBuildManifest = await fse.readJSON( | ||
join(outputDir, `${API_LAMBDA_CODE_DIR}/manifest.json`) | ||
); | ||
|
||
imageBuildManifest = await fse.readJSON( | ||
join(outputDir, `${IMAGE_LAMBDA_CODE_DIR}/manifest.json`) | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fseEmptyDirSpy.mockRestore(); | ||
fseRemoveSpy.mockRestore(); | ||
//return cleanupDir(outputDir); | ||
}); | ||
|
||
describe("Default Handler Third Party Files", () => { | ||
it("next-i18next files are copied", async () => { | ||
expect( | ||
await fse.pathExists( | ||
join( | ||
outputDir, | ||
`${DEFAULT_LAMBDA_CODE_DIR}`, | ||
"next-i18next.config.js" | ||
) | ||
) | ||
).toBe(true); | ||
|
||
const localesFiles = await fse.readdir( | ||
join(outputDir, `${DEFAULT_LAMBDA_CODE_DIR}`, "public", "locales") | ||
); | ||
|
||
expect(localesFiles).toEqual(expect.arrayContaining(["de", "en"])); | ||
}); | ||
}); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
...ibs/lambda-at-edge/tests/build/simple-app-fixture-third-party-integrations/.next/BUILD_ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test-build-id |
1 change: 1 addition & 0 deletions
1
...-edge/tests/build/simple-app-fixture-third-party-integrations/.next/cache/placeholder.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Having a cache/ folder allows to test the cleanup of .next/ except for cache/ for faster builds |
28 changes: 28 additions & 0 deletions
28
...t-edge/tests/build/simple-app-fixture-third-party-integrations/.next/images-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"version": 1, | ||
"images": { | ||
"deviceSizes": [640, 750, 828, 1080, 1200, 1920, 2048, 3840], | ||
"imageSizes": [16, 32, 48, 64, 96, 128, 256, 384], | ||
"domains": [], | ||
"path": "/_next/image", | ||
"loader": "default", | ||
"sizes": [ | ||
640, | ||
750, | ||
828, | ||
1080, | ||
1200, | ||
1920, | ||
2048, | ||
3840, | ||
16, | ||
32, | ||
48, | ||
64, | ||
96, | ||
128, | ||
256, | ||
384 | ||
] | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...dge/tests/build/simple-app-fixture-third-party-integrations/.next/prerender-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"version": 2, | ||
"routes": { | ||
"/": { | ||
"initialRevalidateSeconds": false, | ||
"srcRoute": null, | ||
"dataRoute": "/_next/data/zsWqBqLjpgRmswfQomanp/index.json" | ||
}, | ||
"/contact": { | ||
"initialRevalidateSeconds": false, | ||
"srcRoute": null, | ||
"dataRoute": "/_next/data/zsWqBqLjpgRmswfQomanp/contact.json" | ||
} | ||
}, | ||
"dynamicRoutes": {} | ||
} |
1 change: 1 addition & 0 deletions
1
...t-edge/tests/build/simple-app-fixture-third-party-integrations/.next/routes-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":1,"pages404":true,"basePath":"","redirects":[],"rewrites":[],"headers":[],"dynamicRoutes":[]} |
Empty file.
19 changes: 19 additions & 0 deletions
19
...ts/build/simple-app-fixture-third-party-integrations/.next/serverless/pages-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"/[root]": "pages/[root].js", | ||
"/customers/[customer]": "pages/customers/[customer].js", | ||
"/customers/[customer]/[post]": "pages/customers/[customer]/[post].js", | ||
"/customers/new": "pages/customers/new.js", | ||
"/customers/[customer]/profile": "pages/customers/[customer]/profile.js", | ||
"/customers/[...catchAll]": "pages/customers/[...catchAll].js", | ||
"/products/[[...optionalCatchAll]]": "pages/products/[[...optionalCatchAll]].js", | ||
"/api/customers": "pages/api/customers.js", | ||
"/api/customers/[id]": "pages/api/customers/[id].js", | ||
"/api/customers/new": "pages/api/customers/new.js", | ||
"/terms": "pages/terms.html", | ||
"/about": "pages/about.html", | ||
"/blog/[post]": "pages/blog/[post].html", | ||
"/": "pages/index.js", | ||
"/_app": "pages/_app.js", | ||
"/_document": "pages/_document.js", | ||
"/404": "pages/404.html" | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions
9
...da-at-edge/tests/build/simple-app-fixture-third-party-integrations/next-i18next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
i18n: { | ||
defaultLocale: "en", | ||
locales: ["en", "de"], | ||
localePath: path.resolve("./public/locales") | ||
} | ||
}; |
1 change: 1 addition & 0 deletions
1
...ibs/lambda-at-edge/tests/build/simple-app-fixture-third-party-integrations/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { target: "serverless" }; |
5 changes: 5 additions & 0 deletions
5
.../libs/lambda-at-edge/tests/build/simple-app-fixture-third-party-integrations/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"next-i18next": "^8.5.5" | ||
} | ||
} |
Empty file.
6 changes: 6 additions & 0 deletions
6
...dge/tests/build/simple-app-fixture-third-party-integrations/public/locales/de/common.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"button": { | ||
"login": "Login", | ||
"register": "Register" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...dge/tests/build/simple-app-fixture-third-party-integrations/public/locales/en/common.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"button": { | ||
"login": "Login", | ||
"register": "Register" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions
1
...s/libs/lambda-at-edge/tests/build/simple-app-fixture-third-party-integrations/testFile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Test handler file to check if copied |