-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
RFC: Amplify Library Modularization and Bundle Size Improvement #3365
Comments
I tried the preview versions with my Angular app. The reduction in bundle size was significant. However the setup process wasn't easy. I had to go through a lot of trial and error to make it work. Steps
|
@tuhinkarmakar Hi, thanks for testing! I think Step 1, 2, 4 are required but Step 3 is not. Do you have any issue if not calling |
@powerful23 Without step 3, I was getting an error saying No Auth module registered with Amplify. I updated my comment to add 2 more steps that were required. |
Even when just pulling in |
@mydea it seems |
Does this: https://aws.amazon.com/blogs/developer/new-aws-sdk-for-javascript-developer-preview/ influence the need for writing your own http client? If you can import just the parts of the sdk you need, you can significantly reduce the bundle size. |
If I try to force using |
Impressive, just shaved off around 500K from 1.68 to 1.17, still this library is taking between 30 - 35% of the remaining 1.17 and i only need Authentication. I am using the preview of Angular. I see the auth angular components inside the final bundle and am not using them, i have my own custom components. And they are occupying a whooping 5% on their own. For some tree shaking is not removing them for some reason. |
Yes!! 🚀🙌😄 |
I must echo @ahallora, GraphQL is one of the largest bundled packages in production app... yet we don't use it at all. It's a massive tax when using aws-amplify to the point where we're considering forking @aws-amplify/api just to strip it out. |
@powerful23 thought I'd link your PR here for splitting the API and Graphql features: |
This shaved off 180kb from my bundle of just analytics and auth but it's still 400kb in total. Would love to see the modularised AWS SDK used in this preview version. |
I only use auth, but this branch still pulls in Analytics in my build unfortunately. |
@powerful23 is there plans to keep the preview tagged release in-sync with the updates happening in stable? I think the current preview is two releases behind the stable version currently. |
@jadbox @roblangridge are you running the app in the production mode? |
@powerful23 yes, with mode: "deployment" things are looking like this: |
@powerful23 not sure what you mean by "production mode", but building with CRA via "npm build" is what I'm using, while setting NODE_ENV to production. I can still confirm that graphql is being bundled even though I'm not importing it or any libs that use it: If need be, I can get you access to my project for testing (jdunlap AT outlook.com). |
@jadbox I think you need to configure your webpack config to add |
@jadbox or better yet, how are you building? If you are using |
It's really great, but seems poorly documented, that if you just want the Auth module, and you do this: import Auth from '@aws-amplify/auth' You save nearly 600K on your build over doing this: import { Auth } from 'aws-amplify' |
@powerful23 @aldo-roman sorry for the delay, I am building in production mode (via create-react-app). All my imports are in the style of Using |
I'm having difficulty bundling aws-amplify with rollup, encountering the exact same error described in aws/aws-sdk-js#1769 (comment) I think because aws-sdk v2 isn't published as es modules. Could aws-amplify depend on v3 instead v2 to make it be more friendly to modern bundler like rollup? |
I've ended up rolling my own analytics and auth script using the v3 sdk as I wasn't really using any other parts of amplify. Good news is that bundle is a 35kb download. It's part of a WP plugin right now but I'll see if I can extract it out as an npm package. |
@roborourke how do you roll your own auth with v3 sdk? afaik, the only relevant package is client-cognito-identity-browser, and it only lets you manage the user pool & identity pool but not the actual authentication? |
@hgl you call GetId which requires no auth, then GetCredentialsForId with that ID. Then you can use those credentials for calling the PutEvents and UpdateEndpoint pinpoint commands |
Thanks for prioritizing this change. We are currently using
Alternatively, we've been thinking about migrating to using |
Any update on (a) when this will be merged in to the main release, and (b) when GraphQL will be removed from those needing only the API ? |
Hey everyone, wanted to provide an update of the Modularization work, we currently heads down on this in order to deliver this feature in the coming weeks. Will provide an update on this next week for you. Thanks! |
@depiction, @jliebrand and @bezreyhan, we are in our final phase of testing the new modularization release and is available for you to test as well using preview tag. Can you try testing that release in the meantime and let us know if you face any issues? |
I'm using Update It looks like the Auth module is 31kb and the aws-sdk is 10.71kb. This is a huge improvement over the 74kb I was seeing before. Thanks for making these changes! From my initial testing, login, logout, and sign up all worked as expected. I'm wondering if there is a way where we could make the Auth module even lighter. Even with these changes, the Auth module is our largest dependency. |
You can use the npm preview tag as |
Modularization is available in PreviewHi all, With us launching Modularization in the coming weeks, we wanted to provide the ability for you to use our current preview tag (https://www.npmjs.com/package/aws-amplify) in order to start using Modularization within your applications. We highly recommend to not use this preview tag in production as we are still in the process of fixing small bugs and validating with our own testing internally. Breaking Changes:Below is a breakdown of the breaking changes that are coming with v3 that we wanted to make you aware of:
Installing preview-tag:For installing Amplify modules:You are able to install by running For installing aws-amplify:You are able to install by running For installing UI Components:You are able to install by running NOTE: Please delete your existing node_modules before installing as there are some conflicts with the older versions of package. Filing Bugs:If you do find issues, please let us know by cutting us a bug and we can look into the issue. Thank You! |
Not sure if actually a bug, or my own issue. Can you provide a minimal example of how to use Edit: So I think my issue was that I separately installed each dependency:
Rather than doing them together:
Since it worked after I did it again the latter way. Old post detailsThis is what I have, and but I get a pile of 'cannot import' type errors. Installed both with the
It wasn't mentioned above, but do we also need to add all of these (based on what we want to use)? Or should they all just come 'as part of it'?
import { Auth } from 'aws-amplify'
import { withOAuth } from 'aws-amplify-react'
Auth.configure({
// REQUIRED - Amazon Cognito Region
region: 'us-east-1',
// OPTIONAL - Amazon Cognito User Pool ID
userPoolId: 'us-east-1_REDACTED',
// OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
userPoolWebClientId: 'REDACTED',
// OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
mandatorySignIn: false,
// Cognito Hosted UI configuration
oauth: {
domain: 'REDACTED',
scope: ['email', 'profile', 'openid'],
redirectSignIn: 'http://localhost:3000/',
redirectSignOut: 'http://localhost:3000/',
responseType: 'code', // or 'token', note that REFRESH token will only be generated when the responseType is code
},
}) |
Hi @0xdevalias, there shouldn't be any difference between installing both the packages in one command or separately, it seems that aws-amplify-react package was installed the preview version but somehow aws-amplify was reverted or still using the non-preview version. Let us know if you are able to reproduce it again. |
@Amplifiyer can I suggest one more thing? Moving |
@imedadel Do you mean aws-sdk version 2.x or the new aws-sdk-js-v3? We have completely removed the dependency on aws-sdk version 2.x and only depends on aws-sdk-js-v3. I'm not completely convinced that moving that to peer dependency is better mostly because currently the whole Amplify library extensively depends on it and have strict requirements on which version to use. Since peer dependencies don't help in reducing the application bundle size but only library's bundle size which is mostly irrelevant (that's my understanding, happy to be convinced otherwise). |
Closing this RFC as all the proposed solutions have been launched, see https://aws.amazon.com/blogs/mobile/amplify-framework-announces-new-rearchitected-ui-component-and-modular-javascript-libraries/ Thank you everyone for great feedback and all the contributors for making this happen. |
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs. Looking for a help forum? We recommend joining the Amplify Community Discord server |
This issue is a Request For Comments (RFC). It is intended to elicit community feedback regarding a proposed change to the library. Please feel free to post comments or questions here.
Situation
The bundle size of amplify-js has always been a concern to the customers. At the very beginning we put all the categories in a single package which leads to a huge bundle size. Then we decided to split the whole package into several ones based on the category so we can import each category on need.
The Problem
Although the bundle size can be largely reduced by using scoped packages instead of directly using
aws-amplify
, there are still many things that we can do to make it better:aws-amplify-react
, all the Amplify library will be imported and bundled regardless of the actual usage.node_modules
aws-sdk
takes a lot of the space in the bundle size but we are only using part of it.Proposed Solutions
aws-amplify
. So instead of using scoped packages likeimport Auth from '@aws-amplify/auth'
you can now just useimport { Auth } from 'aws-amplify'
and the bundler will do the rest work for you.import { RestAPI, GraphQLAPI } from 'aws-amplify'
aws-sdk
. The client should be able to generate the request context, sign the request with AWS credentials, validate the input parameters, configure the retry policy etc.Related Issues
#122
#2781
#3097
#3096
#2309
#2199
#1831
#701
#2894
#2058
#488
Reference
Thanks to @aldo-roman for the working of #2313
Preview Versions
05/31/19
You can now use the preview version of amplify to test:
The preview version has generated the ESM modules for all the categories so for example, if you are using webpack 4 bundling your react app, and you are only using
Auth
module like:Then only the
Auth
module will be included in the bundle. Note that the tree shaking will only happen under the production mode.The text was updated successfully, but these errors were encountered: