From e9cddef8dd30e47221ed432b825ab6c5f8012e2d Mon Sep 17 00:00:00 2001 From: Mischa Spiegelmock Date: Wed, 9 Nov 2022 20:54:37 -0800 Subject: [PATCH] Strip sharp dep from bundle (#19) * Remove sharp dep from build since it's provided by lambda layer * fix deps --- .projen/deps.json | 16 ++++++++-------- .projenrc.js | 3 ++- package.json | 8 +++++--- src/NextjsBuild.ts | 19 +++++++++++++++++++ yarn.lock | 4 ++-- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.projen/deps.json b/.projen/deps.json index 5a0eb34c..b9758e8b 100644 --- a/.projen/deps.json +++ b/.projen/deps.json @@ -34,10 +34,6 @@ "version": "10.0.5", "type": "build" }, - { - "name": "esbuild", - "type": "build" - }, { "name": "eslint-config-prettier", "type": "build" @@ -144,10 +140,18 @@ "name": "cross-spawn", "type": "bundled" }, + { + "name": "esbuild", + "type": "bundled" + }, { "name": "fs-extra", "type": "bundled" }, + { + "name": "glob", + "type": "bundled" + }, { "name": "indent-string", "type": "bundled" @@ -178,10 +182,6 @@ "name": "constructs", "version": "^10.0.5", "type": "peer" - }, - { - "name": "esbuild", - "type": "peer" } ], "//": "~~ Generated by projen. To modify, edit .projenrc.js and run \"npx projen\"." diff --git a/.projenrc.js b/.projenrc.js index d76f33fd..56a78166 100644 --- a/.projenrc.js +++ b/.projenrc.js @@ -28,12 +28,13 @@ const project = new awscdk.AwsCdkConstructLibrary({ '@types/fs-extra', '@types/micromatch', '@types/aws-lambda', + 'esbuild', 'aws-lambda', 'serverless-http', 'jszip', + 'glob', ] /* Runtime dependencies of this module. */, devDeps: ['aws-sdk', 'constructs@10.1.21'] /* Build dependencies for this module. */, - peerDeps: ['esbuild'], // do not generate sample test files sampleCode: false, diff --git a/package.json b/package.json index c6ebc854..abb55a52 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "aws-cdk-lib": "2.50.0", "aws-sdk": "^2.1250.0", "constructs": "10.0.5", - "esbuild": "0.15.13", "eslint": "^8", "eslint-config-prettier": "^8.5.0", "eslint-import-resolver-node": "^0.3.6", @@ -65,8 +64,7 @@ }, "peerDependencies": { "aws-cdk-lib": "^2.50.0", - "constructs": "^10.0.5", - "esbuild": "^0.15.13" + "constructs": "^10.0.5" }, "dependencies": { "@types/aws-lambda": "^8.10.108", @@ -75,7 +73,9 @@ "@types/micromatch": "^4.0.2", "aws-lambda": "^1.0.7", "cross-spawn": "^7.0.3", + "esbuild": "^0.15.13", "fs-extra": "^10.1.0", + "glob": "^8.0.3", "indent-string": "*", "jszip": "^3.10.1", "micromatch": "^4.0.5", @@ -88,7 +88,9 @@ "@types/micromatch", "aws-lambda", "cross-spawn", + "esbuild", "fs-extra", + "glob", "indent-string", "jszip", "micromatch", diff --git a/src/NextjsBuild.ts b/src/NextjsBuild.ts index 2f804f30..31b83f5a 100644 --- a/src/NextjsBuild.ts +++ b/src/NextjsBuild.ts @@ -4,6 +4,7 @@ import { Token } from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as spawn from 'cross-spawn'; import * as fs from 'fs-extra'; +import * as glob from 'glob'; import { listDirectory } from './NextjsAssetsDeployment'; import { CompressionLevel, NextjsBaseProps } from './NextjsBase'; @@ -121,6 +122,24 @@ export class NextjsBuild extends Construct { if (buildResult.status !== 0) { throw new Error('The app "build" script failed.'); } + + // cleanup + // delete the `sharp` module since it's provided by the lambda layer + const sharpPathNpm = path.join(this._getNextStandaloneDir(), 'node_modules', 'sharp'); // npm/yarn + if (fs.existsSync(sharpPathNpm)) { + // delete the sharp folder + if (!this.props.quiet) console.debug('├ Deleting sharp module from', sharpPathNpm); + fs.removeSync(sharpPathNpm); + } + // is there a `sharp@x.y.z` folder in the `node_modules/.pnpm` folder? + const pnpmModulesDir = path.join(this._getNextStandaloneDir(), 'node_modules', '.pnpm'); // pnpm + // get glob pattern for sharp version + const matches = glob.sync(path.join(pnpmModulesDir, 'sharp@*')); + if (matches.length > 0) { + // delete the sharp folder + if (!this.props.quiet) console.debug('├ Deleting sharp module from', matches[0]); + fs.removeSync(matches[0]); + } } // getNextBuildId() { diff --git a/yarn.lock b/yarn.lock index 3a62e830..5da147c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2392,7 +2392,7 @@ esbuild-windows-arm64@0.15.13: resolved "https://registry.yarnpkg.com/esbuild-windows-arm64/-/esbuild-windows-arm64-0.15.13.tgz#4ffd01b6b2888603f1584a2fe96b1f6a6f2b3dd8" integrity sha512-3bv7tqntThQC9SWLRouMDmZnlOukBhOCTlkzNqzGCmrkCJI7io5LLjwJBOVY6kOUlIvdxbooNZwjtBvj+7uuVg== -esbuild@*: +esbuild@^0.15.13: version "0.15.13" resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.15.13.tgz#7293480038feb2bafa91d3f6a20edab3ba6c108a" integrity sha512-Cu3SC84oyzzhrK/YyN4iEVy2jZu5t2fz66HEOShHURcjSkOSAVL8C/gfUT+lDJxkVHpg8GZ10DD0rMHRPqMFaQ== @@ -3025,7 +3025,7 @@ glob@^7, glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^ once "^1.3.0" path-is-absolute "^1.0.0" -glob@^8.0.1: +glob@^8.0.1, glob@^8.0.3: version "8.0.3" resolved "https://registry.yarnpkg.com/glob/-/glob-8.0.3.tgz#415c6eb2deed9e502c68fa44a272e6da6eeca42e" integrity sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==