Skip to content

Commit

Permalink
using parse-json for more helpful messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaymathur committed Dec 7, 2017
1 parent 4cc343b commit 2b26f96
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/FSCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -55,7 +56,7 @@ class FSCache {
}

let data = await fs.readFile(cacheFile);
return JSON.parse(data);
return parseJson(data);
} catch (err) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('./fs');
const path = require('path');
const parseJson = require('parse-json');

const existsCache = new Map;

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions test/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;');
Expand All @@ -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);
});
Expand Down Expand Up @@ -130,4 +131,4 @@ describe('hmr', function () {
await sleep(50);
assert.deepEqual(outputs, [3, 10]);
});
});
});
13 changes: 12 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2b26f96

Please sign in to comment.