This repository is inspired by this article
As long as the JSON string is only evaluated once, the JSON.parse approach is much faster compared to the JavaScript object literal, especially for cold loads.
Caution!! : I made this plugin for my understanding about AST and babel plugin, so this plugin is not production ready.
This plugin converts from object literal to JSON.parse (example)
// before
const data = { foo: 42, bar: 1337 };
// after
const data = JSON.parse('{"foo":42,"bar":1337}');
$ npm install babel-plugin-object-to-json-parse -D
or
$ yarn add babel-plugin-object-to-json-parse -D
{
"plugins": ["object-to-json-parse"]
}
The minJSONStringSize
option will prevent the plugin from replacing an expression if the length of the JSON string given to JSON.parse
is smaller than minJSONStringSize
. For example, the following ensures all replacements have a string size of at least 1kb.
{
"plugins": [
["object-to-json-parse", {
"minJSONStringSize": 1024
}]
]
}
$ git clone [email protected]:nd-02110114/babel-plugin-object-to-json-parse.git
$ cd babel-plugin-object-to-json-parse
$ yarn install
// example
$ yarn build && yarn example
// test
$ yarn test