@rexxars/babel-plugin-inline-json-import v0.3.4
@rexxars/babel-plugin-inline-json-import
A babel plugin that inlines all imports of JSON files straight into your JavaScript files.
Example
Given the following JSON file:
{
"foo": "bar"
}
The plugin will transform the following statement:
import json from './path/to/file.json'
or
const json = require('./path/to/file.json')
to:
const json = {foo: 'bar'}
Simple as that! Both require
and import
are supported.
Not only that, but it also supports destructuring:
const {foo} = require('./path/to/file.json')
// =>
const foo = 'bar'
Installation
Install the plugin through npm
, you will also need babel
installed for
obvious reasons:
$ npm install --save-dev @rexxars/babel-plugin-inline-json-import
Add @rexxars/babel-plugin-inline-json-import
to the list of plugins. If you are using a .babelrc
file, the file should have an entry that looks like this:
{
"plugins": ["@rexxars/inline-json-import"]
}
Usage
This should work straight out of the box. You can configure a RegExp
pattern you want to match on, should you only wish it to apply to certain JSON files:
{
"plugins": [
[
"@rexxars/inline-json-import",
{
"match": "/package.json$",
"matchFlags": "i"
}
]
]
}
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request =)
Fork modifications
This module was originally made by Bryan Yap, and has been forked in order to provide the following improvements:
- Inlining of only destructured items where possible
- Rewritten imports now correctly appear after other imports
- Configurable RegExp for specifying which files to inline
- Handles certain "exotic" import styles without crashing
- Handles loading of JSON files from
node_modules
All modifications were contributed upstream and is awaiting review at the time of writing.
License
This project is licensed under the MIT License - see the LICENSE file for details