Don't use JSON.parse when json is a string

This commit is contained in:
Tobias Koppers 2019-12-20 12:47:03 +01:00
parent d16abb3294
commit 567ff7f026
1 changed files with 3 additions and 1 deletions

View File

@ -122,7 +122,9 @@ class JsonGenerator extends Generator {
// Use JSON because JSON.parse() is much faster than JavaScript evaluation // Use JSON because JSON.parse() is much faster than JavaScript evaluation
const jsonStr = stringifySafe(finalJson); const jsonStr = stringifySafe(finalJson);
const jsonExpr = const jsonExpr =
jsonStr.length > 20 ? `JSON.parse(${JSON.stringify(jsonStr)})` : jsonStr; jsonStr.length > 20 && typeof finalJson === "object"
? `JSON.parse(${JSON.stringify(jsonStr)})`
: jsonStr;
source.add(`${module.moduleArgument}.exports = ${jsonExpr};`); source.add(`${module.moduleArgument}.exports = ${jsonExpr};`);
return source; return source;
} }