Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the use of es6 named imports reduce the bundle size when using webpack

I need to understand when I use a named import like this

import { render } from 'react-dom'

does webpack include in the bundle only the render method or the whole module especially when using tree shaking by setting module to false in babel configuration and let webpack take care about them?

Also in the case of importing react

import React from 'react'

&&

import React, { Component, PropTypes } from 'react'

what's the right way?

like image 669
zied hajsalah Avatar asked Mar 31 '17 17:03

zied hajsalah


People also ask

Does webpack support ES6?

It's extremely confusing because webpack itself DOES support ES6 module syntax! But in webpack. config you still have to use require . It seems overkill to install babel JUST for the webpack config file!

How do I reduce React bundle size?

One of the most impactful techniques to reduce the bundle size of a React application is compression. compression is a process in which the size of a file is reduced by re-encoding the file data to use fewer bits of storage than the original file.

How does webpack import work?

When webpack processes your application, it internally builds a dependency graph from one or more entry points and then combines every module your project needs into one or more bundles, which are static assets to serve your content from.


1 Answers

Tree-Shaking is applicable for modules which can be statically analysed (to get the entire dependency tree without running the code) - and it is ONLY for ES2015 Modules and NOT CommonJS(node) modules.

react, react-dom, as of this writing ([email protected]), are NOT published as ES2015 modules. So either of these -

import { render } from "react-dom";

or

import ReactDOM from "react-dom";

will result in the entire react-dom being included in your bundle. The same applies for react and other libraries which are published as CommonJS modules or UMD.

like image 79
Boopathi Rajaa Avatar answered Oct 03 '22 09:10

Boopathi Rajaa