1
\$\begingroup\$

This may be better suited on Stackoverflow, in which case, please let me know and I will move it over...

Background

Consider the following file structure:

/config
│   index.js
│   app.js
│   api.js
│   ...

The purpose of the above is to provide global configuration throughout a static frontend application, including within the webpack build configuration.

My original intention was to provide a global function called config which would behave much like the Laravel config function.

For example:

config('app.name') would return the name property from the app.js configuration file

Instead, my solution simply provides an object which is used like so:

  • config.app.name
  • config.api.url

My Code

The way I am doing this is extremely simple:

/config/index.js

module.exports = {
    app: require('./app'),
    company: require('./company'),
    api: require('./api'),
    locales: require('./locales')
};

/config/app.js

module.exports = {
    name: 'Foobar',
    // etc...
};

Example Usage

import config from './config';

console.log(config.app.name);

My Question

Whilst I am not bothered whether /config provides an object, or a function, I am unsure as to whether the solution I have put together is the cleanest way of achieving this...

I appreciate this question might be considered 'subjective' but does anyone have any alternative ways in which they would go about doing this?

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.