Webpack bundle analysis

Adds webpack-bundle-analyzer to your Webpacker-enabled Rails application
Icons/chart bar
Used 73 times
Created by
R Ross Kaffenberger

Usage
Once installed, compile webpack with WEBPACK_ANALYZE=true to enable bundle analysis. For example:

To analyze the development build, run:

WEBPACK_ANALYZE=true RAILS_ENV=development NODE_ENV=development bin/webpack

To analyze the production build, run:

WEBPACK_ANALYZE=true RAILS_ENV=production NODE_ENV=production bin/webpack

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/Xo5sYr"
Template Source

Review the code before running this template on your machine.

run("yarn add --dev webpack-bundle-analyzer")

inject_into_file 'config/webpack/environment.js', after: "const { environment } = require('@rails/webpacker')\n" do <<~EOF
  
  if (process.env.WEBPACK_ANALYZE === "true") {
    const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
    environment.plugins.append('BundleAnalyzerPlugin', new BundleAnalyzerPlugin())
  }
  EOF
end

puts <<-EOF

USAGE

To analyze the development build, run:

WEBPACK_ANALYZE=true RAILS_ENV=development NODE_ENV=development bin/webpack

To analyze the production build, run:

WEBPACK_ANALYZE=true RAILS_ENV=production NODE_ENV=production bin/webpack
EOF
Comments

Sign up or Login to leave a comment.