Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
fix(index): escape invalid characters (#43)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
joeheyming authored and evilebottnawi committed Nov 17, 2017
1 parent e234bde commit 83f6541
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions 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;
}

0 comments on commit 83f6541

Please sign in to comment.