diff --git a/packages/json/src/index.js b/packages/json/src/index.js index 432410c0a..9767e83ca 100755 --- a/packages/json/src/index.js +++ b/packages/json/src/index.js @@ -11,15 +11,23 @@ export default function json(options = {}) { transform(json, id) { if (id.slice(-5) !== '.json' || !filter(id)) return null; - return { - code: dataToEsm(JSON.parse(json), { - preferConst: options.preferConst, - compact: options.compact, - namedExports: options.namedExports, - indent - }), - map: { mappings: '' } - }; + try { + const parsed = JSON.parse(json); + return { + code: dataToEsm(parsed, { + preferConst: options.preferConst, + compact: options.compact, + namedExports: options.namedExports, + indent + }), + map: { mappings: '' } + }; + } catch (err) { + const message = 'Could not parse JSON file'; + const position = parseInt(/[\d]/.exec(err.message)[0], 10); + this.warn({ message, id, position }); + return null; + } } }; } diff --git a/packages/json/test/test.js b/packages/json/test/test.js index 071b4ff0b..6849d60e9 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -73,14 +73,21 @@ test('handles JSON objects with no valid keys (#19)', async (t) => { }); test('handles garbage', async (t) => { - const bundle = async () => - rollup({ - input: 'fixtures/garbage/main.js', - plugins: [json()] - }); - - const error = await t.throwsAsync(bundle); - t.is(error.message.indexOf('Unexpected token o'), 0); + const warns = []; + + await rollup({ + input: 'fixtures/garbage/main.js', + plugins: [json()], + onwarn: (warning) => warns.push(warning) + }).catch(() => {}); + + const [{ message, id, position, plugin }] = warns; + + t.is(warns.length, 1); + t.is(plugin, 'json'); + t.is(position, 1); + t.is(message, 'Could not parse JSON file'); + t.regex(id, /(.*)bad.json$/); }); test('does not generate an AST', async (t) => {