From 2aa8b944f6038c76f5ae3bef33d86d5b2b81281b Mon Sep 17 00:00:00 2001 From: aleclarson Date: Fri, 8 Jun 2018 14:58:18 -0400 Subject: [PATCH] fix: invalid filenames in `sources` of source maps When the parsed files have no associated filenames (eg: a string or array of strings was passed to the `minify` function), the `sources` array of the generated sourcemap must only contain null values. Currently, it contains the stringified indexes of the array. For example, passing a string to `minify` results in a `sources` array of `["0"]`, which breaks the sourcemap. Also, this PR adds the `sourceMap.json` option. When explicitly set to `false`, the sourcemap is *not* stringified. --- lib/minify.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/minify.js b/lib/minify.js index d84f6825..0da92ac4 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -111,6 +111,7 @@ function minify(files, options) { content: null, filename: null, includeSources: false, + json: true, root: null, url: null, }, true); @@ -207,7 +208,13 @@ function minify(files, options) { toplevel.print(stream); result.code = stream.get(); if (options.sourceMap) { - result.map = options.output.source_map.toString(); + result.map = options.output.source_map.get().toJSON(); + if (Array.isArray(files)) { + result.map.sources = [null]; + } + if (options.sourceMap.json !== false) { + result.map = JSON.stringify(result.map); + } if (options.sourceMap.url == "inline") { result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); } else if (options.sourceMap.url) {