Utilizing Tree-Shaking with Redux v5.0.0
Illustrate how tree-shaking can be applied in a Webpack build configuration for a Redux-enabled application.
import { createStore, combineReducers } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { todosReducer } from './reducers/todosReducer';
import { userReducer } from './reducers/userReducer';
// Combine reducers
const rootReducer = combineReducers({
todos: todosReducer,
user: userReducer
});
// Create store with only the necessary reducers
const store = createStore(rootReducer, composeWithDevTools());
export default store;
This snippet imports only the required reducers from redux. Combining them with combineReducers ensures that only the code relevant to these reducers will be included in the final bundle, primed for tree-shaking.
const webpackConfig = {
// ... other webpack configuration settings ...
optimization: {
usedExports: true, // Enable tree-shaking by marking unused exports
sideEffects: false // Enable package.json sideEffects flag usage
}
// ... rest of webpack configuration ...
};
module.exports = webpackConfig;
In this Webpack configuration snippet, the 'optimization' object's properties 'usedExports' and 'sideEffects' are set to help Webpack in identifying and removing unused code from the final bundle.