-
Notifications
You must be signed in to change notification settings - Fork 153
Feature request: consider adopting tslib
for runtime helpers
#1674
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
Comments
I have been experimenting with the As stated in the OP, the
When the setting is enabled, TypeScript won't emit these helpers in the transpiled code and the operator is expected to provide an implementation for the helpers and make it available at runtime. This is done via the If taken at face value, the idea of not emitting these helpers in each file that needs them and instead import them from a central location ( In doing exactly this I wanted to measure the bundle size savings and so I made a build of Logger and Commons with the setting disabled in which the helpers are generated in the code (aka current status), and another with Next, I created a bare bones function file that only imports the Logger utility and instantiates it: import { Logger } from '@aws-lambda-powertools/logger';
// const { Logger } = require('@aws-lambda-powertools/logger)';
const logger = new Logger({}); The file is purposefully trivial (and short) so that we can look at the output and measure the impact that the setting has on Powertools and on functions using it. Then, I bundled both versions with Below the result analyzed with this tool and using the meta file generated by esbuild as input: CJS with Note The ESM result is the same regardless of The results are pretty interesting and not at all what I was expecting. For ESM, using the setting one way or the other doesn't yield any difference (I double checked!), this is because the helpers involved in this project are related to imports and not needed in our setup. For CJS however the difference is substantial, and the version with I was surprised by this result as I was expecting this version to be smaller than the one with helpers emitted, so I looked more into the result. Upon examining the In both instances, this is the code emitted in each file: var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
// ...
const lodash_merge_1 = __importDefault(require("lodash.merge")); Looking at the unminified esbuild-bundled output in CJS I also noticed that the With this in mind, and considering the fact that we are actually emitting these helpers only twice, it doesn't appear to be worth it to enable the This is definitely something that we'll have to keep an eye on, since our transpiled output might change and the tradeoff could become very different, but at least for now the size of the As a side note, this exercise also made me think about whether there's anything that could be done about For now I'll be closing this issue as not planned. |
|
Use case
When transpiling TypeScript code to JavaScript,
tsc
generates and injects helpers that allow developers to author code using a certain runtime (i.e. Node.js 18) and target other versions (i.e. Node.js 16).The default behavior that TypeScript adopts is to include these helpers in each transpiled file that needs them. TypeScript however offers these same utilities also as a published npm package called
tslib
which can be installed and used as runtime dependency.Following this pattern could potentially bring bundle size savings since the helpers are imported and present in the bundle only once rather than be present in each file that uses them. This however could also have some performance overhead for those customers who choose to not bundle their dependencies since there is now an additional dependency & files to read from disk.
The extent of the impact in both those areas is still to be assessed, however as a point of reference the AWS SDK for JavaScript v3 is using the
tslib
option suggested in this issue.This behavior is governed by two compiler options:
importHelpers
andnoEmitHelpers
. As part of this work we should experiment with them and asses the following:tslib
As part of this issue, we could also explore two additional settings that might be adjacent to the ones above:
downlevelIteration
isolatedModules
Solution/User Experience
From customers perspective the change should be transparent and the utilities should continue working as intended without any change needed. However, in an effort to err on the safe side I suggest we make this change as part of the next major release, this way we'll have a chance to highlight the change in the migration guide/details and make it more explicit.
Alternative solutions
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: