From 83f65414e67496c44ac3b412d1b3c9fed88d4b22 Mon Sep 17 00:00:00 2001 From: Joe Heyming Date: Fri, 17 Nov 2017 01:26:45 -0800 Subject: [PATCH] fix(index): escape invalid characters (#43) * Handle special whitespace characters. If your raw text has a string with Line separator or Paragraph separator, they need to be handled outside of JSON.stringify: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript The fix here is to replace those characters as directed in the MDN article above. I also changed the formatting from tabs to 2 spaces :-p * Address code review. * Rename some variables. * Remove cacheable. Turns out cacheable is only used in webpack 1. So we are removing it. --- index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index a59e712..54d73c0 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,13 @@ /* - MIT License http://www.opensource.org/licenses/mit-license.php - Author Tobias Koppers @sokra + MIT License http://www.opensource.org/licenses/mit-license.php + Author Tobias Koppers @sokra */ -module.exports = function(content) { - this.cacheable && this.cacheable(); - this.value = content; - return "module.exports = " + JSON.stringify(content); +module.exports = function(source) { + this.value = source; + + var json = JSON.stringify(source) + .replace(/\u2028/g, '\\u2028') + .replace(/\u2029/g, '\\u2029'); + + return "module.exports = " + json; }