From 77ec9649168e48c01272f171096825a460a06994 Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Fri, 14 Aug 2020 22:16:20 +0200 Subject: [PATCH] fix(lambda-nodejs): cannot bundle when entry file is named index.ts Do not rename the dist file if it's already named `index.js`. Fixes #9709 --- .../aws-lambda-nodejs/lib/bundling.ts | 2 +- .../aws-lambda-nodejs/test/bundling.test.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts index 7ff7b06ef741c..3122b8339914d 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts @@ -168,7 +168,7 @@ export class Bundling { // Always rename dist file to index.js because Lambda doesn't support filenames // with multiple dots and we can end up with multiple dots when using automatic // entry lookup - `mv ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${distFile} ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/index.js`, + distFile !== 'index.js' ? `mv ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${distFile} ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/index.js` : '', ]); let installer = Installer.NPM; diff --git a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts index 535d9ec0d0541..b0a731e576e19 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts @@ -74,6 +74,25 @@ test('Parcel bundling', () => { expect(findUpMock).toHaveBeenCalledWith('package.json', '/project/folder'); }); +test('Parcel bundling with handler named index.ts', () => { + Bundling.parcel({ + entry: '/project/folder/index.ts', + runtime: Runtime.NODEJS_12_X, + projectRoot: '/project', + }); + + // Correctly bundles with parcel + expect(Code.fromAsset).toHaveBeenCalledWith('/project', { + assetHashType: AssetHashType.BUNDLE, + bundling: expect.objectContaining({ + command: [ + 'bash', '-c', + '$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/index.ts --target cdk-lambda --dist-dir /asset-output --no-autoinstall --no-scope-hoist', + ], + }), + }); +}); + test('Parcel with Windows paths', () => { Bundling.parcel({ entry: 'C:\\my-project\\lib\\entry.ts',