feat: resolve __SST_DIRNAME and __SST_FILENAME at built time #72
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ℹ️ Migration of Pull Request sst/sst#3348 from main SST repo.
Please see history of comment there.
Why
resolve sst/sst#1068
This enables to reference file paths as it can be done with
__dirname
or__filename
on unbundled files.This is useful for creating utils to link handler files to their trigger declaration in SST.
First example: declare triggers next to the handlers
Second example: declare handler paths next to the handler
In both cases:
We use this util with Serverless Framework and CDK which supports
__dirname
. It's painful not to have an alternative in SST.What
Create two new global variables
__SST_DIRNAME
and__SST_FILENAME
:dirname
orfilename
at the built time by an esbuild plugin.Impact
On my small project.
Without the plugin, the build of the config file takes ~3ms.
The esbuild plugin takes between 1 ms and 5 ms to parse a file. It becomes preponderant in the build process but it remains fast (~20ms). The parsed files are only the IaC files, files compiled for lambda runtime are not impacted.
For projects with thousands of IaC files, the build will be slower. But when you have a thousand lambdas you want to avoid having thousands of magic strings to link handlers to triggers 😜.
As this feature impacts performance, I propose to opt-in it. WDYT? What is the best way to do this? A new param
advanced.enablePathInjection
in SSTConfig?TODO before merge