Typescript Compile to Single File

Typescript compile to single file

This will be implemented in TypeScript 1.8. With that version the outFile option works when module is amd or system.

At this time the feature is available in the development version of Typescript.
To install that run:

$ npm install -g typescript@next

For previous versions even if it's not obvious the module and the outFile options can not work together.

You can check this issue for more details.


If you want to output a single file with versions lower than 1.8 you can not use the module option in tsconfig.json. Instead you have to make namespaces using the module keyword.

Your tsconfig.json file should look like this:

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

Also your TS files should look like this:

module SomeModule {
export class RaceTrack {
constructor(private host: Element) {
host.appendChild(document.createElement("canvas"));
}
}
}

And instead of using the import statement you'll have to refer to the imports by namespace.

window.addEventListener("load", (ev: Event) => {
var racetrack = new SomeModule.RaceTrack(document.getElementById("content"));
});

How do I compile a single file in Typescript?

Using --skipLibCheck will suppress the errors, not fix them. It's okay in the short run (just to get you going while you deal with the root cause) but since it opts out from library type-checking entirely, it's really a footgun.

For TypeScript to scan a declaration file and include it in the compilation, it must be either something you depend on explicitly (you import or require in your project) or implicitly (it's added to types it in your tsconfig.json). Often times it's not exactly your dependency, but a dependency of your dependency. For real type-safety, everything in the tree must be in order. Sometimes it's just because the provided typings are imperfect, and need fixing.

You can start with updating @types/selenium-webdriver and updating your dependencies in general. Try running npx npm-check --update to see what needs updating.

If that doesn't help, you can write your type declarations for the library in question (and either send them to DefinitelyTyped or keep them in your project).

TypeScript compile import's into a single file

TypeScript compile import's into a single file

You need a module bundler. e.g. Webpack or browserify.

More

Browser quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

How to compile a specific file with tsc using the paths compiler option

Specifying the input files on the command line voids your tsconfig.json.

When input files are specified on the command line, tsconfig.json files are ignored.

From the TypeScript docs.

So, there's no other option than to use a tsconfig.json with the proper 'include' (or exclude) specification of the files. The good news is that in the same docs you will find:

A tsconfig.json file can inherit configurations from another file using the extends property.

So what you can do is overwrite the include property of your tsconfig with something like this:

{
"extends": "./tsconfig.json",
"include": [
"src/root/index.ts"
]
}

So you could make a script that:

  1. Creates a temporary 'extends tsconfig' file with the desired 'include' property
  2. Calls tsc with --project specified as that temporary tsconfig

One could argue that this is very complicated and question why they made the decision to not support this scenario properly. However, the question remains why one would like to compile just one file of a project. While developing, you can use the watch (-w) option to compile changes files, and upon building a full project you'll probably want to compile exactly that, the full project.

TypeScript compiling as a single JS file

You have to use command line arguments of compiler

--outFile FILE Concatenate and emit output to single file

example

 tsc --outFile modules.js main.ts app.ts


Related Topics



Leave a reply



Submit