After a lot of research here is what I have found to solve this problem;
- Put this method above your module.exports in webpack.config
function externalForMaterialUi(context, request, callback) {
if (/@material-ui.+/.test(request)) {
const name = request.replace(/^.*[\\\/]/, '')
return callback(null, 'root MaterialUI.' + name);
}
callback();
}
And after you should call the method from your webpack externals section;
externals: [
externalForMaterialUi,
]
with this way all the packages of material-ui from your bundle will be excluded.
Another way of doing this is including all the material packages one by one inside externals section;
externals: [
{
'@material-ui/types': {
root: 'MaterialUI',
commonjs2: 'material-ui',
commonjs: 'material-ui',
amd: 'MaterialUI',
umd: 'MaterialUI',
},
'@material-ui/pickers': {
root: 'MaterialUI',
commonjs2: 'material-ui',
commonjs: 'material-ui',
amd: 'MaterialUI',
umd: 'MaterialUI',
},
'@material-ui/core': {
root: 'MaterialUI',
commonjs2: 'material-ui',
commonjs: 'material-ui',
amd: 'MaterialUI',
umd: 'MaterialUI',
},
'@material-ui/lab': {
root: 'MaterialUI',
commonjs2: 'material-ui',
commonjs: 'material-ui',
amd: 'MaterialUI',
umd: 'MaterialUI',
},
},
],