Skip to content
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

Generate smaller bundles #94

Merged
merged 2 commits into from
Jul 13, 2017
Merged

Generate smaller bundles #94

merged 2 commits into from
Jul 13, 2017

Conversation

jsayol
Copy link
Contributor

@jsayol jsayol commented Jul 8, 2017

Work in progress, do not merge yet. Edit: Ready for review

I'm tweaking the build process to generate smaller bundles.

I still want to try a few more things but I'm already seeing some serious improvement. firebase-database.js is still not back to pre-TypeScript size but every other bundle is smaller:

File Before After % diff
firebase-app.js 18.87 KB 16.19 KB -14.20%
firebase-database.js 240.49 KB 181.47 KB -24.54%
firebase-messaging.js 24.93 KB 19.94 KB -20.10%
firebase-storage.js 53.91 KB 36.17 KB -32.91%
firebase.js 475.87 KB 391.45 KB -17.74%

@@ -172,13 +166,13 @@ function compileIndvES2015ModulesToBrowser() {
},
footer: fileName => {
return isFirebaseApp(fileName) ? `
})();` : `
})().default;` : `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably leave a comment here pointing to babel/babel#2212 for future reference as to why this is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that link, it has a lot of useful info. Adding .default was more of a temporary fix after the changes I made, I want to see if there's a better (proper) way to export/import the namespace.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I was using babel-plugin-add-module-exports for that reason. If we are going to move away from using the babel-loader then we need to do something to recreate that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment pointing to that url for reference. I finally opted not to use babel-plugin-add-module-exports since it needs CommonJS modules to do its thing, but webpack needs to work with ES2015 modules in order to perform tree shaking.

@jsayol
Copy link
Contributor Author

jsayol commented Jul 12, 2017

Ok, I'm relatively happy with the results now.

Here's the final size diff compared with the current master:

File Before After % diff
firebase-app.js 18.87 KiB 16.14 KiB -14.47%
firebase-database.js 240.49 KiB 166.19 KiB -30.90%
firebase-messaging.js 24.93 KiB 18.22 KiB -26.92%
firebase-storage.js 53.91 KiB 33.11 KiB -38.58%
firebase.js 475.87 KiB 371.33 KiB -21.97%

Here's how they compare with the latest published version (4.1.3):

File Before After % diff
firebase-app.js 17.94 KiB 16.14 KiB -10.03%
firebase-database.js 117.75 KiB 166.19 KiB +41.14%
firebase-messaging.js 24.86 KiB 18.22 KiB -26.71%
firebase-storage.js 53.69 KiB 33.11 KiB -38.33%
firebase.js 351.91 KiB 371.33 KiB +5.52%

Both firebase-database.js and firebase.js are still larger than they were pre-TypeScript but overall it's a pretty good improvement, all considered.

These last few days I've also been experimenting with more aggressive settings for UglifyJS's mangling of property names but I've run into so many issues, so I'm setting that approach aside for now. It might be worth revisiting in the future though, unless there's any better options available.

P.S.: I didn't mention firebase-auth.js at all since that bundle comes already pre-built, nothing I can do there.

@jsayol jsayol changed the title WIP: Generate smaller bundles Generate smaller bundles Jul 12, 2017
}]
},
plugins: [
new CheckerPlugin(),
new webpack.DefinePlugin({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I diff'ed doing the compression w/ and w/o this. For me I was seeing a little bit better results compiling database without this.

Size Comparison

File With webpack.DefinePlugin Without webpack.DefinePlugin
firebase-database.js 166.19 KB 166.16 KB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I'll get rid of it then.

@@ -171,8 +157,9 @@ function compileIndvES2015ModulesToBrowser() {
`;
},
footer: fileName => {
// Note: '.default' needed because of https://github.com/babel/babel/issues/2212
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this!

mangle: {
props: {
ignore_quoted: true,
regex: /_$/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find on this, I found that by expanding this regex I was able to get some other small savings. The regex I used included the _ prefix:

i.e.

/_$|^_/

I think, once this is in, going through and refactoring property names that don't get exposed (regardless of public or private markings as I know we use some of those for internally public stuff) we can get some more savings.

Size Comparison

File Suffix Only Regex Prefix + Suffix Regex
firebase-app.js 16.14 KB (5.61 KB) 15.7 KB (5.55 KB)
firebase-database.js 166.16 KB (45.55 KB) 165.8 KB (45.52 KB)
firebase-messaging.js 18.22 KB (5.59 KB) 18.2 KB (5.59 KB)
firebase.js 371.3 KB (108.1 KB) 370.49 KB (108.02 KB)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! The SDK code only uses _ as a suffix but other imported dependencies must be using it as a prefix too. I'll update it with this change.

@jsayol
Copy link
Contributor Author

jsayol commented Jul 12, 2017

Let me know before you merge this and I'll squash the commits.

@jshcrowthe
Copy link
Contributor

jshcrowthe commented Jul 13, 2017

@jsayol no worries we always squash merge so no need to do it manually.

@jshcrowthe
Copy link
Contributor

Tests all pass and things are looking fantastic, thanks so much @jsayol!

@jshcrowthe jshcrowthe merged commit bcd13ad into firebase:master Jul 13, 2017
@jsayol jsayol deleted the bundle-size branch July 13, 2017 07:23
@skywalkerlw
Copy link

for my react-app, firebase js takes round 436kb sizes, still too big :(

image

@firebase firebase locked and limited conversation to collaborators Oct 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants