Skip to content

Commit

Permalink
fix: cannot have multiple assets in the same scope (#1016)
Browse files Browse the repository at this point in the history
Fixes #993

(cherry picked from commit 6777ba2)

# Conflicts:
#	src/source.ts
  • Loading branch information
mrgrain authored and mergify[bot] committed Dec 24, 2023
1 parent 7e87219 commit b5c23e5
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@ import {
ISource,
SourceConfig,
} from 'aws-cdk-lib/aws-s3-deployment';
<<<<<<< HEAD

Check failure on line 7 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
import { Construct } from 'constructs';
import { AssetBaseProps, AssetProps, JavaScriptAsset, TypeScriptAsset } from './asset';
=======

Check failure on line 10 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
import { Construct, IConstruct } from 'constructs';
import { TypeScriptAsset, TypeScriptAssetProps } from './asset';
>>>>>>> 6777ba2 (fix: cannot have multiple assets in the same scope (#1016))

Check failure on line 13 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
import { EntryPoints } from './bundler';
import { BuildOptions } from './esbuild-types';

<<<<<<< HEAD

Check failure on line 17 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
export interface JavaScriptSourceProps extends AssetBaseProps{};
export interface TypeScriptSourceProps extends AssetBaseProps{};
=======

Check failure on line 20 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
const assetIds = new WeakMap<IConstruct, number>();
const assetId = (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}`;
};

export interface TypeScriptSourceProps extends TypeScriptCodeProps {};
>>>>>>> 6777ba2 (fix: cannot have multiple assets in the same scope (#1016))

Check failure on line 35 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.

abstract class Source<
Props extends JavaScriptSourceProps | TypeScriptSourceProps,
Expand Down Expand Up @@ -181,6 +203,55 @@ TypeScriptAsset
*/
props: TypeScriptSourceProps = {},
) {
<<<<<<< HEAD

Check failure on line 206 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
super(entryPoints, props);
=======

Check failure on line 208 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
const defaultOptions: Partial<BuildOptions> = {
platform: 'browser',
};

this.props = {
entryPoints,
...props,
buildOptions: {
...defaultOptions,
...props.buildOptions,
},
};
}


bind(scope: Construct, context?: DeploymentSourceContext): SourceConfig {
// If the same AssetCode is used multiple times, retain only the first instantiation.
if (!this.asset) {
this.asset = new TypeScriptAsset(
scope,
assetId(scope, this.constructor.name),
this.props,
);
} else if (Stack.of(this.asset) !== Stack.of(scope)) {
throw new Error(
`Asset is already associated with another stack '${
Stack.of(this.asset).stackName
}'. ` + 'Create a new Asset instance for every stack.',
);
}

if (!context) {
throw new Error(
`To use a ${this.constructor.name}, context must be provided`,
);
}

// we give permissions on all files in the bucket since we don't want to
// accidentally revoke permission on old versions when deploying a new
// version (for example, when using Lambda traffic shifting).
this.asset.bucket.grantRead(context.handlerRole);

return {
bucket: this.asset.bucket,
zipObjectKey: this.asset.s3ObjectKey,
};
>>>>>>> 6777ba2 (fix: cannot have multiple assets in the same scope (#1016))

Check failure on line 255 in src/source.ts

View workflow job for this annotation

GitHub Actions / build

Merge conflict marker encountered.
}
}
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 b5c23e5

Please sign in to comment.