-
-
Notifications
You must be signed in to change notification settings - Fork 90
Conversation
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
This is related to Issue #42 |
This CI failure has nothing to do with my changes |
|
index.js
Outdated
this.cacheable && this.cacheable(); | ||
this.value = content; | ||
|
||
var stringified = JSON.stringify(content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var stringified = ...
=> content = ...
and maybe rename content
to json
please :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the content is the raw text of the file. It's not json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, agreed src
as argument and var json = JSON.stringify(src)
is better :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
index.js
Outdated
this.value = content; | ||
|
||
var stringified = JSON.stringify(content) | ||
.replace(/\u2028/g, '\\u2028') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent chained methods with 2 spaces please
json = ...
.method()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joeheyming Thx
index.js
Outdated
this.value = content; | ||
return "module.exports = " + JSON.stringify(content); | ||
module.exports = function(source) { | ||
this.cacheable && this.cacheable(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't support webpack 1.x. Cachable can be removed at this point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
Turns out cacheable is only used in webpack 1. So we are removing it.
.replace(/\u2028/g, '\\u2028') | ||
.replace(/\u2029/g, '\\u2029'); | ||
|
||
return "module.exports = " + json; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just return "module.exports = " + '"' + source + '"'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the problem I had was that I was importing a file which had these special characters and it turns out that they break JSON. See the MDN article I posted above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I know. But why converting to json is better than wrapping the string with "
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really, it's a matter of choice. I've been used to using json stringify for things like this where code gets evaluated. But yeah, you are right, it is the same.
Notable Changes
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
Issues