Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Linking many files best practice

What's the best practice when including a ton of CSS and JS files and using a build tool like Grunt / Gulp / Webpack?

5 Answers

Kevin Korte
Kevin Korte
28,149 Points

On any project of scale, multiple files are the way to go to keep your sanity. When you have 1500 lines of javascript, and you need to add "one thing" to happen at that "one time", it's very difficult to find, and very easy to screw up.

If your javascript is broken out into 15-20 modules about 80-100 lines of code, it's easier to narrow in and where you need to be working, and its easier to not screw up, since you isolated your changes to only the code that is in that file, not anything else, by accident. Let the computer turn all of those files into one big one.

And in regards to "this advice might change in the future". Well, the performance problem we're trying to avoid here with HTTP 1.1 is latency. Latency is the amount of time it takes to make a response, and receive a response. Less responses to make, less latency, faster page loading. Basically a new TCP connection is used every time it has to fetch an asset from the server. The number of connections are limited by the browser, and the physical distance from the server causes delays, and there is more time delays establishing the connection.

This is why everyone recommends fewer, larger files.

Why is this changing, well, because we HTTP/2 coming. With HTTP/2, numerous assets can be fetched with the same TCP connection, (meaning we reused existing connections).

Check this out from New Relic: https://blog.newrelic.com/2016/02/09/http2-best-practices-web-performance/

The article is only from Feb 9th, so it's relatively new...but it sounds like this is the direction we are heading.

Kevin Korte
Kevin Korte
28,149 Points

Have it compile into one larger, minified file. However this advice might change in the near future.

I agree with Kevin Korte. A lot of people like to create a "dist" folder and you pipe your code from your build system, minified, compressed, what have you. When using git, in your git ignore file, add the dist folder to the ignore file, that way you won't add the .min files to the repo.

Seems like it would be difficult to manage many files that way. What did you mean by "this advice might change in the near future"?

Awesome just need more browser support for it.