1.0.0 • Published 4 years ago
app-json-config-loader v1.0.0
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
:
$ npm install --save-dev app-json-config-loader
Then add the loader to your webpack
config. For example:
// config/api.json
{
"production": {
"endPoint": "/api/v1"
},
"development": {
"endPoint": "/dev_api/"
},
"test": {
"endPoint": "/dev_api/"
}
}
// 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"
}
}
// src/client/configs.json
["api"]
// src/server/configs.json
["api", "database"]
// 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",
}
},
],
},
],
},
};
// 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"
// }
// }
// 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.