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

Fix #43. Add string options to output json object string. #45

Merged
merged 3 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 Down Expand Up @@ -98,6 +109,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;
this.value = [value];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but this can be removed since it was needed in webpack =< v0.8.0 only

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. this.cacheable isn't needed either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.value = [value] can be removed like this.cacheable, not needed anymore 😛

var query = loaderUtils.getOptions(this) || {};
var outprefix = this.version && this.version >= 2 ? "export default " : "module.exports = ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query => options

const options = loaderUtils.getOptions(this) || {}

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

value = options.stringify ? `${JSON.stringify(value)}` : JSON.stringify(value)

const module = this.version >= 2  ? `export default ${value}` : `module.exports = ${value}` 

return module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

var out = JSON.stringify(value);
return outprefix + (query.stringify ? "'" + out + "'" : out) + ";";
}
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"
}
}