From c117d57d9a0d994b0dca32c6539ed58dbc0a2de1 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 13 Feb 2024 16:36:03 +0000 Subject: [PATCH 1/2] add custom_extensions to resolve options context --- crates/turbopack/src/resolve.rs | 4 ++- .../turbopack/src/resolve_options_context.rs | 2 ++ .../docs/features/customizing-turbopack.mdx | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/turbopack/src/resolve.rs b/crates/turbopack/src/resolve.rs index 73c58d9d3cc94..0b52dc4ee72f6 100644 --- a/crates/turbopack/src/resolve.rs +++ b/crates/turbopack/src/resolve.rs @@ -171,7 +171,9 @@ async fn base_resolve_options( conditions }; - let extensions = if let Some(environment) = emulating { + let extensions = if let Some(custom_extension) = &opt.custom_extensions { + custom_extension.clone() + } else if let Some(environment) = emulating { environment.resolve_extensions().await?.clone_value() } else { let mut ext = Vec::new(); diff --git a/crates/turbopack/src/resolve_options_context.rs b/crates/turbopack/src/resolve_options_context.rs index 7c2cc25a941ce..822b79f8cc4dc 100644 --- a/crates/turbopack/src/resolve_options_context.rs +++ b/crates/turbopack/src/resolve_options_context.rs @@ -47,6 +47,8 @@ pub struct ResolveOptionsContext { #[serde(default)] pub custom_conditions: Vec, #[serde(default)] + pub custom_extensions: Option>, + #[serde(default)] /// An additional import map to use when resolving modules. /// /// If set, this import map will be applied to `ResolveOption::import_map`. diff --git a/docs/pages/pack/docs/features/customizing-turbopack.mdx b/docs/pages/pack/docs/features/customizing-turbopack.mdx index 398c0f5c640ef..c1863999685c6 100644 --- a/docs/pages/pack/docs/features/customizing-turbopack.mdx +++ b/docs/pages/pack/docs/features/customizing-turbopack.mdx @@ -113,4 +113,30 @@ module.exports = { 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`. +## Resolve Extensions + +Through `next.config.js`, Turbopack can be configured to resolve modules with custom extensions, similar to webpack's [`resolve.extensions`](https://webpack.js.org/configuration/resolve/#resolveextensions) configuration. + +To configure resolve extension, use the `resolveExtensions` field in `next.config.js`: + +```js filename="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. + Turbopack also supports conditional aliasing through this field, similar to Node.js's [conditional exports](https://nodejs.org/docs/latest-v18.x/api/packages.html#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. From f4f0a59dc7b21238c037599c344ea45d994d3d9f Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 14 Feb 2024 10:47:49 +0100 Subject: [PATCH 2/2] Update docs/pages/pack/docs/features/customizing-turbopack.mdx Co-authored-by: Will Binns-Smith --- docs/pages/pack/docs/features/customizing-turbopack.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pages/pack/docs/features/customizing-turbopack.mdx b/docs/pages/pack/docs/features/customizing-turbopack.mdx index c1863999685c6..efb66afa0d007 100644 --- a/docs/pages/pack/docs/features/customizing-turbopack.mdx +++ b/docs/pages/pack/docs/features/customizing-turbopack.mdx @@ -137,6 +137,6 @@ module.exports = { } ``` -This overwrites the original resolve extensions with the provided list. Make sure to include the default extensions. +This overwrites the original resolve extensions with the provided list. Make sure to include the default extensions listed in the above config. Turbopack also supports conditional aliasing through this field, similar to Node.js's [conditional exports](https://nodejs.org/docs/latest-v18.x/api/packages.html#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.