GeezerVison is showing github.com — How to overwrite __extends emitter? · Issue #2487 · microsoft/TypeScript
How to overwrite __extends emitter? #2487DescriptionCurrently 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. ActivityDickvdBrink commented on Mar 25, 2015 I think this issue is covered by issue #2009
And this PR would have fixed it (I think) #1356 but it is closed. danquirk commented on Mar 25, 2015 Yeah, dupe. If I recall correctly the referenced PR ended up not being a sufficient fix and the full fix was more complex. streamich commented on Mar 25, 2015 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 commented on Mar 25, 2015 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 commented on Mar 25, 2015
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.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|