Skip to content

Conversation

andrewbranch
Copy link
Member

Prerequisite for #62200

This has worked in a coherent way since --module preserve was implemented, but was prohibited with a program error. --moduleResolution bundler (like every other modern TS resolution mode) resolves package.json "exports" conditions according to the output module syntax being emitted. So in --module commonjs, imports in .ts and .js files always resolve with the "require" condition set.

This combination of settings is very rarely going to be what people actually want, and is mostly intended as a "my code works, don't bother me" upgrade path for users on --module commonjs --moduleResolution: node. (More poignantly, it doesn't seem like deprecating --module commonjs is an option at this point, and with --moduleResolution node10 going away, that leaves no moduleResolution settings left that are legal with commonjs.) However, it's also the correct settings for anyone transpiling their code to CommonJS before resolving imports through a bundler. This includes Webpack ts-loader users who have --module commonjs in their ts-loader tsconfig. However, you should not use this combination of settings just because you set your bundler's output to emit a CommonJS module, so our docs / blog post need to be intentional about highlighting this.

@Copilot Copilot AI review requested due to automatic review settings August 22, 2025 17:54
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Aug 22, 2025
@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Aug 22, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR allows the combination of --module bundler with --moduleResolution commonjs, which was previously prohibited with a program error. This change supports users who transpile their TypeScript code to CommonJS before bundling, particularly those using tools like Webpack's ts-loader with CommonJS module compilation settings.

Key changes:

  • Removes the restriction preventing --moduleResolution bundler from being used with --module commonjs
  • Updates error messages to include "commonjs" as a valid module option with bundler resolution
  • Adds comprehensive test coverage for the new combination of settings

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/compiler/program.ts Updates validation logic to allow --module commonjs with --moduleResolution bundler
src/compiler/diagnosticMessages.json Updates error message text to include "commonjs" as a valid option
tests/cases/conformance/moduleResolution/bundler/bundlerCommonJS.ts New test file demonstrating CommonJS module resolution with bundler
tests/cases/conformance/moduleResolution/bundler/bundlerOptionsCompat.ts Updates existing test to use "nodenext" instead of "commonjs"
Multiple baseline files Updates test baselines to reflect the new allowed combination and updated error messages

@github-project-automation github-project-automation bot moved this from Not started to Needs merge in PR Backlog Aug 22, 2025
Comment on lines +34 to +35
// @Filename: /real-imports.mts
import { x } from "pkg"; // Error
Copy link
Member Author

Choose a reason for hiding this comment

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

As of #57896 (TS 5.6), .mts and .cts extensions override the --module setting. Since this import will emit as an import, and the package has only a require entrypoint, the resolution fails.

@andrewbranch andrewbranch merged commit e635bb9 into microsoft:main Aug 22, 2025
33 checks passed
@github-project-automation github-project-automation bot moved this from Needs merge to Done in PR Backlog Aug 22, 2025
@andrewbranch andrewbranch deleted the bundler-commonjs branch August 22, 2025 21:18
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 6.0 milestone Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants