From c1cab1195788a20d0d3174e6c3cebe3e5a74e2c7 Mon Sep 17 00:00:00 2001 From: yuffiy Date: Tue, 14 Mar 2017 14:06:40 +0800 Subject: [PATCH 1/3] Fix #43. Add `string` options to output json object string. --- README.md | 12 ++++++++++++ index.js | 17 +++++++++++------ package.json | 3 +++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 99bd3b4..ce0b5b4 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,17 @@ import json from 'file.json'; import json from 'json-loader!file.json'; ``` + + +### Options + +#### `string` + +By default, the json-loader will output the json object, set this query parameter to 'ture' can output the json object as a string, e.g. `require('json-loader?string!../index.json')`. It's useful work with `ExtractTextPlugin` + + + +

Maintainer

@@ -98,6 +109,7 @@ import json from 'json-loader!file.json';
+ [npm]: https://img.shields.io/npm/v/json-loader.svg [npm-url]: https://npmjs.com/package/json-loader diff --git a/index.js b/index.js index bb7b2c0..36c0267 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,15 @@ /* - 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) + ";"; + this.cacheable && this.cacheable(); + var value = typeof source === "string" ? JSON.parse(source) : source; + this.value = [value]; + var query = loaderUtils.getOptions(this) || {} + if(query.string) + return "module.exports = `" + JSON.stringify(value) + "`;"; + return "module.exports = " + JSON.stringify(value) + ";"; } diff --git a/package.json b/package.json index 71422c5..75593f0 100644 --- a/package.json +++ b/package.json @@ -7,5 +7,8 @@ "repository": { "type": "git", "url": "https://github.com/webpack/json-loader.git" + }, + "dependencies": { + "loader-utils": "^1.0.3" } } From 008febec0a68f3ddaf7b64e1ada85d5f0ffb79bb Mon Sep 17 00:00:00 2001 From: yuffiy Date: Wed, 15 Mar 2017 10:09:25 +0800 Subject: [PATCH 2/3] Remove this.cacheable() and rename `string` to `stringify`. --- README.md | 4 ++-- index.js | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ce0b5b4..7a0436a 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,9 @@ import json from 'json-loader!file.json'; ### Options -#### `string` +#### `stringify` -By default, the json-loader will output the json object, set this query parameter to 'ture' can output the json object as a string, e.g. `require('json-loader?string!../index.json')`. It's useful work with `ExtractTextPlugin` +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')`. diff --git a/index.js b/index.js index 36c0267..06e6860 100644 --- a/index.js +++ b/index.js @@ -5,11 +5,10 @@ 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]; - var query = loaderUtils.getOptions(this) || {} - if(query.string) - return "module.exports = `" + JSON.stringify(value) + "`;"; - return "module.exports = " + JSON.stringify(value) + ";"; + var query = loaderUtils.getOptions(this) || {}; + var outprefix = this.version && this.version >= 2 ? "export default " : "module.exports = "; + var out = JSON.stringify(value); + return outprefix + (query.stringify ? "'" + out + "'" : out) + ";"; } From e60d4d34b3cd2346d798ccb8d75bee36060761db Mon Sep 17 00:00:00 2001 From: yuffiy Date: Wed, 15 Mar 2017 13:04:29 +0800 Subject: [PATCH 3/3] Remove `[value]`, change `query` to `options`. --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 06e6860..5cadd7d 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,9 @@ var loaderUtils = require('loader-utils'); module.exports = function(source) { var value = typeof source === "string" ? JSON.parse(source) : source; - this.value = [value]; - var query = loaderUtils.getOptions(this) || {}; - var outprefix = this.version && this.version >= 2 ? "export default " : "module.exports = "; - var out = JSON.stringify(value); - return outprefix + (query.stringify ? "'" + out + "'" : out) + ";"; + 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 }