Load any file you want to load in any format.
The design philosophy behind this package is allowing developer to use any language he/she preferred to write configuration files.
And this utility is mean to be used by packages that require configuration files.
It's not recommended to use this for loading program files.
npm i load-it -s
Load It supports a wide variety of formats.
/foo
├── aries.json
├── taurus.js
├── gemini.ts
├── leo.cson
├── virgo.coffee
└── libra.yaml
const myJsonModule = loadIt('./aries.json');
const myJsModule = loadIt('./taurus.js');
const myTsModule = loadIt('./gemini.ts');
const myCsonModule = loadIt('./leo.cson');
const myCoffeeModule = loadIt('./virgo.coffee');
const myYamlModule = loadIt('./libra.yaml');
Most of times, you don't need to specify the extension. Having Load It figure out the extension and format for you.
/foo
├── .babelrc.json
├── package.json
└── server.js
const babelConfig = loadIt('./.babelrc'); // resolves to the json config
/foo
├── .babelrc.yaml
├── package.json
└── server.js
const babelConfig = loadIt('./.babelrc'); // resolves to the yaml config
If a directory with the name user specified exists, Load It tries to load the index file.
/foo
└── myModule
└── index.ts
const babelConfig = loadIt('./myModule'); // resolves to 'myModule/index.ts'
If a file exists but without extension, Load It treats it as a data file. This means, Load It tries to load it as 'json', 'cson' or 'yaml'. If it can't be parsed by any parser, it throws an error. This enables configuration files without extension to be written in any format.
/foo
└── .eslintrc
// figure out the file type and load it
const babelConfig = loadIt('./.eslintrc');
Returns the module exported by the file.
MIT © Zhang Kai Yu