How to compile to a single file with TypeScript?

Sometimes, we want to compile to a single file with TypeScript.

In this article, we’ll look at how to compile to a single file with TypeScript.

How to compile to a single file with TypeScript?

To compile to a single file with TypeScript, we remove the module option and add the outFile option in tsconfig.json.

For instance, we write

{
  "compilerOptions": {
    "target": "ES5",
    "removeComments": true,
    "preserveConstEnums": true,
    "outFile": "./build/build.js",
    "sourceRoot": "./src/",
    "rootDir": "./src/",
    "sourceMap": true
  }
}

in tsconfig.json.

We set outFile to ./build/build.js so that the TypeScript compiler will compile all the code into that file.

We remove the module option so that the code will be compiled into one file.

Conclusion

To compile to a single file with TypeScript, we remove the module option and add the outFile option in tsconfig.json.