TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Suggestion: minification

Open RyanCavanaugh opened this issue 12 years ago • 193 comments

TypeScript should support emitting minified JavaScript.

There are several different things we could support:

  1. Just remove whitespace
  2. Minify unobservable identifiers
  3. Remove provably dead code
  4. Whole-program minification (i.e. closure compiler)
  5. (Others?)

RyanCavanaugh avatar Jul 15 '14 16:07 RyanCavanaugh

Yes, we COULD go download some additional third party software. But a lot of devs, like me, don't want to add yet another third party tool, don't want to learn another tool. We don't need it to be 10% faster than the alternative. We just want it to be there because it's simple, it saves us time, and it makes our lives a lot easier.

Your comment is perfect.

I don't like bloating my programs with libraries and extra steps. I like simplicity and development speed. Life is too short to learn and use tons of stuff to get something working that could be way simpler.

The main point here is that it could really be done easily, and by that i mean just removing the whitespaces, that would already be a very good functionality.

As I said before, it isn't needed to implement all the possible minifiers at once. Having in the tsconfig a "minify": true for the recommended available minifications and/or "minify": {"whitespace": true} would be perfect.

ftzi avatar Aug 22 '21 05:08 ftzi

Hi guys!

I made minimize-js to minimize my tsc build.

You can take a look at that. Usually, I launch it using the postbuild script.

Cheers! 🍺

Randagio13 avatar Nov 11 '21 19:11 Randagio13

so its been almost 10 years, and typescript devs refuse to add a simple minifier?

uzbeki avatar Apr 28 '22 23:04 uzbeki

so its been almost 10 years, and typescript devs refuse to add a simple minifier?

I assume nobody has any problem with that you @uzbeki just create a draft PR and we can start see how you implement this simple minifier. Please also maintain it the whole way in all future versions of TypeScript as this would then we a core part of TypeScript.

Alternatively just use external tools like esbuild and go on 🤷

Shinigami92 avatar Apr 29 '22 09:04 Shinigami92

It's an unnecessary overhead to use Webpack (although config can be made by copy-paste), but @Randagio13 idea with postbuild is quite simple and can be implemented in seconds by anyone, when needed without changes in TypeScript

michalzubkowicz avatar May 09 '22 18:05 michalzubkowicz

Your comment is perfect.

I don't like bloating my programs with libraries and extra steps. I like simplicity and development speed. Life is too short to learn and use tons of stuff to get something working that could be way simpler.

Not to mention, it'd make it much easier to get a basic level of minification AND working source maps when the project and the person/team knowledge are primarily some non-JS/TS server-side language like Rust.

(That's my biggest problem. I write stuff that sticks to progressive enhancement principles and don't want to spend all day trying out various different tools to confirm that, no, it won't break in weird ways like Webpack when I try to just write a few progressive enhancement hooks rather than a full SPA and, yes, it will output source maps that map from the original .ts file to the minified form of the JavaScript output.)

ssokolow avatar Sep 24 '22 12:09 ssokolow

With all available tools and implementations out there, and given that this issue... is over 8 years old!! It's nice to have new TypeScript language features, but can we please have some feature complete implementation for the current more trivial issues?

yanickrochon avatar Feb 13 '23 21:02 yanickrochon

After pondering this one for cough a while, we've decided that minification is pretty clearly out of scope for the TypeScript project and will remain so for the foreseeable future.

Broadly, this boils down to two key discussion points:

  1. Could TypeScript minify better than other minifiers in the space? If so, at what cost?
  2. Is putting a good-enough minifier "in the box" worth it?

Regarding the first point, after looking at what modern minifiers can do, what the type system requires of us (which is to say, no type-directed emit), and what other systems which do semantic-informed minification (Closure) do, the cost-benefit ratio is simply not there. Type-directed minification is not desirable or feasible. With types off the table, there's really nothing that TypeScript can do (prior to stripping the types, or immediately after) that other tools can't do an equally good job of. Given our finite time and complexity budget, the best move in terms of increasing the overall goodness of the JavaScript ecosystem is to invest our resources in places where we can provide a value-add in this space.

Regarding "good enough" minification, there's not a lot of meat left on that bone. We understand that people want fewer tools in the toolbox, but duplicating the work and competing on getting the same results with other minifiers seems like a net-negative for the ecosystem. If minification is worthwhile enough, then the extra build step is going to justify the cost over time. In fact, newer tools are so fast that the cost for good minification even lower than it was a decade ago when we first discussed this issue.

When TypeScript came out, it assumed it was the only build tool in the chain, and that other build tools like minifiers had to come strictly before or after it. Nowadays, most modern JavaScript tools operate directly on TypeScript - including some bundlers and minifiers. The kind of JS people are generally writing today, using ES Modules and (maybe) #private members, is also much easier to statically analyze for the sake of DCE/tree-shaking and local identifier renaming, both of which can be done after types are removed from the original code.

We know that there's been a lot of feedback on this issue, and as such believe that there is not much new ground left to cover with further comments on the topic. To prevent a flood of notifications on everyone's inbox, we're temporarily locking this issue for 2 weeks for some pre-emptive cooldown, but further discussion can pick up at that time if needed (2/28/2023).

RyanCavanaugh avatar Feb 14 '23 20:02 RyanCavanaugh

Is there any update on this?

hawkeye-sama avatar Mar 14 '24 01:03 hawkeye-sama

Update: This is still out of scope

RyanCavanaugh avatar Mar 15 '24 17:03 RyanCavanaugh

I don't know if there's already a proposal for this (couldn't find in a simple search), but IMO better than minification would be the opposite.

By that I mean an option for TypeScript to make every possible effort to keep JS line numbers equivalent to their TS source line numbers. Sometimes source maps are difficult to set up or just won't be properly supported. It would be great if I could look at a stack trace with a JS line number and go directly to its source on the same line.

So it would be great if TypeScript could:

  • ensure transpiled code retains line numbers as much as possible (e.g. expand by adding extra lines, collapse by minified one-liners) specially the executable lines
  • e.g. "minify" in one-line code added at the top (e.g. for compatibility) to avoid changing subsequent line numbers
  • keep my indentation style in the output (e.g. tabs or 2 spaces)

This is something you can't really do with an external tool (or at least it would be more complicated) I believe.

Is it worth opening a feature request?

geekley avatar Apr 17 '24 21:04 geekley

This is something you can't really do with an external tool

This would be straightforward-ish to do by using the emitted sourcemaps

RyanCavanaugh avatar Apr 17 '24 21:04 RyanCavanaugh

originally asked in 2014, we are 2024 now.

as a TypeScript n00b am I correct the tsconfig.json has no option to produce minified javascript output files directly? We have the option to "removeComments"

@chaser92 suggest " TypeScript should better do what it's best at, one tool should serve one purpose." I don't agree, it already has options to "minify" the output --> "removeComments", so why not further optimize it with additional options or a generic "minify": true/false ?

Not a fan of having yet another tool to maintain, update, (resolve conflicts- with while this could be in TypeScript itself.

looking from a distance at this this looks like something so obvious and "normal" to have...

Imagine the reduction of traffic and resources this could give on webservers if this would be on by default, globally. 💚 😃

wstaelens avatar Oct 15 '24 09:10 wstaelens