Title: Introduce new flag for minifier optimization hints · Issue #38374 · microsoft/TypeScript · GitHub
Open Graph Title: Introduce new flag for minifier optimization hints · Issue #38374 · microsoft/TypeScript
X Title: Introduce new flag for minifier optimization hints · Issue #38374 · microsoft/TypeScript
Description: Search Terms Optimization Minifier Bundler IIFE Wrapper Terser Suggestion TypeScript 3.9 pulled in PR #32011, which tailors TypeScript's output for optimization with minifiers such as Terser. This PR definitely goes in the right directio...
Open Graph Description: Search Terms Optimization Minifier Bundler IIFE Wrapper Terser Suggestion TypeScript 3.9 pulled in PR #32011, which tailors TypeScript's output for optimization with minifiers such as Terser. This ...
X Description: Search Terms Optimization Minifier Bundler IIFE Wrapper Terser Suggestion TypeScript 3.9 pulled in PR #32011, which tailors TypeScript's output for optimization with minifiers such as Terser. T...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38374
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Introduce new flag for minifier optimization hints","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ, especially the \"Common Feature Requests\" section: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\nOptimization\r\nMinifier\r\nBundler\r\nIIFE\r\nWrapper\r\nTerser\r\n\r\n## Suggestion\r\n\r\nTypeScript 3.9 pulled in PR #32011, which tailors TypeScript's output for optimization with minifiers such as Terser. This PR definitely goes in the right direction for optimizing TypeScript's emit for most users, but doesn't necessarily work for all toolchains out there. At Google we have optimization processes after TypeScript compilation that use static analysis to look for classes, do some rough type-checking, and for example rename class properties and drop methods. The wrapper introduced in TypeScript 3.9 hinders our ability to analyze these classes.\r\n\r\nWe'd like to propose a new flag to toggle this behavior, which I'll call `--add-optimization-hints` for now. This flag would control if TypeScript decides to emit these hints for optimizers and bundlers, which is not necessarily limited to the `/** @class */` form introduced in TypeScript 3.9. For users such as Google, we can disable this flag and instead rely on our own optimizers to handle this, but users piping directly through Webpack and Terser can still receive the benefits from #32011.\r\n\r\n## Use Cases\r\n\r\n### Removing IIFE Wrapper for ES2015 Classes\r\nExpanding on our toolchain a bit from above, at Google we use a combination of [tsickle](https://github.com/angular/tsickle), [closure-compiler](https://github.com/google/closure-compiler), and other tools to optimize our TypeScript code. Tsickle is used to add type annotations and use the `goog.module` module system to TypeScript's emit such that Closure Compiler can optimize the code. Using the TypeScript 3.9 RC which emits IIFE wrappers around ES2015 classes Closure Compiler is unable to understand that this is an alias for the inner class. By setting `--add-optimization-hints=false` we would be able to statically analyze TypeScript's output without doing additional special processing.\r\n\r\n### Adding Further Optimization Hints\r\nPR #32011 isn't the first to request optimization hints from the TypeScript output, for example #13721 requested a `/** #pure */` annotation as well, and there could be more in the future that TypeScript would like to hint at. Adding this flag allows TypeScript to produce messier code that is less similar to the input, but more compatible with bundlers and minifiers.\r\n\r\n## Examples\r\n\r\nThis would be a new flag for the TypeScript compiler. Assuming TypeScript 3.9 is being used, given the input:\r\n\r\n```ts\r\n// input.ts\r\nclass C {\r\n static prop = 10;\r\n\r\n getProp() {\r\n return C.prop;\r\n }\r\n}\r\n```\r\n\r\n**Compiling with `tsc --add-optimization-hints=true`**:\r\n\r\n```js\r\n// output.js\r\n\"use strict\";\r\nlet C = /** @class */ (() => {\r\n class C {\r\n getProp() {\r\n return C.prop;\r\n }\r\n }\r\n C.prop = 10;\r\n return C;\r\n})();\r\n```\r\n\r\n**Compiling with `tsc --add-optimization-hints=false`**:\r\n\r\n```js\r\n// output.js\r\n\"use strict\";\r\nclass C {\r\n getProp() {\r\n return C.prop;\r\n }\r\n}\r\nC.prop = 10;\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n","author":{"url":"https://github.com/rrdelaney","@type":"Person","name":"rrdelaney"},"datePublished":"2020-05-06T21:50:46Z","url":"https://github.com/microsoft/TypeScript/issues/38374"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d23a55ac-21e8-74db-298e-de44325f5ba1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCB0:3A8E64:D6DF5:11D9C7:6B0182AF |
| html-safe-nonce | 5930ad9ea4c53f83941dbb1ba6b18e692de30fb0b0794fc57e8f8246c25dda67 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0IwOjNBOEU2NDpENkRGNToxMUQ5Qzc6NkIwMTgyQUYiLCJ2aXNpdG9yX2lkIjoiNjcyMzE5MzA5MzcxMTAzNzEwMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d6067d2b017a1a168f49f80eca821408958548e9433b1b829c043f3279f7d60b |
| hovercard-subject-tag | issue:613635487 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/38374/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff1b9b683e190c978106841d093f24b81de512523d8d7caf26904fbe15034ea3/microsoft/TypeScript/issues/38374 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff1b9b683e190c978106841d093f24b81de512523d8d7caf26904fbe15034ea3/microsoft/TypeScript/issues/38374 |
| og:image:alt | Search Terms Optimization Minifier Bundler IIFE Wrapper Terser Suggestion TypeScript 3.9 pulled in PR #32011, which tailors TypeScript's output for optimization with minifiers such as Terser. This ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rrdelaney |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width