Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree shaking of shared dependencies in webpack 5 module federation

I am working on an architecture for a dynamic dashboard with components fetched from different remote react bundles using webpack 5 module federation. I do have different libraries which are shared across some of these remote bundles. These packages are tree shakable. So each remote bundle will be having different codes from the same package. If I share these packages as singleton, when two components with same dependency loads to DOM in runtime, is there anyway webpack can get the lib code from both bundles merged? Or is it necessary that we have to disable tree shaking in such shared libraries? (By shared libraries I meant the npm packages)

like image 646
jn_dev Avatar asked Feb 18 '21 05:02

jn_dev


People also ask

Does webpack have tree shaking?

In webpack, tree shaking works with both ECMAScript modules (ESM) and CommonJS, but it does not work with Asynchronous Module Definition (AMD) or Universal Module Definition (UMD).

How does tree shake work in webpack?

Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module syntax, i.e. import and export . The name and concept have been popularized by the ES2015 module bundler rollup.

What is webpack 5 module federation?

Webpack 5 offers the Module Federation Plugin that lets you create multiple separate builds without dependencies between each other so they can be developed and deployed individually.

Does webpack tree shake CSS?

By default Webpack side effects property is set to false; which doesn't enable the tree shaking technique. By flipping the switch and turning the value into true, it will enable the tree shaking technique, and reduce your CSS file size by removing unused code. Smaller CSS files means better performance.


2 Answers

Webpack automatically disables tree-shaking for shared packages.

like image 154
Xiaofeng Xie Avatar answered Oct 19 '22 09:10

Xiaofeng Xie


Without being able to see exactly what you want to do I'm not exactly sure if this completely answers your question, but might be useful to the situation. You can get more fine tuned control of bundles with modules.exports optimization. You can get pretty granular here. A fontawesome example is at the top of the code snippet along with the optimization settings

// Import within node app

  if ($('.fad').length) {
    import('../../node_modules/@fortawesome/fontawesome-pro/scss/duotone.scss');
  }

// Webpack 

modules.exports {
  optimization: {
    splitChunks : {
      chuncks: 'all',
      cacheGroups: {
      duotonecss: {
      test    : /[\\/]node_modules[\\/]@fortawesome[\\/]fontawesome-pro[\\/]scss[\\/](duotone)\.scss/,
      name    : 'duotonecss',
      chunks  : 'all',
      enforce : true,
       },
     },
    },
  },
};
like image 22
Trevor Sutherland Avatar answered Oct 19 '22 10:10

Trevor Sutherland