5 ms·
Is there another way to provide strong types for a JSON file? I believe vscode uses schemas from http://schemastore.org/ http://schemastore.org/ for many confi
by devongovett 7y ago
Is there another way to provide strong types for a JSON file?
I believe vscode uses schemas from http://schemastore.org/ http://schemastore.org/ for many config files already.
- orf 7y agoIt wasn't clear until now that the configuration is done through JSON rather than being configurable through code (i.e TypeScript). Urgh, JSON for configuration. That's going to be horrible.
- devongovett 7y agoJSON is much more static and predictable for configuration. Config through a real programming language was a mistake that many tools made unfortunately. Static configuration has several nice properties including cacheability and simplicity that cannot be guaranteed in a full programming language. Just look at webpack.config.js for an example of the opposite.
- orf 7y agoThat's all totally awesome and cool, right up until the moment you want to add a comment to your configuration. Because you know, it's configuration, and that requires comments. Quite a few tools went from just JSON configuration files to supporting both .js and .json. Regarding caching, perhaps I'm mistaken but what's the issue there? Two JSON documents can be equivalent with different bytes so you need to re-parse the configuration file fairly often anyway. So what's the difference with just re-evaluating a .js configuration file and caching the output? If someone wants to put a `if (random() > 0.1)` statement in their config then that's their problem.
- hoten 7y agojsonp
- devongovett 7y agoWe use json5, which allows comments. You cannot statically cache the results of a .js file - it might include objects or functions that cannot be serialized, and might cause side effects that parcel cannot know about. If we supported .js config files, we would have to intentionally limit what you can return to basically just JSON, so you wouldn’t get much benefit from it and it would be hard to enforce.
- orf 7y agoOh right! Thank you for your clarifying comments, this does sound pretty interesting. Sorry for being flippant before. Looking forward to reading about the new release!
- manigandham 7y agoAll configuration is static. The advantage of using JS (or a language file) is that you run it to generate the appropriate config for that moment and environment. That need won't ever go away, so not supporting means we're back to users generating their own "static" config files in a separate process. Nothing has been saved, only made more complicated.
- ironmagma 7y agoIf you define the types in TS, you can use https://github.com/YousefED/typescript-json-schema https://github.com/YousefED/typescript-json-schema to automatically generate a JSON schema from it. That way, configurations can be specified either in an executable JS module or in a JSON file.
- devongovett 7y agoYep, we’ll definitely use a tool like that. We already have types in Flow for all of Parcel 2, so should be possible to do something similar.
- orta 7y agoYep, a schema would work fine for this (and you can generate it from types) You can test them locally using vscode using the "$schema" field on the root obj, here's an example PR doing all that: https://github.com/danger/peril/pull/281 https://github.com/danger/peril/pull/281 You can either have users add that schema reference in the JSON (throw it in the template) or ship a vscode extension which connects them