From 2b26f9691d3dc489c509476718fa852b231ffde1 Mon Sep 17 00:00:00 2001 From: Ajay Narain Mathur Date: Thu, 7 Dec 2017 22:33:19 +1100 Subject: [PATCH] using parse-json for more helpful messages --- package.json | 1 + src/FSCache.js | 3 ++- src/utils/config.js | 3 ++- test/hmr.js | 7 ++++--- yarn.lock | 13 ++++++++++++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 7556c4ecc26..1f6589a6b44 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "micromatch": "^3.0.4", "mkdirp": "^0.5.1", "node-libs-browser": "^2.0.0", + "parse-json": "^4.0.0", "physical-cpu-count": "^2.0.0", "postcss": "^6.0.10", "postcss-value-parser": "^3.3.0", diff --git a/src/FSCache.js b/src/FSCache.js index 560f1f93f36..90e46ca4048 100644 --- a/src/FSCache.js +++ b/src/FSCache.js @@ -3,6 +3,7 @@ const path = require('path'); const md5 = require('./utils/md5'); const objectHash = require('./utils/objectHash'); const pkg = require('../package.json'); +const parseJson = require('parse-json'); // These keys can affect the output, so if they differ, the cache should not match const OPTION_KEYS = ['publicURL', 'minify', 'hmr']; @@ -55,7 +56,7 @@ class FSCache { } let data = await fs.readFile(cacheFile); - return JSON.parse(data); + return parseJson(data); } catch (err) { return null; } diff --git a/src/utils/config.js b/src/utils/config.js index 885a1acf462..ea520188017 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -1,5 +1,6 @@ const fs = require('./fs'); const path = require('path'); +const parseJson = require('parse-json'); const existsCache = new Map; @@ -32,7 +33,7 @@ async function load(filepath, filenames, root = path.parse(filepath).root) { return require(configFile); } - return JSON.parse(await fs.readFile(configFile)); + return parseJson(await fs.readFile(configFile)); } return null; diff --git a/test/hmr.js b/test/hmr.js index dd44bc8d809..e9d76970bcf 100644 --- a/test/hmr.js +++ b/test/hmr.js @@ -5,6 +5,7 @@ const rimraf = require('rimraf'); const promisify = require('../src/utils/promisify'); const ncp = promisify(require('ncp')); const WebSocket = require('ws'); +const parseJson = require('parse-json'); describe('hmr', function () { let b, ws; @@ -44,7 +45,7 @@ describe('hmr', function () { fs.writeFileSync(__dirname + '/input/local.js', 'exports.a = 5; exports.b = 5;'); - let msg = JSON.parse(await nextEvent(ws, 'message')); + let msg = parseJson(await nextEvent(ws, 'message')); assert.equal(msg.type, 'update'); assert.equal(msg.assets.length, 1); assert.equal(msg.assets[0].generated.js, 'exports.a = 5; exports.b = 5;'); @@ -61,7 +62,7 @@ describe('hmr', function () { fs.writeFileSync(__dirname + '/input/local.js', 'require("fs"); exports.a = 5; exports.b = 5;'); - let msg = JSON.parse(await nextEvent(ws, 'message')); + let msg = parseJson(await nextEvent(ws, 'message')); assert.equal(msg.type, 'update'); assert.equal(msg.assets.length, 2); }); @@ -130,4 +131,4 @@ describe('hmr', function () { await sleep(50); assert.deepEqual(outputs, [3, 10]); }); -}); \ No newline at end of file +}); diff --git a/yarn.lock b/yarn.lock index 2195538bd2d..4731dede0f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1521,7 +1521,7 @@ errno@^0.1.1, errno@^0.1.4: dependencies: prr "~0.0.0" -error-ex@^1.2.0: +error-ex@^1.2.0, error-ex@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc" dependencies: @@ -2476,6 +2476,10 @@ jsesc@~0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d" +json-parse-better-errors@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.1.tgz#50183cd1b2d25275de069e9e71b467ac9eab973a" + json-schema-traverse@^0.3.0: version "0.3.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340" @@ -3234,6 +3238,13 @@ parse-json@^2.2.0: dependencies: error-ex "^1.2.0" +parse-json@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0" + dependencies: + error-ex "^1.3.1" + json-parse-better-errors "^1.0.1" + parseurl@~1.3.2: version "1.3.2" resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3"