Throw on invalid JSON with array as top-level structure
Opened this issue · 4 comments
This plugin is really simple and good. Thank you for your efforts!
But it can be even better if it would check the validity of JSON to insert a little bit better. For example, insertion of JSON with array as top-level structure is available. But contents of JSON is just concatenated to bundled script (not squashed as will be with object as top-level element). preferConst
isn't working as well. Neither validation of supplied structure.
It would be better to, at least, warn the user that he supplied something that is not fully supported, or just throw an error.
It should just be turning [...]
into export default [...]
— is that not happening? Any valid JSON should be supported, but there are optimisations for objects.
It maybe happening but problem is with what I get in bundle (note that it is invalid JSON and not const):
var log = [
{
"date": "2017-06-03",
"type": "feat",
"desc": "Add changelog window with info on changes",
"pull": "22"
}
{
"date": "2017-05-29",
"type": "perf",
"desc": "Improve performance of grid processing",
"pull": "21"
}
]
;
Got this with import statement:
import log from '../changelog.json'
Exploring context of problem brings up some interesting stuff though: I can import whatever I want in that JSON. For example, I can get this in bundled file:
var log = Whoops, just some literals
;
With this as ../changelog.json
(that is right, it is not even a JSON!):
Whoops, just some literals
This is definitely a problem.
Used rollup.config.js
:
import cleanup from 'rollup-plugin-cleanup'
import replace from 'rollup-plugin-replace'
import json from 'rollup-plugin-json'
export default {
entry: 'src/index.js',
plugins: [
json({
preferConst: true
}),
replace({
delimiters: ['__', '__'],
BUILD: process.env.TRAVIS_BUILD_NUMBER || 'X-LOCAL'
}),
cleanup()
],
format: 'iife',
dest: 'dist/index.js'
}
Ah, okay. If the .json file doesn't contain JSON then that's a different matter. I've just released a new version, 2.3.0 — now, it will always call JSON.parse
(before that was only happening with objects), so if JSON is malformed you'll get an error at bundle time, pointing to the file with the error.
Super! Thank you!