Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot have multiple assets in the same scope (backport #1017) #1019

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';
import { defaultPlatformProps } from './private/utils';
import { uniqueAssetId } from './utils';

export { CodeConfig } from 'aws-cdk-lib/aws-lambda';
export interface JavaScriptCodeProps extends AssetBaseProps {};
Expand All @@ -29,7 +30,7 @@ export class EsbuildCode<
protected getAsset(scope: Construct): EsbuildAsset<AssetProps> {
return new EsbuildAsset(
scope,
this.constructor.name,
uniqueAssetId(scope, this.constructor.name),
this.props,
);
}
Expand Down Expand Up @@ -137,7 +138,7 @@ export class JavaScriptCode extends EsbuildCode<JavaScriptCodeProps> {
protected getAsset(scope: Construct): EsbuildAsset<AssetProps> {
return new JSAsset(
scope,
this.constructor.name,
uniqueAssetId(scope, this.constructor.name),
this.props,
);
}
Expand Down Expand Up @@ -186,7 +187,7 @@ export class TypeScriptCode extends EsbuildCode<TypeScriptCodeProps> {
protected getAsset(scope: Construct): EsbuildAsset<AssetProps> {
return new TSAsset(
scope,
this.constructor.name,
uniqueAssetId(scope, this.constructor.name),
this.props,
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Construct } from 'constructs';
import { AssetBaseProps, AssetProps, JavaScriptAsset, TypeScriptAsset } from './asset';
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';
import { uniqueAssetId } from './utils';


export interface JavaScriptSourceProps extends AssetBaseProps{};
export interface TypeScriptSourceProps extends AssetBaseProps{};
Expand Down Expand Up @@ -75,7 +77,7 @@ abstract class Source<
if (!this.asset) {
this.asset = new this.assetClass(
scope,
this.constructor.name,
uniqueAssetId(scope, this.constructor.name),
this.props,
);
} else if (Stack.of(this.asset) !== Stack.of(scope)) {
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IConstruct } from 'constructs';

export function isEsbuildError(error: unknown): boolean {
return !!error
&& typeof error == 'object'
&& error != null
&& 'errors' in error
&& 'warnings' in error;
}


const assetIds = new WeakMap<IConstruct, number>();
export const uniqueAssetId = (scope: IConstruct, name: string) => {
const nextId = (assetIds.get(scope) ?? 0) + 1;
assetIds.set(scope, nextId);

// Only one asset per scope, skip the id
if (nextId === 1) {
return name;
}

return `${name}${nextId}`;
};
18 changes: 18 additions & 0 deletions test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,21 @@ describe('Amazon CloudWatch Synthetics', () => {
});
});
});


describe('multiple Code in the same scope', () => {
it('does not throw', () => {
const stack = new Stack();

const codeOne = new TypeScriptCode('fixtures/handlers/ts-handler.ts', {
buildOptions: { absWorkingDir: resolve(__dirname) },
});

const codeTwo = new TypeScriptCode('fixtures/handlers/ts-handler.ts', {
buildOptions: { absWorkingDir: resolve(__dirname) },
});

codeOne.bind(stack);
codeTwo.bind(stack);
});
});
32 changes: 32 additions & 0 deletions test/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,36 @@ describe('source', () => {
expect(source.props.buildOptions.platform).toBe('browser');
});
});

describe('with multiple sources in the same scope', () => {
it('does not throw', () => {
const stack = new Stack();

const sourceOne = new TypeScriptSource('fixtures/handlers/ts-handler.ts', {
buildOptions: {
absWorkingDir: resolve(__dirname),
},
});
const sourceTwo = new TypeScriptSource('fixtures/handlers/ts-handler.ts', {
buildOptions: {
absWorkingDir: resolve(__dirname),
},
});

const destinationBucket = new Bucket(stack, 'WebsiteBucket', {
autoDeleteObjects: true,
publicReadAccess: true,
removalPolicy: RemovalPolicy.DESTROY,
websiteIndexDocument: 'index.html',
});

new BucketDeployment(stack, 'MultipleAssets', {
destinationBucket,
sources: [
sourceOne,
sourceTwo,
],
});
});
});
});
Loading