From 7ff1e1b08e6f7525bb6fe29617acc7e52c3725d6 Mon Sep 17 00:00:00 2001 From: Josh Del Favero Date: Thu, 18 Jan 2018 12:02:29 -0500 Subject: [PATCH] Refactored the minifyJS to include support for writing the source map file of the minified code --- node/minifyjs.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/node/minifyjs.js b/node/minifyjs.js index 76876d5..770fa8c 100644 --- a/node/minifyjs.js +++ b/node/minifyjs.js @@ -9,13 +9,28 @@ var domainManager; function minifyJS(currentPath, filepath, customPath, options) { - var text; + var code = {}; + var mapPath; + var inputFile = currentPath.split('/').pop(); + var pathroot = filepath.split('/').slice(0, -1).join('/'); + try { - text = fs.readFileSync(currentPath).toString(); + code[inputFile] = fs.readFileSync(currentPath).toString(); } catch (err) { domainManager.emitEvent("minifyjs", "statusUpdate", err.toString()); } - var minified = UglifyJS.minify(text, JSON.parse(options)).code; + + var optionsJSON = JSON.parse(options); + var uglyjs = UglifyJS.minify(code, optionsJSON); + var minified = uglyjs.code; + var map = uglyjs.map; + + if (optionsJSON.hasOwnProperty('sourceMap')) { + mapPath = optionsJSON.sourceMap.hasOwnProperty('url') ? pathroot + '/' + optionsJSON.sourceMap.url + : filepath + '.map'; + mkfile(mapPath, customPath, map); + } + return mkfile(filepath, customPath, minified); }