Production build tree shaking not working
See original GitHub issueTree shaking does not work. I created a React app with both Nx and create-react-app
, and imported a single lodash function.
Current Behavior
Nx bundle:
Expected Behavior
Create react app bundle:
Steps to Reproduce
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import styles from './app.module.scss';
import NxWelcome from './nx-welcome';
import { get } from 'lodash-es';
export function App() {
console.log(get({ foo: true }, 'foo'));
return (
<>
<NxWelcome title="lodash" />
<div />
</>
);
}
export default App;
Environment
> NX Report complete - copy this into the issue template
Node : 16.13.2
OS : darwin x64
npm : 8.1.2
nx : 13.9.7
@nrwl/angular : Not Found
@nrwl/cypress : 13.9.7
@nrwl/detox : Not Found
@nrwl/devkit : 13.9.7
@nrwl/eslint-plugin-nx : 13.9.7
@nrwl/express : 13.9.7
@nrwl/jest : 13.9.7
@nrwl/js : 13.9.7
@nrwl/linter : 13.9.7
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : 13.9.7
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : 13.9.7
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : 13.9.7
@nrwl/web : 13.9.7
@nrwl/workspace : 13.9.7
typescript : 4.4.4
rxjs : 7.5.4
---------------------------------------
Community plugins:
@nx-tools/nx-docker: 2.3.0
Issue Analytics
- State:
- Created a year ago
- Comments:11
Top Results From Across the Web
Webpack Tree Shaking not working when importing from a file ...
If I remove the moment import from module.js tree shaking works fine and i only see square in my bundle. Is this how...
Read more >Tree Shaking - 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...
Read more >Tree shaking and code splitting in webpack - LogRocket Blog
What is tree shaking? Tree shaking, also known as dead code elimination, is the practice of removing unused code in your production build....
Read more >How I fixed webpack tree shaking in 3 easy steps
Make sure webpack is set to mode: production . This turns on a bunch of internal flags to make it all work. Step...
Read more >Tree-Shaking Problems with Component Libraries - Medium
Tree -shaking isn't magic, and much like the garbage collector, ... and you're producing an ES Module build (which you should!), don't bundle ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A full solution that worked for me. Nx version: 13.10.2
apps/<app>/project.json
apps/<app>/custom-webpack.config.js
package.json
If your app has side effects, for example, global styles, you need to specify an array of effectful files in the
package.json
:I didn’t have problems with Jest or
"target": "es2015"
in thetsconfig.base.json
file as described in the comment above.https://webpack.js.org/configuration/optimization/#optimizationsideeffects
By logging out the webpack configs that nx is using, I see it has
optimization: { sideEffects: false }
. Is there a reason this is turned off?Additionally, Nx has my tsconfig.json set to
"target": "es2015",
.I fully expect both of these issues to prevent tree shaking.
After I fixed both of these issues, I can confirm Nx properly tree shakes 👍 . All I did was:
(see https://nx.dev/guides/customize-webpack)
and in
tsconfig.base.json
I set"target": "esnext",
😃In turn these changes break Jest, to fix this I added
jest.preset.js:
babel.config.json:
And now jest works again
After making these changes and switching branches
nx build
also broke with this:After
rm -fr node_modules/.cache
this error was also resolved, and is tracked in a separate bug I reported here https://github.com/nrwl/nx/issues/9662