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 #1016) #1017

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -10,6 +10,7 @@ import {
} from './asset';
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';
import { uniqueAssetId } from './utils';

function nodeMajorVersion(): number {
return parseInt(process.versions.node.split('.')[0], 10);
Expand All @@ -32,7 +33,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 @@ -145,7 +146,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 @@ -194,7 +195,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
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +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 @@ -342,3 +342,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 @@ -178,4 +178,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