DEV Community

Cover image for Keep your Javascript dependencies under control
Leonid Svyatov
Leonid Svyatov

Posted on

4 1

Keep your Javascript dependencies under control

With all the Javascript libraries and their dependencies these days it’s easy to get carried away and lost track of how heavy your frontend is.

Here is a great tool for Webpack backed applications: Webpack Bundle Analyzer. It shows the exact size (parsed and gzipped included) of each dependency you put inside your package.json file.

And here is an example of how to add it to your Rails project.

First, we need to add it to the package.json:

# Save the package to your `devDependencies`
yarn add -D webpack-bundle-analyzer
Enter fullscreen mode Exit fullscreen mode

Second, add this to your config/webpack/environment.js file, right before module.exports = environment; line:

if (process.env.ANALYZE_BUNDLE) {
  const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  environment.plugins.append('BundleAnalyzer', new BundleAnalyzerPlugin());
}
Enter fullscreen mode Exit fullscreen mode

Third, make it available as yarn/npm run task:

// package.json
{
  // ...
  "scripts": {
    // ...
    "analyze": "ANALYZE_BUNDLE=true RAILS_ENV=production ./bin/webpack"
  },
  // ...
}
Enter fullscreen mode Exit fullscreen mode

Done. You can run it like this now:

yarn run analyze
Enter fullscreen mode Exit fullscreen mode

Happy keeping your Javascript dependencies under control! :)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (1)

Collapse
 
joelnet profile image
JavaScript Joel

+1 for recommending webpack-bundle-analyzer. This has become part of every build we have. It is critical for any front end app when size is a factor.

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay