@apparts/config

A config-loading framework

Usage no npm install needed!

<script type="module">
  import appartsConfig from 'https://cdn.skypack.dev/@apparts/config';
</script>

README

+TITLE: Config

+DATE: [2019-02-07 Thu]

+AUTHOR: Philipp Uhl

  • Usage

Storage in an environment variable can be done in two ways:

  1. as a string that is parsable by JSON.parse
  2. as a Base-64 encoded string that decodes to a JSON.parse-parsable string

** Node environment

To require a configuration that is stored in one of the files

  • =config/my-color.json=
  • =config/my-color.js= or the environment variable =MY_COLOR= (note, a dash becomes an underscore and everything is in uppercase):

+BEGIN_SRC sh

const Colors = require('apparts-config').get('my-color');

+END_SRC

** Webpack for web environment

To require a configuration that is stored in the environment variable =MY_COLOR= (note, a dash becomes an underscore and everything is in uppercase):

+BEGIN_SRC sh

const Colors = require('apparts-config').get('my-color');

+END_SRC

In order to create environment variables use the following webpack (v

2.0) configuration options:

+BEGIN_SRC js

module.exports = env => { return { plugins: [ new webpack.DefinePlugin({ 'process.env': { 'MY_COLOR': JSON.stringify(JSON.stringify(require('./src/utils/colors.js'))) } }), ] } };

+END_SRC

** React-Nativ environment

This package is not supported for that kind of environment. In order to use packages that require this package anyways the following configuration is necessary:

It is necessary to create an alias for the module. This requires the module =babel-plugin-module-resolver= to be installed and the following to be present in the file =babel.config.js=:

+BEGIN_SRC js

module.exports = { // ... "plugins": [ ["module-resolver", { "alias": { "apparts-config": "./src/config.js" } }] ] };

+END_SRC

The file =./src/config.js= must be like the following and containing all the configuration:

+BEGIN_SRC js

module.exports.get = (config) => { return { // my configs here configName: { // all my configuration for configName } // ... }[config]; };

+END_SRC