Skip to content

Commit

Permalink
Check if image component is used (#1065)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Phang <[email protected]>
  • Loading branch information
jvarho and dphang authored May 11, 2021
1 parent 0a388c6 commit 7372bf2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
10 changes: 9 additions & 1 deletion packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,15 @@ class Builder {
const isDefaultLoader = !imageLoader || imageLoader === "default";
const hasImageOptimizer = hasImagesManifest && isDefaultLoader;

if (hasImageOptimizer) {
// ...nor if the image component is not used
const exportMarker = fse.existsSync(
join(this.dotNextDir, "export-marker.json")
)
? await fse.readJSON(path.join(this.dotNextDir, "export-marker.json"))
: {};
const isNextImageImported = exportMarker.isNextImageImported !== false;

if (hasImageOptimizer && isNextImageImported) {
await this.buildImageLambda(imageBuildManifest);
}

Expand Down
18 changes: 17 additions & 1 deletion packages/libs/lambda-at-edge/tests/build/build-dynamic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { join } from "path";
import fse from "fs-extra";
import execa from "execa";
import Builder, { ASSETS_DIR } from "../../src/build";
import { DEFAULT_LAMBDA_CODE_DIR, API_LAMBDA_CODE_DIR } from "../../src/build";
import {
DEFAULT_LAMBDA_CODE_DIR,
API_LAMBDA_CODE_DIR,
IMAGE_LAMBDA_CODE_DIR
} from "../../src/build";
import { cleanupDir, removeNewLineChars } from "../test-utils";
import { OriginRequestDefaultHandlerManifest } from "../../src/types";

Expand Down Expand Up @@ -377,6 +381,18 @@ describe("Builder Tests (dynamic)", () => {
});
});

describe("Images Handler", () => {
it("has empty images handler directory", async () => {
expect.assertions(1);

const imageDir = await fse.readdir(
join(outputDir, `${IMAGE_LAMBDA_CODE_DIR}`)
);

expect(imageDir).toEqual([]);
});
});

describe("Default Handler", () => {
it("copies build files", async () => {
const files = await fse.readdir(
Expand Down
4 changes: 2 additions & 2 deletions packages/libs/lambda-at-edge/tests/build/build-no-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ describe("Builder Tests (no API routes)", () => {
it("has empty images handler directory", async () => {
expect.assertions(1);

const apiDir = await fse.readdir(
const imageDir = await fse.readdir(
join(outputDir, `${IMAGE_LAMBDA_CODE_DIR}`)
);

expect(apiDir).toEqual([]);
expect(imageDir).toEqual([]);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 1,
"hasExportPathMap": false,
"exportTrailingSlash": false,
"isNextImageImported": false
}

0 comments on commit 7372bf2

Please sign in to comment.