https://github.com/unmyke/app-json-config-loader
JSON configuration loader for Webpack
https://github.com/unmyke/app-json-config-loader
javascript loader webpack
Last synced: 2 months ago
JSON representation
JSON configuration loader for Webpack
- Host: GitHub
- URL: https://github.com/unmyke/app-json-config-loader
- Owner: unmyke
- License: mit
- Created: 2019-04-09T08:58:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:43:05.000Z (over 3 years ago)
- Last Synced: 2025-03-22T01:37:08.628Z (over 1 year ago)
- Topics: javascript, loader, webpack
- Language: JavaScript
- Homepage:
- Size: 2.43 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# app-json-config-loader
Loads configuration as declared in the `json`-file scopes. Loader uses [get-json-config] as core to get scopes of configuration you stored. Tranform list of scopes of configuration to configuration object at build by [webpack] allows you to divide your configuration by scopes (for instance, configuration for api, database, logging modules), define which scopes must be shared between client and server and which must not be shared (database credentails must not to included to cleint bundle).
## Requirements
This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.
## Getting Started
To begin, you'll need to install `app-json-config-loader`:
```console
$ npm install --save-dev app-json-config-loader
```
Then add the loader to your `webpack` config. For example:
```json
// config/api.json
{
"production": {
"endPoint": "/api/v1"
},
"development": {
"endPoint": "/dev_api/"
},
"test": {
"endPoint": "/dev_api/"
}
}
```
```json
// config/database.json
{
"production": {
"host": "db.expamle.host",
"port": 27017,
"username": "produsername",
"password": "prodpassword"
},
"development": {
"host": "localhost",
"port": 27017,
"username": "devusername",
"password": "devpassword"
},
"test": {
"host": "localhost",
"port": 27117,
"username": "testusername",
"password": "testpassword"
}
}
```
```json
// src/client/configs.json
["api"]
```
```json
// src/server/configs.json
["api", "database"]
```
```js
// webpack.server.config.js
module.exports = (env) => {
module: {
rules: [
{
test: /server\/configs\.json$/,
use: [
{
loader: "app-json-config-loader",
options: {
env, // default process.env.NODE_ENV || "development",
configPath: "./config" // default "./config",
}
},
],
},
],
},
};
```
```js
// src/server/entry.js
const config = require('./configs.json');
// if development mode config will be:
// {
// api:
// { endpoint: '/dev_api' },
// database: {
// host: "localhost",
// port: 27017,
// username: "devusername",
// password: "devpassword"
// }
// }
```
```js
// src/client/entry.js
const config = require('./configs.json');
// if production mode config will be:
// {
// api:
// { endpoint: '/dev_api' },
// }
```
And run `webpack` via your preferred method.
## License
#### [MIT](./LICENSE)
[get-json-config]: https://github.com/unmyke/get-json-config
[webpack]: https://webpack.js.org