Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
fix: add stringify option to output JSON object as string (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
HairyRabbit authored and joshwiens committed Jul 22, 2017
1 parent 2993ea0 commit dbf1fa5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ import json from 'file.json';
import json from 'json-loader!file.json';
```



### Options

#### `stringify`

By default, the json-loader will output the json object, set this query parameter to 'true' can output the json object as a string, e.g. `require('json-loader?stringify!../index.json')`.




<h2 align="center">Maintainer</h2>

<table>
Expand All @@ -76,6 +87,7 @@ import json from 'json-loader!file.json';
</tbody>
</table>


[npm]: https://img.shields.io/npm/v/json-loader.svg
[npm-url]: https://npmjs.com/package/json-loader

Expand Down
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/*
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
*/
var loaderUtils = require('loader-utils');

module.exports = function(source) {
this.cacheable && this.cacheable();
var value = typeof source === "string" ? JSON.parse(source) : source;
this.value = [value];
return "module.exports = " + JSON.stringify(value) + ";";
var value = typeof source === "string" ? JSON.parse(source) : source;
var options = loaderUtils.getOptions(this) || {};
value = JSON.stringify(value)
value = options.stringify ? `'${value}'` : value
var module = this.version && this.version >= 2 ? `export default ${value};` : `module.exports = ${value};`;
return module
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"repository": {
"type": "git",
"url": "https://github.com/webpack/json-loader.git"
},
"dependencies": {
"loader-utils": "^1.0.3"
}
}

0 comments on commit dbf1fa5

Please sign in to comment.