Skip to content

Add support for custom module resolution conditions in tsconfig.jsonΒ #53213

Closed

Description

@pilaoda

Suggestion

πŸ” Search Terms

package.json exports condition

βœ… Viability Checklist

My suggestion meets these guidelines:

  • [√] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [√] This wouldn't change the runtime behavior of existing JavaScript code
  • [√] This could be implemented without emitting different JS based on the types of the expressions
  • [√] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [√] This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

New config in tsconfig.json about custom module resolution conditions: https://nodejs.org/api/packages.html#community-conditions-definitions.

πŸ“ƒ Motivating Example

Sharing same typescript source codes and generating outputs for different targets in monorepo.

πŸ’» Use Cases

My monorepo:

- packages
    - my-lib
        - src
        - dist
            - dev
            - prod
        - package.json
        - tsconfig.dev.json
        - tsconfig.prod.json
    - my-app
        - src
        - dist
            - dev
            - prod
        - package.json
        - tsconfig.dev.json
        - tsconfig.prod.json

packages/my-lib/tsconfig.dev.json:

{
       "compilerOptions": {
		"rootDir": "./src",
		"outDir": "./dist/dev",
	},
}

packages/my-lib/tsconfig.prod.json:

{
       "compilerOptions": {
		"rootDir": "./src",
		"outDir": "./dist/prod",
	},
}

packages/my-lib/package.json:

{
    "exports": {
        "dev": "./dist/dev/index.d.ts",
        "prod": "./dist/prod/index.d.ts",
    },
}

packages/my-app/tsconfig.dev.json:

{
       "compilerOptions": {
		"rootDir": "./src",
		"outDir": "./dist/dev",
                "module": "commonjs",
                "moduleResolution": "node16",
                "moduleResolutionConditions": ["dev"],  // feature request
	},
	"references": [
		{ "path": "../my-lib/tsconfig.dev.json }
	],
}

packages/my-app/tsconfig.prod.json:

{
       "compilerOptions": {
		"rootDir": "./src",
		"outDir": "./dist/prod",
		"module": "commonjs",
                "moduleResolution": "node16",
                "moduleResolutionConditions": ["prod"],  // feature request
	},
        "references": [
		{ "path": "../my-lib/tsconfig.prod.json }
	],
}

Switch target between "dev" and "prod" may be an unnecessary demand, but switch between something like "server" and "client", or "node" and "dom" may be impossible without this feature.

Activity

  1. pilaoda commented on Mar 12, 2023

    @pilaoda
    Author

    https://webpack.js.org/guides/package-exports/#conditions-custom
    Since packages like webpack already support such custom conditions, I think there is no reason that typescript should abandon this feature.

  2. npenin commented on Apr 4, 2023

    @npenin

    Even though it is hard to find in the doc, the feature already exists: https://www.typescriptlang.org/tsconfig#customConditions

  3. pilaoda commented on Apr 4, 2023

    @pilaoda
    Author

    Great job! I guess this issue is created on version 4.9 which does't support this feature. Any way, Thanks!

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions