Skip to content

Commit

Permalink
fix: cannot have multiple assets in the same scope (backport #1017) (#…
Browse files Browse the repository at this point in the history
…1019)

This is an automatic backport of pull request #1017 done by [Mergify](https://mergify.com).

---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>
  • Loading branch information
mergify[bot] authored Dec 24, 2023
1 parent 7e87219 commit 093165a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './asset';
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';
import { defaultPlatformProps } from './private/utils';
import { defaultPlatformProps, uniqueAssetId } from './private/utils';

export { CodeConfig } from 'aws-cdk-lib/aws-lambda';
export interface JavaScriptCodeProps extends AssetBaseProps {};
Expand All @@ -29,7 +29,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 +137,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 +186,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
14 changes: 14 additions & 0 deletions src/private/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IConstruct } from 'constructs';
import { BuildOptions, Platform, TransformOptions } from '../esbuild-types';

export function isEsbuildError(error: unknown): boolean {
Expand All @@ -22,3 +23,16 @@ export function defaultPlatformProps(options?: BuildOptions | TransformOptions):

return {};
}

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}`;
};
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 './private/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
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,
],
});
});
});
});

0 comments on commit 093165a

Please sign in to comment.