Aggregating JSON file loader for webpack
Install
npm install multi-json-loader
Usage
./data/places.json
./data/users/account.json
./data/users/profile.json
./irrelevant.whatever
/* Doesn't matter */
example.js
var data = ;// => {// places: {// placesKey: "places value"// },// users: {// account: {// accountKey: "account value"// },// profile: {// profileKey: "profile value"// }// }// }
Note that because I don't understand webpack enough, you have to provide a valid resource file (./irrelevant.whatever
above) even though it won't actually be loaded.
Without webpack
You can also use the loader's functionality independent of webpack.
var multiJsonLoader = ;var messages = multiJsonLoader;console;// => { a: { 'a-key': 'a-value' }, b: { 'b-key': 'b-value' } }
See no-webpack-example
subdirectory.
Options
cwd
The current working directory in which to search. Defaults toprocess.cwd()
.glob
Glob to match files against usingnode-glob
. Defaults to*.json
.
Why not combine the two options?
Path parts in cwd
will not be included in the aggregated JSON object, whereas path parts in glob
will.
var data = ;// => {// c: {// <contents of c directory>// }// }
var data = ;// => {// a: {// b: {// c: {// <contents of c directory>// }// }// }// }
License
MIT