Unverified Commit 1ba41403 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

docs: `optimizePackageImports` (#55185)

Follow up from https://github.com/vercel/next.js/pull/54695.
parent 981e8fa9
---
title: optimizePackageImports
description:
---
{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
Some packages can export hundreds or thousands of modules, which can cause performance issues in development and production.
Adding a package to `optimizePackageImports` will only load the modules you are actually using, while still giving you the convenience of writing import statements with many named exports.
```js filename="next.config.js"
module.exports = {
optimizePackageImports: ['package-name'],
}
```
Libraries like `@mui/icons-material`, `@mui/material`, `date-fns`, `lodash`, `lodash-es`, `react-bootstrap`, `@headlessui/react`, `@heroicons/react`, and `lucide-react` are already optimized by default.
......@@ -254,159 +254,7 @@ module.exports = {
### Modularize Imports
<details open>
<summary>Examples</summary>
- [modularize-imports](https://github.com/vercel/next.js/blob/canary/examples/modularize-imports/)
</details>
Allows to modularize imports, similar to [babel-plugin-transform-imports](https://www.npmjs.com/package/babel-plugin-transform-imports).
Transforms member style imports of packages that use a “barrel file” (a single file that re-exports other modules):
```js
import { Row, Grid as MyGrid } from 'react-bootstrap'
import { merge } from 'lodash'
```
...into default style imports of each module. This prevents compilation of unused modules:
```js
import Row from 'react-bootstrap/Row'
import MyGrid from 'react-bootstrap/Grid'
import merge from 'lodash/merge'
```
Config for the above transform:
```js filename="next.config.js"
module.exports = {
modularizeImports: {
'react-bootstrap': {
transform: 'react-bootstrap/{{member}}',
},
lodash: {
transform: 'lodash/{{member}}',
},
},
}
```
#### Handlebars variables and helper functions
This transform uses [handlebars](https://docs.rs/handlebars) to template the replacement import path in the `transform` field. These variables and helper functions are available:
1. `member`: Has type `string`. The name of the member import.
2. `lowerCase`, `upperCase`, `camelCase`, `kebabCase`: Helper functions to convert a string to lower, upper, camel or kebab cases.
3. `matches`: Has type `string[]`. All groups matched by the regular expression. `matches.[0]` is the full match.
For example, you can use the `kebabCase` helper like this:
```js filename="next.config.js"
module.exports = {
modularizeImports: {
'my-library': {
transform: 'my-library/{{ kebabCase member }}',
},
},
}
```
The above config will transform your code as follows:
```js
// Before
import { MyModule } from 'my-library'
// After (`MyModule` was converted to `my-module`)
import MyModule from 'my-library/my-module'
```
You can also use regular expressions using Rust [regex](https://docs.rs/regex/latest/regex/) crate’s syntax:
```js filename="next.config.js"
module.exports = {
modularizeImports: {
'my-library/?(((\\w*)?/?)*)': {
transform: 'my-library/{{ matches.[1] }}/{{member}}',
},
},
}
```
The above config will transform your code as follows:
```js
// Before
import { MyModule } from 'my-library'
import { App } from 'my-library/components'
import { Header, Footer } from 'my-library/components/app'
// After
import MyModule from 'my-library/my-module'
import App from 'my-library/components/app'
import Header from 'my-library/components/app/header'
import Footer from 'my-library/components/app/footer'
```
#### Using named imports
By default, `modularizeImports` assumes that each module uses default exports. However, this may not always be the case — named exports may be used.
```ts filename="my-library/MyModule.ts"
// Using named export instead of default export
export const MyModule = {}
// my-library/index.ts
// The “barrel file” that re-exports `MyModule`
export { MyModule } from './MyModule'
```
In this case, you can use the `skipDefaultConversion` option to use named imports instead of default imports:
```js filename="next.config.js"
module.exports = {
modularizeImports: {
'my-library': {
transform: 'my-library/{{member}}',
skipDefaultConversion: true,
},
},
}
```
The above config will transform your code as follows:
```js
// Before
import { MyModule } from 'my-library'
// After (imports `MyModule` using named import)
import { MyModule } from 'my-library/MyModule'
```
#### Preventing full import
If you use the `preventFullImport` option, the compiler will throw an error if you import a “barrel file” using default import. If you use the following config:
```js filename="next.config.js"
module.exports = {
modularizeImports: {
lodash: {
transform: 'lodash/{{member}}',
preventFullImport: true,
},
},
}
```
The compiler will throw an error if you try to import the full `lodash` library (instead of using named imports):
```js
// Compiler error
import lodash from 'lodash'
```
This option has been superseded by [`optimizePackageImports`](/docs/app/api-reference/next-config-js/optimizePackageImports) in Next.js 13.5. We recommend upgrading to use the new option that does not require manual configuration of import paths.
## Experimental Features
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment