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)
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).
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.
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.
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.
Webpack automatically disables tree-shaking for shared packages.
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,
},
},
},
},
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With