question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack build does not remove dead code with SWC as compiler in default webpack config

See original GitHub issue

Current Behavior

Importing a react-icon causes the entire module to be included in the resulting webpack bundle, not just the one icon.

// packages/bundle-test/src/app/app.tsx
import { MdPerson } from "react-icons/md"

export function App() {
  return (
    <MdPerson size={128} />
  );
}
export default App;

Running webpack analyze shows the file size of bundled output is excessive image

Inspecting dist/main.hash.esm.js we can see the entire module has been imported.

Expected Behavior

Importing a react-icon should not include the entire module from react-icons/md, only the imported icon. Inspecting dist/main.hash.esm.js should show that only the one icon has been imported. In the reproducible repo below, I have been able to produce a fix that outputs this analysis, significantly reducing the size footprint:

image

Steps to Reproduce

A minimal repo has been made by @rudfoss and a branch attempting to fix this has been made by me @espenja over at https://github.com/rudfoss/nx-bundling-issue/tree/minimizer-fix

Editing the code in ./webpack.custom.ts to the below code, or just using the default "@nrwl/react/plugins/webpack" as webpackConfig, reproduces the issue reported in this ticket:

//./webpack.custom.ts
/* eslint-disable @typescript-eslint/no-var-requires */
import { Configuration, ids } from "webpack"
const getWebpackConfig = require("@nrwl/react/plugins/webpack.js")

module.exports = (config: Configuration) => {
    getWebpackConfig(config)

    // if (config.optimization) {
    //     delete config.optimization.minimizer
    // }

    // if (config.plugins) {
    //     config.plugins.push(new ids.HashedModuleIdsPlugin({
    //         hashFunction: 'sha256',
    //         hashDigest: 'hex',
    //         hashDigestLength: 4,
    //     }))
    // }
    return config
};

By default from nx, config.optimization.minimizer contains an instance of new webpack.ids.HashedModuleIdsPlugin() By removing the default config.optimization.minimizer setting, the bundle produces expected results. Therefore I am left wondering if the HashedModuleIdsPlugin is inserted at the wrong place by nx? Should it not instead have been inserted in config.plugins as per webpacks own test files found here https://github.com/webpack/webpack/blob/c181294865dca01b28e6e316636fef5f2aad4eb6/test/configCases/hash-length/hashed-module-ids/webpack.config.js ?

Enabling my fix again to the below code should produce a significantly lower size footprint in the webpack output.

//./webpack.custom.ts
/* eslint-disable @typescript-eslint/no-var-requires */
import { Configuration, ids } from "webpack"
const getWebpackConfig = require("@nrwl/react/plugins/webpack.js")

module.exports = (config: Configuration) => {
    getWebpackConfig(config)

    if (config.optimization) {
        delete config.optimization.minimizer
    }

    if (config.plugins) {
        config.plugins.push(new ids.HashedModuleIdsPlugin({
            hashFunction: 'sha256',
            hashDigest: 'hex',
            hashDigestLength: 4,
        }))
    }
    return config
};

We are using swc as compiler.

Failure Logs

N/A

Environment

Node : 16.14.2 OS : win32 x64 npm : 8.5.0

nx : 14.1.9-beta.1 @nrwl/angular : Not Found @nrwl/cypress : 14.1.9-beta.1 @nrwl/detox : Not Found @nrwl/devkit : 14.1.9-beta.1 @nrwl/eslint-plugin-nx : 14.1.9-beta.1 @nrwl/express : Not Found @nrwl/jest : 14.1.9-beta.1 @nrwl/js : 14.1.9-beta.1 @nrwl/linter : 14.1.9-beta.1 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/nx-cloud : Not Found @nrwl/nx-plugin : Not Found @nrwl/react : 14.1.9-beta.1 @nrwl/react-native : Not Found @nrwl/schematics : Not Found @nrwl/storybook : 14.1.9-beta.1 @nrwl/web : 14.1.9-beta.1 @nrwl/workspace : 14.1.9-beta.1 typescript : 4.6.4

Community plugins:

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
espenjacommented, Oct 3, 2022

@AgentEnder Would it be possible to re-open this issue and take another look? I have provided more information and an updated reproduction repository after migrating to the newest version of nx and verifying that it still does not work. This is specifically for swc as compiler.

0reactions
KraKeN-47commented, Oct 25, 2022

As of nx v14.8.4 and v15 , newly built @nrwl/react default app bundle size differs very much between babel and swc options, both examples ran with default serve:production scripts and analyzed with webpack-bundle-analyzer:

Babel:

image

Swc:

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

TerserWebpackPlugin | webpack
This plugin uses terser to minify/minimize your JavaScript. Getting Started. Webpack v5 comes with the latest terser-webpack-plugin out of the box. If you...
Read more >
How to prevent unused code from being dropped during ...
When I run webpack, a index.bundle.min.js file is created, but it is completely blank. For this simple example, I would expect it to...
Read more >
Minification - SWC
Starting with v1.2.67 , you can configure SWC to minify your code by ... If you set jsc.minify.compress to true or {} ,...
Read more >
Loading JavaScript - SurviveJS
Webpack processes ES2015 module definitions by default and transforms them into code. It does not t…
Read more >
Migrating to SWC: A brief overview - LogRocket Blog
SWC is faster than webpack and Babel. ... The script will automatically take the configuration and use SWC to compile your JavaScript code, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found