Skip to content

Commit aa2b684

Browse files
committed
Use jsesc to escape special characters.
Similar to webpack-contrib/raw-loader#36 we can use jsesc here to deal with special characters in general.
1 parent cda9d7f commit aa2b684

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
const jsesc = require('jsesc');
2+
13
module.exports = function (source) {
24
if (this.cacheable) this.cacheable();
35

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

6-
value = JSON.stringify(value)
7-
.replace(/\u2028/g, '\\u2028')
8-
.replace(/\u2029/g, '\\u2029');
8+
value = JSON.stringify(value);
9+
value = jsesc(value);
910

1011
return `module.exports = ${value}`;
1112
}

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
"repository": {
1414
"type": "git",
1515
"url": "https://github.com/webpack/json-loader.git"
16+
},
17+
"dependencies": {
18+
"jsesc": "^2.5.1"
1619
}
1720
}

0 commit comments

Comments
 (0)