2

I break my TypeScript apps into multiple files, usually one for each class. When I release my app I concat and minify them into a single file. The problem I have is that the TypeScript generatred __extends function gets put into each compiled JavaScript file. So I end up with multiple copies of the same code in the final product.

Is there a way to tell the compiler to only put the __extends function in one JavaScript file? Is there some other solution?

1
  • It shouldn't matter to have multiple copies in your end file because your gzipped size should not be affected since the code fragments are exactly the same. But of course, if you don't serve your scripts gzipped (why not?), then you probably have to global search/replace the code fragments and include the small sniplet in a separate file at the top of the concatnation. Commented Nov 22, 2013 at 4:26

2 Answers 2

5

One solution would be to combine the output into one file via the TypeScript compiler:

tsc --out final.js app.ts

You could then minify final.js, which should only have one extends function.

Sign up to request clarification or add additional context in comments.

1 Comment

That's a good solution. If only there was a way to specify that in Visual Studio when compiling in release mode. I suppose for now I could create a release script instead.
4

The option to combine the output into file is great, but it isn't supported in all versions and all module versions.
In order to overcome it you can use the noEmitHelpers compiler option to remove the helper function and to write your own in a separate file.

You should create a new emitHelperFile.js that will contain the extend function.

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};

And omit it from the generated output of the compiler

tsc app.ts --noEmitHelpers 

You can check more options of the compiler here https://www.typescriptlang.org/docs/handbook/compiler-options.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.