Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to overwrite __extends emitter? #2487

Closed
streamich opened this issue Mar 25, 2015 · 5 comments
Closed

How to overwrite __extends emitter? #2487

streamich opened this issue Mar 25, 2015 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@streamich
Copy link

Currently TypeScripts emits in every file something like this:

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

Which is only some 300 bytes, but it is in every file. If my web app has hundreds of files, this adds up.

Instead what is the easiest modification to do to make it emit the following in every file:

var __extends = Object.create;

Can I do something like this?

typescript.settings.emitter.extends = "var __extends = Object.create;"

And leave it up to me to make sure Object.create actually exists.

@DickvdBrink
Copy link
Contributor

I think this issue is covered by issue #2009
And this PR would have fixed it (I think) #1356 but it is closed.

@danquirk
Copy link
Member

Yeah, dupe. If I recall correctly the referenced PR ended up not being a sufficient fix and the full fix was more complex.

@danquirk danquirk added the Duplicate An existing issue was already created label Mar 25, 2015
@streamich
Copy link
Author

Well, this is kinda important. This code is emitted in every file. An average web app has some 1,000 files: 300 bytes x 1,000 files = 300Kb for every page view.

@streamich
Copy link
Author

Maybe you could consider a hot fix, like something below:

if(you_want_a_slow_huge_web_app_with_hundreds_of_redundant__extends_functions_that_will_never_execute) {
    writeLine();
    write("var __extends = this.__extends || function (d, b) {");
    increaseIndent();
    writeLine();
    write("for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];");
    writeLine();
    write("function __() { this.constructor = d; }");
    writeLine();
    write("__.prototype = b.prototype;");
    writeLine();
    write("d.prototype = new __();");
    decreaseIndent();
    writeLine();
    write("};");
} else {
    write("var __extends = " + some_very_short_global_var);
}

@RyanCavanaugh
Copy link
Member

This code is emitted in every file.

If you compile with --out, you're only going to get it once. If you don't have any derived classes in a file, that file will not have an __extends function.

An average web app has some 1,000 files: 300 bytes x 1,000 files = 300Kb for every request.

If you really believe this is a huge problem, please spend the 10 minutes to write a step in your build pipeline to run a simple regex against the generated .js. If you're really serving 1,000 JS files on every request, you need to re-examine your caching policies. If you're serving them over separate HTTP calls then you're spending way more than that amount of bandwidth in HTTP headers anyway and it you should be serving it as a single bundled file. Since 1,000 files worth of JS is going to be several megabytes, you're going to want to gzip it anyway, which means that repeated text is going to be highly compressed. This can't conceivably be the blocker you make it out to be.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants