Turbopack can be customized to transform different files and change how modules are resolved.
Good to know:
- These features are experimental and will only work with
next --turbo.- Turbopack for Next.js does not require loaders nor loader configuration for built-in functionality. Turbopack has built-in support for css and compiling modern JavaScript, so there's no need for
css-loader,postcss-loader, orbabel-loaderif you're using@babel/preset-env.
If you need loader support beyond what's built in, many webpack loaders already work with Turbopack. There are currently some limitations:
require()d plugin modules as option values.To configure loaders, add the names of the loaders you've installed and any options in next.config.js, mapping file extensions to a list of loaders:
module.exports = {
experimental: {
turbo: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
},
}Good to know: Prior to Next.js version 13.4.4,
experimental.turbo.ruleswas namedexperimental.turbo.loadersand only accepted file extensions like.mdxinstead of*.mdx.
The following loaders have been tested to work with Turbopack's webpack loader implementation:
babel-loader@svgr/webpacksvg-inline-loaderyaml-loaderstring-replace-loaderraw-loaderThrough next.config.js, Turbopack can be configured to modify module resolution through aliases, similar to webpack's resolve.alias configuration.
To configure resolve aliases, map imported patterns to their new destination in next.config.js:
module.exports = {
experimental: {
turbo: {
resolveAlias: {
underscore: 'lodash',
mocha: { browser: 'mocha/browser-entry.js' },
},
},
},
}This aliases imports of the underscore package to the lodash package. In other words, import underscore from 'underscore' will load the lodash module instead of underscore.
Turbopack also supports conditional aliasing through this field, similar to Node.js's conditional exports. At the moment only the browser condition is supported. In the case above, imports of the mocha module will be aliased to mocha/browser-entry.js when Turbopack targets browser environments.
Through next.config.js, Turbopack can be configured to resolve modules with custom extensions, similar to webpack's resolve.extensions configuration.
To configure resolve extensions, use the resolveExtensions field in next.config.js:
module.exports = {
experimental: {
turbo: {
resolveExtensions: [
'.mdx',
'.tsx',
'.ts',
'.jsx',
'.js',
'.mjs',
'.json',
],
},
},
}This overwrites the original resolve extensions with the provided list. Make sure to include the default extensions.
For more information and guidance for how to migrate your app to Turbopack from webpack, see Turbopack's documentation on webpack compatibility.
© 2024 Vercel, Inc.
Licensed under the MIT License.
https://nextjs.org/docs/app/api-reference/next-config-js/turbo