Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

TypeScript - compile all ts files to one js

0 contributions
9 points

In TypeScript it is possible to compile all *.ts files to one *.js file in following way.

Download all source code from one place here.

1. outFile property example

tsconfig.json file:

{
    "version" : "3.0.3",
    "compilerOptions" : {
        "module" : "amd",
        "removeComments" : true,
        "outFile" : "./out.js",
        "baseUrl" : "./",
        "sourceMap" : true,
        "alwaysStrict" : true,
        "declaration" : true,
    },
    "include": [
        "./**/*.ts"
    ],
    "exclude": [
        "./out.d.ts"
    ]
}

Note: "outFile" : "./out.js" defines single source code output file.

 

Person.ts file:

export class Person
{
	public constructor(public name : string, public age : number)
	{
		// nothing here ...
	}
	
	public toString() : string
	{
		return `{ Name: ${this->name}, Age: ${this->age} }`;
	}
}

Printer.ts file:

import { Person } from "./Person";

export class Printer
{
	public static printPerson(person : Person) : void
	{
		console.log(person.toString());
	}
	
	public static printPersons(persons : Array<Person>) : void
	{
		for(let entry of persons)
			this.printPerson(entry);
	}
}

Main.ts file:

import { Person } from "./Person";
import { Printer } from "./Printer";

let persons = new Array<Person>();

persons.push(new Person('John', 30));
persons.push(new Person('Adam', 23));
persons.push(new Person('Kate', 41));

Printer.printPersons(persons);

index.htm file:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Example</title>
  <meta name="description" content="Example typescript files compilation to single javascript file.">
  <meta name="author" content="dirask.com">
  <!--
  <script src="https://requirejs.org/docs/release/2.3.6/minified/require.js"></script>
  -->
  <script src="https://requirejs.org/docs/release/2.3.6/comments/require.js"></script>
  <script src="out.js"></script>
</head>
<body>
  <script>
  
    require( [ 'Main' ] );
  
  </script>
</body>
</html>

Note: above source code uses https://requirejs.org/ modules loader.

 

Compilation and running:

  1. Open console (e.g. Bash)
  2. Go to project directory
  3. Run tsc program to compile all files to one
  4. Open index.htm in web browser

 

Bash console preview:

$ cd /C/Project
$ tsc

 

/C/Project directory preview:

Notes:

  • after tsc command run three new files (out.*) should appear.
  • out.d.ts contains all declarations (useful for intellisense and itengration out.js file with other typescript projects)
  • out.js.map contans development mapping for debug process
  • out.js contans all sourcecode transpiled to single javascript file

 

Web browser preview: 

 

Download all source code from one place here.

2. Troubleshooting

3. References

  1. RequireJS - Home Page
  2. tsconfig.json - TypeScript Docs

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more