Skip to content

Commit

Permalink
Use jsesc to escape special characters.
Browse files Browse the repository at this point in the history
Similar to webpack-contrib/raw-loader#36 we can
use jsesc here to deal with special characters in general.
  • Loading branch information
bmeurer committed Sep 28, 2017
1 parent cda9d7f commit aa2b684
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const jsesc = require('jsesc');

module.exports = function (source) {
if (this.cacheable) this.cacheable();

var value = typeof source === "string" ? JSON.parse(source) : source;

value = JSON.stringify(value)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
value = JSON.stringify(value);
value = jsesc(value);

return `module.exports = ${value}`;
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"repository": {
"type": "git",
"url": "https://github.com/webpack/json-loader.git"
},
"dependencies": {
"jsesc": "^2.5.1"
}
}

0 comments on commit aa2b684

Please sign in to comment.