Skip to content

Instantly share code, notes, and snippets.

@the0neWhoKnocks
Last active March 16, 2018 18:08
Show Gist options
  • Save the0neWhoKnocks/c692e1f0c3e932c10b857ebe757d8595 to your computer and use it in GitHub Desktop.
Save the0neWhoKnocks/c692e1f0c3e932c10b857ebe757d8595 to your computer and use it in GitHub Desktop.
How to use a JS configuration when a module only accepts JSON

How to use a JS configuration when a module only accepts JSON

There are times when you're using a Node module that can only be configured via JSON. When it comes to comments or setting up dynamic behavior, JSON falls flat. This will describe how (with some slight alterations) to get the best of both worlds.

The examples demonstrate settings for nodemon within a monorepo.

  • package.json - The watch:server command will first run conf.nodemon.js and then start up nodemon with the config that was generated.
  • conf.nodemon.js - Defines the configuration for the module with any logic that needs to be applied, then writes it out to a JSON file.
const { resolve } = require('path');
const { writeFileSync } = require('fs');
const ROOT = resolve(__dirname, '../../');
const conf = {
execMap: {
js: 'babel-node',
},
ignoreRoot: [
'.git',
],
verbose: true,
watch: [
// detects server changes
`${ROOT}/node_modules/@namespace/server-module/dist/`,
// detects changes from components
`${ROOT}/packages/app-module/webpack-assets.json`,
// detects changes to the app
`${ROOT}/packages/app-module/src/`,
],
};
try {
writeFileSync('/tmp/nodemon.json', `${JSON.stringify(conf)}\n`);
} catch (err) {
throw err;
}
"scripts": {
"watch:server": "node ./conf.nodemon.js && nodemon --config /tmp/nodemon.json ./src/index.js"
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment