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

JavaScript Understanding webpack.config.js

What about if I want to output multiple files, such as vendor.js and bundle.js?

What about if I want to output multiple files, such as vendor.js and bundle.js?

Many cases I may want 2 different output files, maybe because I am using require.ensure to lazy load modules.

Angelica Hart Lindh
Angelica Hart Lindh
19,465 Points

Could you post your webpack.config file to look at?

3 Answers

Angelica Hart Lindh
Angelica Hart Lindh
19,465 Points

Ah ok no problem David.

I think there are definitely circumstances where splitting files for lazy loading is beneficial, such as if you are working on a large scale application. However, bundling everything into one file, such as *.js and *.css files, for medium to small applications is performant as well. For the user, they will be generally experience a slightly longer initial load time during the first visit, anything following that will be super fast as it will have already be downloaded client side. This is beneficial for things like navigation. It's actually better to perform one large HTTP request than it is for two. In addition, the user will download the file and simply cache it for the next visit. A big performance improvement with one 'bundle.js' is to minify/uglify it, this will really make big differences to the file size.

I have used the code splitting in Webpack but it didn't offer much in performance improvement as the project wasn't so large.

Plenty of discussion on Stack Overflow and Reddit.

I don't have any yet. I was watching the course for the first time. My question is:

Webpack is bundling everything on a bundle.js. But on an ideal world, all the dependencies and code for a certain module would be lazy loaded based on the needs.

Ali Raza
Ali Raza
13,633 Points

David Ferreira,

You should be able to do by using Webpack multiple entry points checkout the docs

{
  entry: {
    app: './src/app.js',
    search: './src/search.js'
  },
  output: {
    filename: '[name].js',
    path: __dirname + '/dist'
  }
}

// writes to disk: ./dist/app.js, ./dist/search.js