From 5c853fbca71c3490432fe328c2222bdcb44ff61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Fri, 21 Aug 2020 11:12:29 +0200 Subject: [PATCH] support asynchronous uglify modules --- index.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index caf4d0a..cc03ab9 100644 --- a/index.js +++ b/index.js @@ -21,17 +21,24 @@ function uglifyStream (opts) { var stream = duplexify() var writer = concat({ encoding: 'string' }, function (source) { - var minified = uglify.minify(source, opts) - if (minified.error) { - stream.emit('error', minified.error) - return + var result = uglify.minify(source, opts) + if (result.then) { + result.then(onsuccess, onerror) + } else if (result.error) { + return onerror(result.error) + } else onsuccess(result) + + function onsuccess (minified) { + var final = minified.code + if (minified.map) { + final += '\n' + convert.fromJSON(minified.map).toComment() + } + var reader = fromString(final) + stream.setReadable(reader) } - var final = minified.code - if (minified.map) { - final += '\n' + convert.fromJSON(minified.map).toComment() + function onerror (error) { + stream.emit('error', error) } - var reader = fromString(final) - stream.setReadable(reader) }) stream.setWritable(writer)