Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default modularizeImports that breaks up @mui/icons-material, @mui/material, date-fns, lodash, lodash-es, ramda, react-bootstrap #50900

34 changes: 34 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,40 @@ function assignDefaults(
}
}

const userProvidedModularizeImports = result.modularizeImports
// Unfortunately these packages end up re-exporting 10600 modules, for example: https://unpkg.com/browse/@mui/[email protected]/esm/index.js.
// Leveraging modularizeImports tremendously reduces compile times for these.
result.modularizeImports = {
...(userProvidedModularizeImports || {}),
// This is intentionally added after the user-provided modularizeImports config.
'@mui/icons-material': {
transform: '@mui/icons-material/{{member}}',
},
'@mui/material': {
transform: '@mui/material/{{member}}',
},
Comment on lines +685 to +687
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. It was reported in #51872. I have open #51953 to fix this.

'date-fns': {
transform: 'date-fns/{{member}}',
},
lodash: {
transform: 'lodash/{{member}}',
},
'lodash-es': {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: what do you think about including lodash/fp ?

'lodash/fp': {
      transform: 'lodash/fp/{{member}}',
    },

transform: 'lodash-es/{{member}}',
},
// TODO: Enable this once we have a way to remove the "-icon" suffix from the import path.
// Related discussion: https://github.com/vercel/next.js/pull/50900#discussion_r1239656782
// 'lucide-react': {
// transform: 'lucide-react/dist/esm/icons/{{ kebabCase member }}',
// },
ramda: {
transform: 'ramda/es/{{member}}',
},
'react-bootstrap': {
transform: 'react-bootstrap/{{member}}',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change results in the following error

Module not found: Can't resolve 'react-bootstrap/useAccordionButton'
  3 | import { Inter } from 'next/font/google'
  4 | import styles from '@/styles/Home.module.css'
> 5 | import { useAccordionButton } from 'react-bootstrap'
  6 | 
  7 | const inter = Inter({ subsets: ['latin'] })
  8 | 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because some hooks do not have independent files, the above problems are caused.

If someone encounters the above issue when using react-bootstrap and receives the error message Module not found: Can’t resolve 'react-bootstrap/xxx',
you can solve the problem with the following configuration:

const nextConfig = {
  webpack: config => {
    let modularizeImports = null
    config.module.rules.some(rule => (
      rule.oneOf?.some(oneOf => {
        modularizeImports = oneOf?.use?.options?.nextConfig?.modularizeImports
        return modularizeImports
      })
    ))
    if (modularizeImports?.['react-bootstrap'])
      delete modularizeImports['react-bootstrap']
    return config
  }
}

},
}

return result
}

Expand Down