├── .clang-format
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── gulpfile.js
├── package.json
├── src
└── main.ts
├── test
├── e2e
│ └── small_e2e.ts
├── input
│ ├── a.ts
│ ├── b.ts
│ ├── e2e_input.ts
│ ├── external_call_site.ts
│ └── external_return.ts
└── unit
│ └── main_test.ts
├── tsconfig.json
└── tsd.json
/.clang-format:
--------------------------------------------------------------------------------
1 | Language: JavaScript
2 | BasedOnStyle: Google
3 | ColumnLimit: 100
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # node-waf configuration
20 | .lock-wscript
21 |
22 | # Compiled binary addons (http://nodejs.org/api/addons.html)
23 | build/Release
24 |
25 | # Dependency directory
26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27 | node_modules
28 |
29 | # Typescript Specific
30 | typings/
31 |
32 | build/
33 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "stable"
4 | before_script:
5 | - npm install
6 | script:
7 | - npm test
8 | cache:
9 | directories:
10 | - node_modules
11 | - typings
12 | env:
13 | global:
14 | # https://github.com/DefinitelyTyped/tsd#tsdrc
15 | # Token has no scope (read-only access to public information)
16 | - TSD_GITHUB_TOKEN=15cc257af05b77cc99ce080a1ddfcceb6e012ce2
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TS-Minify (Experimental) [](https://travis-ci.org/angular/ts-minify)
2 |
3 | ### About
4 |
5 | ---
6 | TS-Minify is tool to aid in the reduction of code size for programs written in the [TypeScript](http://www.typescriptlang.org/) language. It is currently **highly experimental**.
7 |
8 | This tool is developed on TypeScript and NodeJS, and transpiled to ES5; it uses the CommonJS module system.
9 |
10 | **There is currently no CLI or build-tool integration for TS-Minify.**
11 |
12 | ### Table of Contents
13 |
14 | ----------
15 |
16 | - [Motivation](#motivation)
17 | - [What it Does](#what-it-does)
18 | - [How Does it Work? The TL;DR](#how-does-it-work-the-tldr)
19 | - [External vs. Internal Types](#external-vs-internal-types)
20 | - [Structural Typing](#structural-typing)
21 | - [Usage](#usage)
22 | - [TSConfig Users](#tsconfig-users)
23 | - [Scope of Minification](#scope-of-minification)
24 | - [Caveats/Warnings](#caveatswarnings)
25 | - [Sample Measurements](#sample-measurements)
26 | - [Contributing](#contributing)
27 | - [Contributors](#contributors)
28 | - [License: Apache 2.0](#license-apache-20)
29 | - [Future](#future)
30 |
31 | ### Motivation
32 |
33 | ---
34 | Angular 2 (which is written in TypeScript) currently sits at around 135kb after being [Uglified](https://github.com/mishoo/UglifyJS) and compressed through GZIP. In comparison, Angular 1.4 is about 50kb, minified and compressed.
35 |
36 | A smaller bundle size means that less data needs to be transferred and loaded by the browser. This contributes to a better user experience.
37 |
38 | The impetus for this tool was to reduce the code size of the Angular 2 bundle, but TS-Minify is meant to be a generic tool that can be used on programs written in TypeScript.
39 |
40 |
41 | ### What it Does
42 |
43 | ---
44 | To achieve code size reduction, TS-Minify uses the idea of property renaming: take a TypeScript source file and rename properties to shorter property names, then re-emit the file as valid TypeScript.
45 |
46 |  This diagram demonstrates TS-Minify's role in the intended minification pipeline.
47 |
48 | TS-Minify *only* targets property renaming. Minification tactics like whitespace removal, dead code removal, variable name mangling, etc. are taken care of by tools such as [UglifyJS](https://github.com/mishoo/UglifyJS). TS-Minify specifically targets property renaming since it is something that is difficult to achieve *safely* without type information. As such, TS-Minify requires a program to be correctly and throughly typed, otherwise, unwanted renaming may occur.
49 |
50 | ### How Does it Work? The TL;DR
51 |
52 | ---
53 | The TypeScript Compiler API is utilized in order to access the Abstract Syntax Tree of a TypeScript source file. Identifiers that might be considered properties (such as identifiers in property declarations, property access expressions, method names in method declarations, etc.) are renamed to short names such as `a`, `b`, etc. after determining their renaming eligibility. A “renaming” global map is created with mappings from the original property name to the new generated property name so that re-namings are kept consistent between properties with the same names.
54 |
55 | ```javascript
56 | { maybeHandleCall: g }
57 | { reportMissingType: h }
58 | { getHandler: i }
59 | { handlePropertyAccess: j }
60 | { emitExtraImports: k }
61 | { emit: l }
62 | { visitTypeName: m }
63 | ```
64 | An example of some mappings from the original property name to the shorter, generated property name.
65 |
66 | The renaming eligibility of a property takes several factors into consideration:
67 |
68 | - Does the property belong to an object declared in an external file ([`.d.ts file`](http://definitelytyped.org/))?
69 | - Does the property belong to a [standard built-in object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects)?
70 | - Does the property name belong to the DOM?
71 |
72 | ### External vs. Internal Types
73 |
74 | ---
75 | TS-Minify is able to safely rename properties by understanding which types are *external*, and which are not, to one's program.
76 |
77 | An *external* type is something that you are bringing into your program. It is a type which you have not defined. This includes objects and functions from the JavaScript standard library, and JavaScript libraries like underscore.js or jQuery. If you are using external libraries, it is imperative that you include their corresponding `.d.ts` files.
78 |
79 | Typings for the standard library are included in the default library typings, `lib.d.ts`, which the TypeScript compiler uses during transpilation (it will typecheck your program at compile time) and does not need to be explicitly included.
80 |
81 | *Internal* types are ones that you, the programmer, have defined yourselves. Declaring a class `Foo` in your program tells the minifier that properties on `Foo` objects are within the renaming scope. Internal types also include things like object literal types.
82 |
83 | **Note**: If you want all internal sources to contain the same renamings, you should pass the relevant files together through a single pass of the minifier.
84 |
85 | An example of what types are considered *external* and *internal*:
86 |
87 | ```javascript
88 | var x: { name: string, message: string }; // The object literal is an internal type
89 |
90 | var x: Error; // Error is an external type
91 |
92 | // Structurally, the two are compatible, which brings us to the next section...
93 | ```
94 |
95 | ### Structural Typing
96 |
97 | ---
98 | TypeScript uses a [structural type system](https://en.wikipedia.org/wiki/Structural_type_system). This means that objects and variables can be casted from one type to another as long as they are structurally compatible. Sometimes this means that external objects might be casted to internal objects, or vise versa.
99 |
100 | Here is an example of an external type being casted to an internal type:
101 |
102 | ```javascript
103 | function x(foo: { name: string, message: string }) {
104 | foo.name;
105 | return foo;
106 | }
107 |
108 | x(new Error());
109 | ```
110 |
111 | Function `x` *expects* an internal object literal type. The call site `x(new Error())` passes an `Error` object as the parameter.
112 |
113 | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) is an object in the JavaScript standard library. Hence it is external to our small program above. Structurally, an `Error` object and the object literal `{ name: string, message: string }` are compatible. However, this means that we do not want to rename the properties on the object literal because we will likely want to access the properties of the `Error` object as we know them: `name` and `message`. If we renamed the properties of the object literal, it means that if an `Error` object is passed into a function call for `x()`, `foo.$some_renaming` will throw an error, because `Error` does not have property `$some_renaming`.
114 |
115 | Here is an example of an internal type being coerced into an external type:
116 |
117 | ```javascript
118 | function ff(e: Error) {
119 | return e.name;
120 | }
121 |
122 | ff({ name: null, message: null });
123 | ```
124 |
125 | The parameter `e` on function `ff` is an external type to our program. We do not want to rename its properties. At the function call site, we are passing in an object literal type with properties `name` and `message`, which is structurally compatible with `Error`. We do not want to rename properties in the object literal because we need to coerce it into an external typing, which is expected to have properties `name` and `message`.
126 |
127 | All of this is to say, the tool relies on type information to figure out if two types can be coerced into each other and whether their properties can be renamed based on the internal/external distinction. Type your programs! :)
128 |
129 | ### Usage
130 |
131 | ---
132 |
133 | First clone the repo and run `npm install`.
134 |
135 | ```shell
136 | $ git clone git@github.com:angular/ts-minify.git
137 | $ cd ts-minify
138 | $ npm install
139 | ```
140 |
141 | Create a new TypeScript file:
142 |
143 | ```javascript
144 | import {Minifier, options} from './src/main';
145 | var minifier = new Minifier();
146 | minifier.renameProgram(['path/to/file.ts', 'path/to/another/file.ts', 'some/other/file.d.ts'], 'path/to/destination');
147 | ```
148 |
149 | Transpile the above script into JavaScript using the TypeScript compiler and run it with Node:
150 |
151 | ```shell
152 | tsc --module commonjs script.ts
153 | node script.js
154 | ```
155 |
156 | A new instance of the minifier takes an optional constructor argument:
157 |
158 | ```javascript
159 | MinifierOptions {
160 | failFast?: boolean;
161 | basePath?: string;
162 | }
163 | ```
164 |
165 | `failFast`: Setting `failFast` to true will throw an error when the minifier hits one.
166 |
167 | `basePath`: Specifies the base path that the minifier uses to figure out to where to write the minfied TypeScript file. The `basePath` maybe be relative or absolute. All paths are resolved from the directory in which the minifier is executed. If there is no base path, the minified TypeScript file is outputted to the specified destination using a flattened file structure.
168 |
169 | `minifier.renameProgram()` takes a list of file names and an optional destination path:
170 |
171 | ```javascript
172 | renameProgram(fileNames: string[], destination?: string)
173 | ```
174 |
175 | `renameProgram` accepts TypeScript files and `.d.ts` files. If you are not explicitly including typings using the reference path syntax in your TypeScript file, please pass in the `.d.ts` file so that the minifier can use the type information.
176 |
177 | The minifier will uniformly rename the properties (which are available for renaming) across all the files that were passed in.
178 |
179 | ### TSConfig Users
180 |
181 | ---
182 | If you are using a `tsconfig.json` file to reference your type definitions, pass in the `.d.ts` files for libraries used in your program to the minifier so that it can reference the type information for property renaming. The minfier does not have access to the `tsconfig.json` file. Otherwise, make sure to have `/// ` at the top of your programs if you are using any external libraries.
183 |
184 | ### Scope of Minification
185 |
186 | ---
187 | Currently, TS-Minify will do property renaming throughout all the files that are passed into it. The minifier excludes `.d.ts` files from renaming because those are the typing definitions of external libraries. If your TypeScript program exports objects across those files, the minifier will rename them uniformly in their declarations and their usage sites.
188 |
189 | ### Caveats/Warnings
190 |
191 | ---
192 | In order to get the most out of the minifier, it is recommended that the user types their program as specifically and thoroughly as possible. In general, you should avoid using the `any` type, and implicit `any`s in your code as this might result in unwanted naming.
193 |
194 | We recommend that you compile your TypeScript program with the `noImplicitAny` compiler flag enabled before trying the minifier.
195 |
196 | Please avoid explicit any casting:
197 |
198 | ```javascript
199 | // example of explicit any casting
200 | var x = 7;
201 | x;
202 | ```
203 |
204 | ### Sample Measurements
205 |
206 | ---
207 | TS-Minify was run on a well-typed TypeScript program with the following stats and results:
208 |
209 | - Approximately 2100 lines of code (split across 10 files)
210 | - Unminified and Uglified: 72kb
211 | - Minified and Uglified: 56kb (codesize reduction of about 20%)
212 | - About 6% - 8% codesize reduction after minification, Uglification, and GZIP compression.
213 |
214 | ### Contributing
215 |
216 | ---
217 | Clone the repository from GitHub:
218 |
219 | ```shell
220 | $ git clone git@github.com:angular/ts-minify.git
221 | $ cd ts-minify
222 | $ npm install
223 | ```
224 |
225 | Run the unit tests with `gulp unit.test`
226 |
227 | Run the end-to-end tests with `gulp e2e.test`.
228 |
229 | This project uses `clang-format`. Run `gulp test.check-format` to make sure code you write conforms to this standard.
230 |
231 | - If you need some guidance on understanding TypeScript, look at the [TypeScript GitHub Wiki](https://github.com/Microsoft/TypeScript/wiki).
232 | - If you need a quick introduction to the TS Compiler API, take a look at the page on using the [TS Compiler](https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API) API.
233 | - Take a look at the `typescript.d.ts` for type signatures, and to understand what is available to you from the TS toolchain.
234 |
235 | If you need some help debugging, there is a `DEBUG` flag that can be enabled in `src/main.ts`:
236 |
237 | ```javascript
238 | const DEBUG = true; // switch from false to true to enable the console.logs
239 | ```
240 |
241 | There are some helpful print statements which print out:
242 |
243 | - the `SyntaxKind` of the nodes being traversed
244 | - the minified string output
245 | - a dictionary of external/internal type casts
246 |
247 | Remember to switch the `DEBUG` flag off afterwards.
248 |
249 | ### Contributors
250 |
251 | ---
252 | Daria Jung (Google intern)
253 |
254 |
255 | ### License: Apache 2.0
256 |
257 | ---
258 | ```
259 | Copyright 2015 Google, Inc. http://angular.io
260 |
261 | Licensed under the Apache License, Version 2.0 (the "License");
262 | you may not use this file except in compliance with the License.
263 | You may obtain a copy of the License at
264 |
265 | http://www.apache.org/licenses/LICENSE-2.0
266 |
267 | Unless required by applicable law or agreed to in writing, software
268 | distributed under the License is distributed on an "AS IS" BASIS,
269 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
270 | See the License for the specific language governing permissions and
271 | limitations under the License.
272 | ```
273 | ### Future
274 |
275 | ---
276 |
277 | Property renaming is hard and there are a lot of [issues](https://github.com/angular/ts-minify/issues)! If you're interesting in contributing to this project, please check some of them out.
278 |
279 | Issues are labelled `easy`, `medium`, and `hard`. Some have a `Needs Exploration` label which means you might have to poke around a bit before reaching conclusions about how to tackle the problem!
280 |
281 |
--------------------------------------------------------------------------------
/gulpfile.js:
--------------------------------------------------------------------------------
1 | var gulp = require('gulp');
2 | var mocha = require('gulp-mocha');
3 | var typescript = require('typescript');
4 | var tsc = require('gulp-typescript');
5 | var del = require('del');
6 | var exec = require('child_process').exec;
7 |
8 | var clangFormat = require('clang-format');
9 | var formatter = require('gulp-clang-format');
10 |
11 | var TSC_OPTIONS = {
12 | module: "commonjs",
13 | noExternalResolve: false,
14 | declarationFiles: true,
15 | typescript: require('typescript'),
16 | };
17 |
18 | var tsProject = tsc.createProject(TSC_OPTIONS);
19 |
20 | gulp.task('test.check-format', function() {
21 | return gulp.src(['*.js', 'src/**/*.ts', 'test/**/*.ts'])
22 | .pipe(formatter.checkFormat('file', clangFormat));
23 | });
24 |
25 | gulp.task('clean', function(callback) { del(['./build/'], callback); });
26 |
27 | gulp.task('compile', function() {
28 | var sourceTsFiles = ['./src/*.ts'];
29 |
30 | var tsResult = gulp.src(sourceTsFiles).pipe(tsc(tsProject));
31 |
32 | tsResult.dts.pipe(gulp.dest('./build/src'));
33 | return tsResult.js.pipe(gulp.dest('./build/src'));
34 | });
35 |
36 | gulp.task('test.compile', ['compile'], function(done) {
37 | return gulp.src(['test/**/*.ts'], {base: '.'}).pipe(tsc(tsProject)).pipe(gulp.dest('build/'));
38 | });
39 |
40 | gulp.task('unit.test', ['test.compile'], function() {
41 | var mochaOptions = {};
42 | return gulp.src('build/test/unit/main_test.js', {read: false}).pipe(mocha(mochaOptions));
43 | });
44 |
45 | gulp.task('e2e.test', ['test.compile'], function() {
46 | var e2e = require('./build/test/e2e/small_e2e');
47 | e2e.runE2ETests();
48 | });
49 |
50 | gulp.task('watch', function() {
51 | gulp.watch(['./src/*.ts', './test/**/*.ts'], ['compile', 'test.compile', 'unit.test', 'e2e.test']);
52 | });
53 |
54 | gulp.task('default', ['compile', 'unit.test', 'e2e.test', 'watch']);
55 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ts-minify",
3 | "version": "0.0.1",
4 | "description": "A tool to aid in the minification of Typescript code.",
5 | "repository": "https://github.com/angular/ts-minify",
6 | "license": "Apache-2.0",
7 | "scripts": {
8 | "prepublish": "npm install tsd@^0.6.3 && tsd reinstall --overwrite",
9 | "test": "gulp unit.test"
10 | },
11 | "dependencies": {
12 | "typescript": "alexeagle/TypeScript#error_is_class",
13 | "gulp": ">=3.9.0",
14 | "fs-extra": "^0.23.0"
15 | },
16 | "devDependencies": {
17 | "clang-format": "^1.0.25",
18 | "gulp-clang-format": "^1.0.21",
19 | "gulp-mocha": "^2.1.1",
20 | "chai": "*",
21 | "mocha": "*",
22 | "gulp-util": ">=3.0.6",
23 | "gulp-typescript": "^2.8.0",
24 | "del": ">=1.2.0",
25 | "tsd": "^0.6.3"
26 | }
27 | }
--------------------------------------------------------------------------------
/src/main.ts:
--------------------------------------------------------------------------------
1 | ///
2 | ///
3 | ///
4 |
5 | import * as ts from 'typescript';
6 | import * as path from 'path';
7 | import * as fsx from 'fs-extra';
8 | import * as fs from 'fs';
9 |
10 | const DEBUG = false;
11 |
12 | export const options: ts.CompilerOptions = {
13 | allowNonTsExtensions: true,
14 | module: ts.ModuleKind.CommonJS,
15 | target: ts.ScriptTarget.ES5,
16 | };
17 |
18 | export interface MinifierOptions {
19 | failFast?: boolean;
20 | basePath?: string;
21 | }
22 |
23 | export class Minifier {
24 | static reservedJSKeywords = Minifier.buildReservedKeywordsMap();
25 | // Key: (Eventually fully qualified) original property name
26 | // Value: new generated property name
27 | private _renameMap: {[name: string]: string} = {};
28 |
29 | // Key: Type symbol at actual use sites (from)
30 | // Value: A list of the expected type symbol (to)
31 | private _typeCasting: Map =