From c8ab9fe2cae7fca7ccf2f06c659b8591e4328e5d Mon Sep 17 00:00:00 2001 From: kzc Date: Fri, 29 Jul 2016 16:18:56 -0400 Subject: [PATCH 1/2] Add simple file globbing to bin/uglifyjs for Windows --- bin/uglifyjs | 3 +++ tools/node.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/bin/uglifyjs b/bin/uglifyjs index 30d234fd..3f0c8254 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -273,6 +273,9 @@ if (ARGS.comments != null) { var files = ARGS._.slice(); +if (process.platform === "win32") + files = UglifyJS.simple_glob(files); + if (ARGS.self) { if (files.length > 0) { print_error("WARN: Ignoring input files since --self was passed"); diff --git a/tools/node.js b/tools/node.js index 20ecb473..87be47b7 100644 --- a/tools/node.js +++ b/tools/node.js @@ -261,3 +261,47 @@ exports.writeNameCache = function(filename, key, cache) { fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8"); } }; + +// A file glob function that only supports "*" and "?" wildcards in the basename. +// Example: "foo/bar/*baz??.*.js" +// Argument `glob` may be a string or an array of strings. +// Returns an array of strings. Garbage in, garbage out. +exports.simple_glob = function simple_glob(glob) { + var results = []; + if (Array.isArray(glob)) { + glob.forEach(function(elem) { + results = results.concat(simple_glob(elem)); + }); + return results; + } + if (glob.match(/\*|\?/)) { + var dir = path.dirname(glob); + try { + var entries = fs.readdirSync(dir); + } catch (ex) {} + if (entries) { + var pattern = "^" + (path.basename(glob) + .replace(/\(/g, "\\(") + .replace(/\)/g, "\\)") + .replace(/\{/g, "\\{") + .replace(/\}/g, "\\}") + .replace(/\[/g, "\\[") + .replace(/\]/g, "\\]") + .replace(/\+/g, "\\+") + .replace(/\^/g, "\\^") + .replace(/\$/g, "\\$") + .replace(/\*/g, "[^/\\\\]*") + .replace(/\./g, "\\.") + .replace(/\?/g, ".")) + "$"; + var mod = process.platform === "win32" ? "i" : ""; + var rx = new RegExp(pattern, mod); + for (var i in entries) { + if (rx.test(entries[i])) + results.push(dir + "/" + entries[i]); + } + } + } + if (results.length === 0) + results = [ glob ]; + return results; +}; From c393f3406caa1439b55afbcb3e09e4d8d7d2920a Mon Sep 17 00:00:00 2001 From: kzc Date: Fri, 29 Jul 2016 21:27:30 -0400 Subject: [PATCH 2/2] Add input file glob support to minify() --- test/input/issue-1242/bar.es5 | 4 ++++ test/input/issue-1242/baz.es5 | 4 ++++ test/input/issue-1242/foo.es5 | 5 +++++ test/input/issue-1242/qux.js | 4 ++++ test/mocha/glob.js | 28 ++++++++++++++++++++++++++++ tools/node.js | 1 + 6 files changed, 46 insertions(+) create mode 100644 test/input/issue-1242/bar.es5 create mode 100644 test/input/issue-1242/baz.es5 create mode 100644 test/input/issue-1242/foo.es5 create mode 100644 test/input/issue-1242/qux.js create mode 100644 test/mocha/glob.js diff --git a/test/input/issue-1242/bar.es5 b/test/input/issue-1242/bar.es5 new file mode 100644 index 00000000..6e308a30 --- /dev/null +++ b/test/input/issue-1242/bar.es5 @@ -0,0 +1,4 @@ +function bar(x) { + var triple = x * (2 + 1); + return triple; +} diff --git a/test/input/issue-1242/baz.es5 b/test/input/issue-1242/baz.es5 new file mode 100644 index 00000000..83c98ef6 --- /dev/null +++ b/test/input/issue-1242/baz.es5 @@ -0,0 +1,4 @@ +function baz(x) { + var half = x / 2; + return half; +} diff --git a/test/input/issue-1242/foo.es5 b/test/input/issue-1242/foo.es5 new file mode 100644 index 00000000..4b439075 --- /dev/null +++ b/test/input/issue-1242/foo.es5 @@ -0,0 +1,5 @@ +var print = console.log.bind(console); +function foo(x) { + var twice = x * 2; + print('Foo:', twice); +} diff --git a/test/input/issue-1242/qux.js b/test/input/issue-1242/qux.js new file mode 100644 index 00000000..94171f38 --- /dev/null +++ b/test/input/issue-1242/qux.js @@ -0,0 +1,4 @@ +var a = bar(1+2); +var b = baz(3+9); +print('q' + 'u' + 'x', a, b); +foo(5+6); diff --git a/test/mocha/glob.js b/test/mocha/glob.js new file mode 100644 index 00000000..c2fc9464 --- /dev/null +++ b/test/mocha/glob.js @@ -0,0 +1,28 @@ +var Uglify = require('../../'); +var assert = require("assert"); + +describe("minify() with input file globs", function() { + it("minify() with one input file glob string.", function() { + var result = Uglify.minify("test/input/issue-1242/foo.*", { + compress: { collapse_vars: true } + }); + assert.strictEqual(result.code, 'function foo(o){print("Foo:",2*o)}var print=console.log.bind(console);'); + }); + it("minify() with an array of one input file glob.", function() { + var result = Uglify.minify([ + "test/input/issue-1242/b*.es5", + ], { + compress: { collapse_vars: true } + }); + assert.strictEqual(result.code, 'function bar(n){return 3*n}function baz(n){return n/2}'); + }); + it("minify() with an array of multiple input file globs.", function() { + var result = Uglify.minify([ + "test/input/issue-1242/???.es5", + "test/input/issue-1242/*.js", + ], { + compress: { collapse_vars: true } + }); + assert.strictEqual(result.code, 'function bar(n){return 3*n}function baz(n){return n/2}function foo(n){print("Foo:",2*n)}var print=console.log.bind(console);print("qux",bar(3),baz(12)),foo(11);'); + }); +}); diff --git a/tools/node.js b/tools/node.js index 87be47b7..5d0d9637 100644 --- a/tools/node.js +++ b/tools/node.js @@ -73,6 +73,7 @@ exports.minify = function(files, options) { bare_returns: options.parse ? options.parse.bare_returns : undefined }); } + if (!options.fromString) files = UglifyJS.simple_glob(files); [].concat(files).forEach(function (files, i) { if (typeof files === 'string') { addFile(files, options.fromString ? i : files);