Performance Webpack Build.js

Asked

Viewed 354 times

13

I’m starting with Webpack. It compiles all javascript and css files into a single file Build.js, Bundle.js, whatever it is... In the end it generates a fully minified file using the Node command Production, but even so it generates a relatively heavy file. For a large project ( Which is mine ), it can reach 10mb easy. Charging 10mb at once is very bad.
How does Webpack control this? What is the advantage of using it if it brings me a minified but huge file? It selects the scripts that will be loaded from import?

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
          }
        }
      },
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.s(a|c)ss$/,
        loader: 'style-loader!css-loader!sass-loader'
      },
      { test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader?limit=100000'
      }
    ]
  },
  resolve: {
    extensions: ['.json', '.min.css', '.min.js', , '.scss' , '.css', '.js' ],
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map',
  plugins:[
    new webpack.ProvidePlugin({
       $: "jquery",
       jQuery: "jquery"
    })
  ]
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}



Thank you!

  • 1

    It actually does a lot more than that. But you need to learn to control what it’s doing, which takes a while. It seems the case to generate several "Chunks" of separate JS, not a single build. The webpack is made for this, and can know what is in each generated js file and load them bit by bit as needed. Post your webpack configuration on the question that improves the chances of you getting help in your specific case.

  • Thanks for the answer! For what you gave to see I’m still beeem layman in Webpack. I made some changes to the hand as needed. But I basically made these settings from a Vuejs CLI. It at the end generates me a file of about 20 lines, but each file minified in only one line

  • 1

    I personally find the webpack complicated, I’ve been using it for a while (1 year or so), and there’s a lot I don’t know. For example, I know he does what I say, but I don’t know how to do it in his code without rereading the documentation. Let’s see if anyone comes in here who knows how to answer in the can. Or at some point I’ll come back here and answer, 'cause I need to learn how to do that, too :)

  • Yay. He’s really good, right, but I got a kick out of it. There must be some detail I must be forgetting rsrs, anyway, I appreciate the help :)

  • Does this give a light? https://hackernoon.com/optimising-your-application-bundle-size-with-webpack-e85b00bab579 that question in the English OS also appears to be on the same line https://stackoverflow.com/questions/31040379/webpack-creating-large-file--withsmallproject

1 answer

1


In fact Webpack offers alternatives, the Code Splitting using plugins, and demand loading using import library Systemjs, to which returns a Promise.

But I see from your code that you’re using Vue.js. You can Vue.js makes loading on demand even easier, See more about this: in own documentation of Vue.js, and in that article. There are several articles on the internet about this.

More options:

  1. If the application is on the internet or you are 100% sure that the user will have internet connection. Use CDN wherever possible.

    a) The user may already cache the same third party libraries that you use, mainly bootstrap, jQuery, angular, React browser...

    b) probably the user will download the contents of a library on the CDN server than on the hosting server and with the minimum latency, reduced response times to download each file. CDN servers are robust to ensure bandwidth, low latency and availability.

    c) Here I recommend CDN to third party libraries almost always, because of item (b). It is up to the developer to make benchmarks and simulate use cases, because there may not be much difference in the case of a site hosted on good servers within walking distance of most users. Spoiler: Even with multiple separate HTTP requests, very low latency and bandwidth give CDN an advantage.

  2. Compress your Javascript files via a gzip compression on the web server (apache, Nginx, IIS) or do it for Webpack even with a plugin like this: https://github.com/webpack-contrib/compression-webpack-plugin

  3. Use a cache policy adjusted with your use case. Third party libraries joined in a different file with longer cache expiration time. Most commonly used libraries in another file with less time.

  4. Rethink the need for each library if it’s not worth implementing its own function or extracting it from the library instead of downloading the entire library altogether.

  • Very complete answer!! But it still hasn’t killed my main question (Or if killed it wasn’t very clear hehe)... The final file, the 10Mb Bundle.JS, how does the browser handle these 10Mb of javascript to download in a request? Isn’t it better to make several small requests? In this case the union that Webpack makes would be somewhat useless, no? Thank you!

  • 1

    It will be downloaded at once. The case is that using the HTTP 1 protocol, file requests are very expensive. Hence the idea of merging files. Then the confusion starts, there are people uniting jQuery, angular as project files and would have a much bigger gain with CDN. Webpack starts from this but goes much further! You can join only the project files (which would not benefit from a CDN). You can use Code Splitting, plugins and many more.

  • 1

    In the case of the HTTP 2 protocol, file requests are not expensive because the connection to the server is open eliminating the cost of the request round trip. You can load file by file at the same (+-) final cost as a bundled file, with the advantage that next requests will only bring files newer than the ones in the cache.

  • Perfect guy. So 1 Huge Bundle would give the same performance as various requirements in HTTP2? Recommend me some article on that? Thanks =)

  • 1

    https://developers.google.com/web/fundamentals/performance/http2/? hl=en

  • Perfect guy, thank you so much :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.