├── .github └── FUNDING.yml ├── .gitignore ├── .travis.yml ├── README.md ├── extraParams.hxml ├── haxelib.json ├── releaseHaxelib.sh ├── src └── UglifyJS.hx ├── test.hxml ├── test └── Main.hx └── uglifyjs.hxproj /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | patreon: markknol 4 | custom: ['http://paypal.me/markknol'] 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | dist: trusty 3 | 4 | language: haxe 5 | 6 | haxe: 7 | - "3.4.4" 8 | - "3.4.5" 9 | - "3.4.6" 10 | - "3.4.7" 11 | - "development" 12 | 13 | matrix: 14 | allow_failures: 15 | - haxe: development 16 | 17 | install: 18 | - npm install uglify-js 19 | - haxelib dev uglifyjs . 20 | 21 | script: 22 | - haxe test.hxml 23 | 24 | deploy: 25 | - provider: script 26 | haxe: 3.4.7 27 | script: bash ./releaseHaxelib.sh $HAXELIB_PWD 28 | on: 29 | tags: true -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Haxe/JavaScript Uglify 2 | [![Build Status](https://travis-ci.org/markknol/hx-uglifyjs.svg?branch=master)](https://travis-ci.org/markknol/hx-uglifyjs) [![Haxelib Version](https://img.shields.io/github/tag/markknol/hx-uglifyjs.svg?label=haxelib)](http://lib.haxe.org/p/uglifyjs) 3 | 4 | UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit. 5 | 6 | This library runs the [`uglify-js` node module](https://www.npmjs.com/package/uglify-js) after your Haxe/JavaScript build completed. 7 | 8 | ## Installation 9 | 10 | First: Install the node module using NPM: 11 | 12 | To install locally on your project: (recommended) 13 | ```cli 14 | npm install uglify-js --save-dev 15 | ``` 16 | 17 | To install globally use: 18 | ```cli 19 | npm install uglify-js -g 20 | ``` 21 | 22 | Second: Install using [Haxelib](https://lib.haxe.org/p/uglifyjs/): 23 | 24 | ```bash 25 | haxelib install uglifyjs 26 | ``` 27 | 28 | To use in code, add to your build hxml: 29 | 30 | ```bash 31 | -lib uglifyjs 32 | 33 | 34 | # disable uglifyjs from being executed: 35 | -D uglifyjs_disabled 36 | 37 | # overwrite original output rather then generating a .min.js next to it 38 | -D uglifyjs_overwrite 39 | 40 | # disable compression 41 | -D uglifyjs_no_compress 42 | 43 | # disable mangling (renaming of local variables) 44 | -D uglifyjs_no_mangle 45 | 46 | # keep JavaScript comments 47 | -D uglifyjs_comments 48 | -D uglifyjs_comments=filter 49 | 50 | # keep line breaks and indent the generated code 51 | -D uglifyjs_beautify 52 | 53 | # override default uglify-js node module path 54 | -D uglifyjs_bin=path/to/bin/uglifyjs 55 | 56 | # enable source map generation 57 | -D uglifyjs_sourcemap 58 | 59 | # sourcemap generation options. 60 | # See UglifyJS docs for details on usage. 61 | ## url='url/to/sourcemap/directory/OutFile.map' 62 | ## value will default to just OutFile.map if define value is not specified 63 | -D uglifyjs_sourcemap_url[='url/to/sourcemap/directory/'] 64 | 65 | ## includeSources 66 | -D uglifyjs_sourcemap_sources 67 | 68 | ## base=path/to/base 69 | -D uglifyjs_sourcemap_base=path/to/base 70 | 71 | ## root=path/to/root 72 | -D uglifyjs_sourcemap_root=path/to/root 73 | 74 | ## content=path/to/inputmap.js.map 75 | -D uglifyjs_sourcemap_content=path/to/inputmap.js.map 76 | 77 | ## file=path/to/InFile.js 78 | -D uglifyjs_sourcemap_file=path/to/InFile.js 79 | 80 | ``` 81 | 82 | ## Using Terser as alternative of Uglify 83 | 84 | If you are using [`terser` node module](https://www.npmjs.com/package/terser) instead of `uglifyjs`, you can just use `-D uglifyjs_bin=node_modules/.bin/terser` (assuming it's a local dependency) since both API's are the same. 85 | -------------------------------------------------------------------------------- /extraParams.hxml: -------------------------------------------------------------------------------- 1 | --macro UglifyJS.run() 2 | -------------------------------------------------------------------------------- /haxelib.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "uglifyjs", 3 | "url": "https://github.com/markknol/hx-uglifyjs", 4 | "license": "MIT", 5 | "tags": ["haxe","js","uglify","minify","obfuscation"], 6 | "description": "Minify using uglifyjs", 7 | "version": "1.0.0", 8 | "classPath": "src/", 9 | "releasenote": "add -D uglifyjs_beautify", 10 | "contributors": [ 11 | "markknol" 12 | ], 13 | "dependencies": { 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /releaseHaxelib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | rm -f uglifyjs.zip 3 | zip -r uglifyjs.zip README.md src haxelib.json extraParams.hxml 4 | haxelib submit uglifyjs.zip $HAXELIB_PWD --always 5 | -------------------------------------------------------------------------------- /src/UglifyJS.hx: -------------------------------------------------------------------------------- 1 | #if macro 2 | import haxe.macro.Context; 3 | import haxe.macro.Compiler; 4 | #end 5 | import sys.FileSystem; 6 | using StringTools; 7 | using haxe.io.Path; 8 | 9 | class UglifyJS { 10 | 11 | public static function run() { 12 | #if (!display && macro) 13 | if (!Context.defined("uglifyjs_disabled") && !Context.defined("uglifyjs_slavemode")) { 14 | Context.onAfterGenerate(compile); 15 | } 16 | #end 17 | } 18 | 19 | static function compile() { 20 | #if macro 21 | var inPath = Compiler.getOutput(); 22 | if (!inPath.endsWith('.js')) { 23 | Context.warning('Expected .js extension for output file $inPath', Context.currentPos()); 24 | } else { 25 | var outputPath = if (Context.defined("uglifyjs_overwrite")) inPath else '${inPath.withoutExtension()}.min.js'; 26 | compileFile(inPath, outputPath); 27 | } 28 | #end 29 | } 30 | 31 | static public function compileFile(inPath: String, outPath: String) { 32 | var params = [ 33 | #if !uglifyjs_no_compress 34 | '--compress', 35 | #end 36 | #if !uglifyjs_no_mangle 37 | '--mangle', 38 | #end 39 | #if uglifyjs_comments 40 | '--comments', 41 | #end 42 | #if uglifyjs_sourcemap 43 | '--source-map', 44 | #end 45 | #if uglifyjs_beautify 46 | "--beautify", 47 | #end 48 | '--output', outPath, 49 | '--', inPath 50 | ]; 51 | 52 | #if uglifyjs_sourcemap 53 | var sourcemapParams = [ 54 | #if uglifyjs_sourcemap_url 55 | "url='" + 56 | #if macro (Context.definedValue("uglifyjs_sourcemap_url") != "1" 57 | ? Context.definedValue("uglifyjs_sourcemap_url") 58 | : "") 59 | + #end Path.withoutDirectory(outPath) + ".map'", 60 | #end 61 | #if uglifyjs_sourcemap_sources 62 | 'includeSources', 63 | #end 64 | #if macro 65 | #if uglifyjs_sourcemap_base 66 | "base='" + Context.definedValue("uglifyjs_sourcemap_base") + "'", 67 | #end 68 | #if uglifyjs_sourcemap_content 69 | "content='" + Context.definedValue("uglifyjs_sourcemap_content") + "'", 70 | #end 71 | #if uglifyjs_sourcemap_filename 72 | "filename='" + Context.definedValue("uglifyjs_sourcemap_filename") + "'", 73 | #end 74 | #if uglifyjs_sourcemap_root 75 | "root='" + Context.definedValue("uglifyjs_sourcemap_root") + "'", 76 | #end 77 | #end 78 | ]; 79 | 80 | var sourcemapArgs = sourcemapParams.join(','); 81 | if (sourcemapArgs.trim().length > 0) { 82 | params.insert(params.indexOf("--source-map") + 1, sourcemapArgs); 83 | } 84 | #end 85 | 86 | 87 | #if uglifyjs_comments 88 | var uglifyjs_comments = Context.definedValue("uglifyjs_comments"); 89 | if (uglifyjs_comments.length > 1) params.insert(params.indexOf("--comments") + 1, uglifyjs_comments); 90 | #end 91 | 92 | Sys.command(getCmd(), params); 93 | } 94 | 95 | static function getCmd() { 96 | 97 | var cmd = #if macro Context.defined('uglifyjs_bin') 98 | ? Context.definedValue('uglifyjs_bin') 99 | : #end Sys.systemName() == 'Windows' 100 | ? 'node_modules\\.bin\\uglifyjs.cmd' 101 | : './node_modules/.bin/uglifyjs'; 102 | 103 | if (!FileSystem.exists(cmd)) cmd = 'uglifyjs'; // try global 104 | return cmd; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /test.hxml: -------------------------------------------------------------------------------- 1 | -main Main 2 | -cp test 3 | -lib uglifyjs 4 | 5 | --each 6 | 7 | # test with normal settings 8 | -js bin/uglifyjs.js 9 | 10 | --next 11 | 12 | -js bin/uglifyjs.js 13 | 14 | # test again with additional settings 15 | -D uglifyjs_disabled 16 | -D uglifyjs_overwrite 17 | -D uglify_no_compress 18 | -D uglify_no_mangle 19 | -D uglifyjs_comments 20 | -D uglifyjs_sourcemap 21 | -D uglifyjs_sourcemap_sources 22 | -------------------------------------------------------------------------------- /test/Main.hx: -------------------------------------------------------------------------------- 1 | package ; 2 | 3 | /** 4 | @author $author 5 | **/ 6 | class Main { 7 | public static function main() { 8 | new Main(); 9 | } 10 | 11 | public function new() { 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /uglifyjs.hxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | cmd /c haxe $(OutputFile) 44 | 45 | 46 | 47 | 48 | 52 | 53 | 54 | --------------------------------------------------------------------------------