aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.6k stars 3.9k forks source link

assets: custom watch/hotswap implementation for asset bundling #23722

Open mrgrain opened 1 year ago

mrgrain commented 1 year ago

Describe the feature

An API for asset bundling to hook into the watch/hotswap feature to create an incremental build of the asset.

Use Case

Asset bundling can be expensive. While some providers (e.g. Docker) already use an internal cache to build increments, not all do and it is the responsibility of the provider to store state.

A concrete would be bundling a lambda function with esbuild. This could use esbuild's native watch feature to update assets instead of always doing a full build. Or it could rely on CDKs file watcher, but trigger a rebuild when one of its files changed.

Proposed Solution

Maybe two variations

// Uses a custom file watcher implementation and does an incremental update
// will callback to the CDK watch context when an update has occurred
interface WatchingAsset {
  startWatching(context) {
    // watch files & update when needed
    // then inform the CDK watch context
    context.updated(/* data */);
  }
// Has a well known method that can register interests with the global CDK watcher
interface IncrementalAsset {
  gatherInterest(context) {
    watchProvider.registerInterest('handler/**/*.ts');
  }

  // this is called when ever a rebuild is needed
  rebuild(changeDetails) {
    // do the incremental rebuild here
  }
}

Other Information

No response

Acknowledgements

CDK version used

n/a

Environment details (OS name and version, etc.)

n/a

comcalvi commented 1 year ago

Are you proposing to make these external APIs? Why not make watching assets the default behavior of cdk watch?

mrgrain commented 1 year ago

Are you proposing to make these external APIs? Why not make watching assets the default behavior of cdk watch?

They probably should be public so any custom asset could implement them if desired.

Adding an implementation to the assets bundled in the CDK would make it the default behavior for those assets.