Skip to content

Commit

Permalink
chore(custom-resource-handlers): refactor minify and bundling step (#…
Browse files Browse the repository at this point in the history
…26287)

Make `minify-and-bundle-sources.ts` more clear by putting the bundled files into its own `dist` folder, and copying that `dist` folder into `aws-cdk-lib`.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored Jul 7, 2023
1 parent 6da9a4c commit 4c0dae6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/custom-resource-handlers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"version": "0.0.0",
"scripts": {
"build": "tsc -b && node scripts/minify-sources.js",
"build": "tsc -b && node scripts/minify-and-bundle-sources.js",
"integ": "integ-runner",
"lint": "cdk-lint",
"package": "cdk-package",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function recFolderStructure(fileOrDir: string) {
recFolderStructure(path.join(fileOrDir, i));
}
} else {
if (path.extname(fileOrDir) === '.ts' && !fileOrDir.includes('.d.ts') && !fileOrDir.includes('nodejs-entrypoint')) {
// Only minify + bundle 'index.ts' files.
// The reason why they are called 'index.ts' is that aws-cdk-lib expects that
// as the file name and it is more intuitive to keep the same name rather than
// rename as we copy it out.
if (fileOrDir.includes('index.ts')) {
entryPoints.push(fileOrDir);
}
}
Expand All @@ -23,7 +27,7 @@ recFolderStructure(bindingsDir);
for (const ep of entryPoints) {
void esbuild.build({
entryPoints: [ep],
outfile: `${ep.slice(0, ep.lastIndexOf('.'))}.js`,
outfile: calculateOutfile(ep),
external: ['@aws-sdk/*'],
format: 'cjs',
platform: 'node',
Expand All @@ -36,3 +40,14 @@ for (const ep of entryPoints) {
tsconfig: 'tsconfig.json',
});
}

function calculateOutfile(file: string) {
// turn ts extension into js extension
file = path.join(path.dirname(file), path.basename(file, path.extname(file)) + '.js');

// replace /lib with /dist
const fileContents = file.split(path.sep);
fileContents[fileContents.lastIndexOf('lib')] = 'dist';

return fileContents.join(path.sep);
}
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-ecr/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ export class Repository extends RepositoryBase {
private enableAutoDeleteImages() {
const firstTime = Stack.of(this).node.tryFindChild(`${AUTO_DELETE_IMAGES_RESOURCE_TYPE}CustomResourceProvider`) === undefined;
const provider = CustomResourceProvider.getOrCreateProvider(this, AUTO_DELETE_IMAGES_RESOURCE_TYPE, {
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'lib', 'aws-ecr', 'auto-delete-images-handler'),
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'dist', 'aws-ecr', 'auto-delete-images-handler'),
useCfnResponseWrapper: false,
runtime: CustomResourceProviderRuntime.NODEJS_18_X,
description: `Lambda function for auto-deleting images in ${this.repositoryName} repository.`,
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ export class Bucket extends BucketBase {

private enableAutoDeleteObjects() {
const provider = CustomResourceProvider.getOrCreateProvider(this, AUTO_DELETE_OBJECTS_RESOURCE_TYPE, {
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'lib', 'aws-s3', 'auto-delete-objects-handler'),
codeDirectory: path.join(__dirname, '..', '..', 'custom-resource-handlers', 'dist', 'aws-s3', 'auto-delete-objects-handler'),
useCfnResponseWrapper: false,
runtime: CustomResourceProviderRuntime.NODEJS_18_X,
description: `Lambda function for auto-deleting objects in ${this.bucketName} S3 bucket.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ customresourcedir=$(node -p "path.dirname(require.resolve('@aws-cdk/custom-resou
awscdklibdir=${scriptdir}/..

list_custom_resources() {
for file in $customresourcedir/lib/*/*/index.js; do
for file in $customresourcedir/dist/*/*/index.js; do
echo $file | rev | cut -d "/" -f 2-4 | rev
done
}
Expand Down

0 comments on commit 4c0dae6

Please sign in to comment.