From 73abf0cb012562400264075288933f12695337cb Mon Sep 17 00:00:00 2001 From: cjblomqvist Date: Thu, 27 Mar 2014 17:54:23 +0100 Subject: [PATCH 1/2] Add support for other charsets than utf-8 for input --- bin/uglifyjs | 21 ++++++++++++++++++--- package.json | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 3a3318b2..d8164ade 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -9,6 +9,7 @@ var optimist = require("optimist"); var fs = require("fs"); var path = require("path"); var async = require("async"); +var iconv = require("iconv-lite") var acorn; var ARGS = optimist .usage("$0 input1.js [input2.js ...] [options]\n\ @@ -64,6 +65,7 @@ You need to pass an argument to this option to specify the name that your module .describe("v", "Verbose") .describe("V", "Print version number and exit.") .describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.") + .describe("charset-input", "Charset of input (default utf-8)") .alias("p", "prefix") .alias("o", "output") @@ -87,6 +89,7 @@ You need to pass an argument to this option to specify the name that your module .string("comments") .string("wrap") .string("p") + .string("charset-input") .boolean("expr") .boolean("source-map-include-sources") @@ -434,15 +437,27 @@ function getOptions(x, constants) { function read_whole_file(filename, cb) { if (filename == "-") { var chunks = []; - process.stdin.setEncoding('utf-8'); + if(!ARGS.charset_input) process.stdin.setEncoding('utf-8'); process.stdin.on('data', function (chunk) { chunks.push(chunk); }).on('end', function () { - cb(null, chunks.join("")); + if(ARGS.charset_input) { + cb(null, iconv.decode(Buffer.concat(chunks), ARGS.charset_input)); + } else { + cb(null, chunks.join("")); + } }); process.openStdin(); } else { - fs.readFile(filename, "utf-8", cb); + if(ARGS.charset_input) { + fs.readFile(filename, function (err, data) { + if(err) cb(err, data); + + cb(err, iconv.decode(data, ARGS.charset_input)); + }); + } else { + fs.readFile(filename, "utf-8", cb); + } } } diff --git a/package.json b/package.json index 031e3395..b5b6111b 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "async" : "~0.2.6", "source-map" : "~0.1.33", "optimist" : "~0.3.5", - "uglify-to-browserify": "~1.0.0" + "uglify-to-browserify": "~1.0.0", + "iconv-lite": "*" }, "browserify": { "transform": [ "uglify-to-browserify" ] From a220f76ca922cd373d5769571371a224d147fa58 Mon Sep 17 00:00:00 2001 From: cjblomqvist Date: Thu, 27 Mar 2014 18:08:47 +0100 Subject: [PATCH 2/2] Add support for other charsets than utf-8 for output (for executable anyway) --- bin/uglifyjs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index d8164ade..6ea04dbf 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -66,6 +66,7 @@ You need to pass an argument to this option to specify the name that your module .describe("V", "Print version number and exit.") .describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.") .describe("charset-input", "Charset of input (default utf-8)") + .describe("charset-output", "Charset of output (default utf-8)") .alias("p", "prefix") .alias("o", "output") @@ -370,9 +371,13 @@ async.eachLimit(files, 1, function (file, cb) { } if (OUTPUT_FILE) { - fs.writeFileSync(OUTPUT_FILE, output, "utf8"); + if(ARGS.charset_output) { + fs.writeFileSync(OUTPUT_FILE, iconv.encode(output, ARGS.charset_output)); + } else { + fs.writeFileSync(OUTPUT_FILE, output, "utf8"); + } } else { - sys.print(output); + sys.print(ARGS.charset_output ? iconv.encode(output, ARGS.charset_output) : output); } if (ARGS.stats) {