24

Are there any tools or forks of TypeScript to support public namespace obfuscation? I.e. to turn:

class MyUtil {
   print(): void { ... }
}

Into something like:

class xxy {
   aab(): void { ... }
}

If not, would be be straight forward to fork the compiler to provide this? Perhaps with some type of class annotation indication what should / shouldn't be obfuscated.

(Obviously public obfuscation has to be used carefully, no good for libraries! But great if used consistently across your project)

wonea
  • 4,783
  • 17
  • 86
  • 139
user826840
  • 1,233
  • 2
  • 13
  • 28
  • Possible duplicate of https://stackoverflow.com/questions/12682268/is-it-possible-to-compile-typescript-into-minified-code. – Andrew Eisenberg Mar 09 '18 at 19:06
  • "mangle" option with a JS Uglify package on your Javascript Output, can be done with Webpack using UglifyJsPlugin – Disfigure Mar 12 '18 at 20:10

3 Answers3

23

I had the exact same question, and it was instantly deleted by SO.

https://github.com/angular/ts-minify

This is exactly the tool you (and I) are looking for, it seems to work pretty well, I needed to comment out a few parts where the the author was taking precautions I don't think are applicable.

wonea
  • 4,783
  • 17
  • 86
  • 139
user378380
  • 771
  • 8
  • 12
  • 1
    The most likely reason why your question was closed is because you were asking for tool recommendations, which is specifically not appropriate for SO. See https://meta.stackoverflow.com/questions/254393/what-exactly-is-a-recommendation-question – Andrew Eisenberg Mar 09 '18 at 19:06
  • 1
    I really still feel like ts-minify is the answer you seek, have you tried it and it doesnt work? – user378380 Mar 10 '18 at 19:46
  • 3
    Asking for _tools_ is one of the ways to ask for _ways_. Programmatically speaking, `Tools extends Ways`. It is not inappropriate. I don't get it. Upvoted this one. – Константин Ван Mar 14 '18 at 07:01
9

I don't believe there is any reason to do this in TypeScript. You can instead use something like Closure Compiler to do the obfuscation on your JavaScript output. Specifically look into Closure's Advanced Compilation settings.

Brian Terlson
  • 9,330
  • 1
  • 21
  • 18
  • 6
    I appreciate it could be done on plain JS with other tools. But I think it makes sense to do it as part of the TS compiler, it will reduce the number of separate tools you need to install if nothing else. I'd like to see TS offering minification and obfuscation options. – user826840 Jan 01 '13 at 11:38
  • 3
    It absolutely makes sense for Typescript to 'know' about obfuscation. I want to be forced to explicitly mark properties as 'public' (or exported or whatever) that I need exposed to my HTML page. For instance I'm using Knockout and I need to bind to some properties on the viewmodel but not all, or maybe I have private functions. If Typescript could handle this for me I'd get much more efficient code where a standard obfuscator may not have enough information to make the decision. – Simon_Weaver Feb 18 '15 at 21:10
  • If you ship TypeScript code for production, then this is valid. You may be using `ts-node` for on-the-fly transpilation (which isn't as slow as you might imagine), or running your app in a TypeScript interpreter like Deno (https://github.com/denoland/deno), etc. – trusktr May 28 '19 at 19:51
0

 Not a direct answer! Js obfuscator

https://github.com/javascript-obfuscator/javascript-obfuscator

Plugins:

Webpack plugin: webpack-obfuscator
Webpack loader: obfuscator-loader
Gulp: gulp-javascript-obfuscator
Grunt: grunt-contrib-obfuscator
Rollup: rollup-plugin-javascript-obfuscator

NOTE:

Only obfuscate the code that belongs to you.

It is not recommended to obfuscate vendor scripts and polyfills, since the obfuscated code is 15-80% slower (depends on options) and the files are significantly larger.

Community
  • 1
  • 1
Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97