这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/turbopack/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions crates/turbopack/src/resolve_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct ResolveOptionsContext {
#[serde(default)]
pub custom_conditions: Vec<String>,
#[serde(default)]
pub custom_extensions: Option<Vec<String>>,
#[serde(default)]
/// An additional import map to use when resolving modules.
///
/// If set, this import map will be applied to `ResolveOption::import_map`.
Expand Down
26 changes: 26 additions & 0 deletions docs/pages/pack/docs/features/customizing-turbopack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be a good idea to move functionality like this to a shared field that both webpack and turbopack read from

Copy link
Contributor

Choose a reason for hiding this comment

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

Either way, this needs to be added to the typescript types

Copy link
Contributor

Choose a reason for hiding this comment

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

oh right, this isn't the next.js repo yet, we should really move the docs over

Copy link
Contributor

Choose a reason for hiding this comment

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

The original comment still stands though, maybe this should be something like experimental.compiler.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 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.