Inline values from a JSON file eg. a config file
Does not work if the argument to
require()
is an identifier or a template literal
config.json:
{
"foo": "bar"
}
In
var foo = require('config').foo;
Out
var foo = "bar";
$ npm install babel-plugin-inline-json --save-dev
.babelrc
{
"plugins": [["inline-json", {"matchPattern": "config"}]]
}
$ babel --plugins inline-json script.js
require("babel-core").transform("code", {
"plugins": [["inline-json", {"matchPattern": "config"}]]
});
this is based on @mwilliams-change's babel-plugin-inline-json-config-values.