All Projects → nissy-dev → Babel Plugin Object To Json Parse

nissy-dev / Babel Plugin Object To Json Parse

Licence: mit
This plugin converts object literal to JSON.parse

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin Object To Json Parse

Catom
A 0 runtime CSS in JS library
Stars: ✭ 84 (-44.37%)
Mutual labels:  babel-plugin
Babel Plugin I18next Extract
Babel plugin that statically extracts i18next and react-i18next translation keys.
Stars: ✭ 93 (-38.41%)
Mutual labels:  babel-plugin
Babel Plugin Runtyper
⚡️ Runtime type-checker for JavaScript
Stars: ✭ 117 (-22.52%)
Mutual labels:  babel-plugin
Modify Babel Preset
💫 Create a modified babel preset based on an an existing preset.
Stars: ✭ 85 (-43.71%)
Mutual labels:  babel-plugin
Jsx Control Statements
Neater If and For for React JSX
Stars: ✭ 1,305 (+764.24%)
Mutual labels:  babel-plugin
Babel Plugin Recharts
Stars: ✭ 108 (-28.48%)
Mutual labels:  babel-plugin
Babel Plugin Captains Log
Babel plugin that injects helpful details into console statements
Stars: ✭ 80 (-47.02%)
Mutual labels:  babel-plugin
Extract React Types
One stop shop to document your react components.
Stars: ✭ 141 (-6.62%)
Mutual labels:  babel-plugin
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (-39.74%)
Mutual labels:  babel-plugin
Babel Plugin Prismjs
A babel plugin to use PrismJS with standard bundlers.
Stars: ✭ 114 (-24.5%)
Mutual labels:  babel-plugin
Babel Plugin Stateful Functional React Components
Stateful functional React components without runtime overhead
Stars: ✭ 86 (-43.05%)
Mutual labels:  babel-plugin
Idx.macro
a 'babel-macros' version of 'babel-plugin-idx'
Stars: ✭ 90 (-40.4%)
Mutual labels:  babel-plugin
Js Proposal Algebraic Effects
📐Let there be algebraic effects in JS
Stars: ✭ 110 (-27.15%)
Mutual labels:  babel-plugin
Compiled
A familiar and performant compile time CSS-in-JS library for React.
Stars: ✭ 1,235 (+717.88%)
Mutual labels:  babel-plugin
Babel Plugin Polished
Compile polished helper functions at build time
Stars: ✭ 133 (-11.92%)
Mutual labels:  babel-plugin
Babel Plugin Optimize Clsx
Babel plugin to optimize the use of clsx, classnames, and other libraries with a compatible API
Stars: ✭ 80 (-47.02%)
Mutual labels:  babel-plugin
Babel Plugin Jsx Adopt
Stars: ✭ 94 (-37.75%)
Mutual labels:  babel-plugin
Babel Plugin Transform Incremental Dom
Turn JSX into IncrementalDOM
Stars: ✭ 146 (-3.31%)
Mutual labels:  babel-plugin
Babel Plugin Transform Typescript Metadata
Babel plugin to emit decorator metadata like typescript compiler
Stars: ✭ 142 (-5.96%)
Mutual labels:  babel-plugin
Babel Plugin Mobx Deep Action
Reduces `action` and `runInAction` boilerplates
Stars: ✭ 110 (-27.15%)
Mutual labels:  babel-plugin

babel-plugin-object-to-json-parse 🚀

CircleCI License: MIT npm version

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.

Object to JSON.parse

This plugin converts from object literal to JSON.parse

// before
const data = { foo: 42, bar: 1337 };

// after
const data = JSON.parse('{"foo":42,"bar":1337}');

How to use

Install

$ npm install babel-plugin-object-to-json-parse -D
or
$ yarn add babel-plugin-object-to-json-parse -D

setup .babelrc

{
  "plugins": ["object-to-json-parse"]
}

Options

minJSONStringSize (number, defaults to 1024)

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
    }]
  ]
}

Caution!!

this plugin may not be production ready

I just made this plugin for my understanding about AST and babel plugin.

this plugin doesn't support partially JSON expression

I decided not to support partially JSON expression like below.

Partially JSON expressions such as [notValid, {isValid:true}] ensuring {isValid:true} is transformed.

const data = { bar: invalid_object, foo: 'foo' }
↓
const data = { bar: invalid_object, JSON.parse('{"foo": "foo"}')}

This is because I think most large objects are not partially JSON expressions. JSON.parse() is much faster in the case that object is 10 kB or larger. Converting small object to JSON.parse expression is not meaningful.

this plugin produces output that only works in modern environments (e.g. Node.js v10+)

I don't care about some backwards compatibilities like this issue.

Development

Any contributions are welcomed from everyone!!

Setup

$ git clone [email protected]:nissy-dev/babel-plugin-object-to-json-parse.git
$ cd babel-plugin-object-to-json-parse
$ yarn install

Tips

// build
$ yarn build

// test
$ yarn test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].