Skip to content

Allow --sourceMap command line flag (and others) in build mode (--build) #45130

Closed
@BTOdell

Description

@BTOdell

Suggestion

Currently, the --sourceMap command line flag does not work when running in build mode.

D:\Programming\MyProject>tsc --build --sourceMap false
error TS5094: Compiler option '--sourceMap' may not be used with '--build'.

🔍 Search Terms

source map command line build mode tsconfig solution

✅ 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

Allow the --sourceMap command line flag to be used in build mode to act as a global override for the tsconfig file(s). So if any tsconfig project files enable source map generation, then setting this flag from the command line will disable it for the entire build (including referenced tsconfigs).

This type of override behavior could also be supported for other boolean command line flags, such as --skipLibCheck and --removeComments. Essentially, flags that affect the speed or output of file generation but not affecting language parsing or interpretation.

📃 Motivating Example

./tsconfig.base.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    ...
    "sourceMap": true
  }
}

./src/tsconfig.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "../dist"
  }
}
D:\Programming\MyProject>tsc --build --sourceMap false

Should override the "sourceMap" config in the tsconfig.base.json and never output source maps.

💻 Use Cases

When building a TypeScript project for release on the NPM registry, often you'll want to skip generating source maps unless you plan on including the source .ts files along with the .js.map files. If you simply omit including the .js.map files in your package, then tools like Webpack will complain that it can't find the source map files. If you try including the .js.map files in your package, then you'll start getting warnings from Webpack that it can't find the source .ts files. So you end up having to include the source code in your release packages on NPM. This results in bloated release download sizes.

A solution for those projects with single tsconfig file builds is to create a second tsconfig file that extends your main tsconfig file and sets the sourceMap config to false. However, this problem and suggestion isn't about projects with simple builds. It's about complex "solution-style" builds heavily utilizing project references where it's not feasible to have an auxiliary tsconfig file alongside each of your main tsconfig files.

If the option to globally override a configuration setting was available, then you could leave the sourceMap config set to true for development and debug, and in a "prepublishOnly" script in your package.json you could compile your code with source maps completely disabled for release publishing.

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