Skip to content

Turbopack: add extension alias config option to match alternate file extensions#95426

Open
mischnic wants to merge 4 commits into
canaryfrom
imm/extension-alias2
Open

Turbopack: add extension alias config option to match alternate file extensions#95426
mischnic wants to merge 4 commits into
canaryfrom
imm/extension-alias2

Conversation

@mischnic

@mischnic mischnic commented Jul 2, 2026

Copy link
Copy Markdown
Member

Recration of #95261 , original description:

Fixes #82945
Closes PACK-5449

Adds a config option that can resolve extensions with other optional extensions, matching the behavior of Webpack's resolve.extensionAlias:

module.exports = {
  turbopack: {
    resolveExtensionAlias: {
      // checks for .ts first, then .tsx, and finally .js
      '.js': ['.ts', '.tsx', '.js'],
      // checks for .mts first, then .mjs
      '.mjs': ['.mts', '.mjs'],
    },
  },
}

TypeScript with ESM sets the expectation that you don't transform the import, so it wants to import from the compiled .js file. This breaks down in Next if you have code that's shared between a Node.js server and your Next app.

Most of the pieces were already in place because of other direct aliasing options for Turbopack (resolveAlias). I did have to match 3 different prefixes ("", "./", "../") to cover all potential paths. This could be considered leaking internal implementation details from ImportMap::lookup...

There's a new E2E test exercising this functionality.

I've looked at this a few different ways for perf concerns. Yes we're adding some wildcard matching, but I think the only new computation is at config/setup time, in a hopefully-cached turbo task.

Finally, I have identified one limitation: When the option is used, some dynamic imports with >4 sections will be too complex to match. This is already an issue with existing alias code. It will trigger a hard error with a "Complex patterns into wildcard exports fields are not implemented" message. Since this doesn't add errors if the option isn't leveraged, we should consider

  1. finding a way to surface a better error message in alias-specific code
  2. documenting the limitations / error in the alias option docs

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: f627c55

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Stats cancelled

Commit: f627c55
View workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Turbopack: support importing .ts/.tsx via .js extension (parity with webpack resolve.extensionAlias)

2 participants