Last active
June 19, 2020 19:58
-
-
Save arnellebalane/4051fb87b8122b52a2ae83bf8c26abfe to your computer and use it in GitHub Desktop.
managing secret configs with webpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# no need to ignore entire "config" directory | |
secrets.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this file: config/index.js | |
// secrets: config/secrets.json | |
// The values in this object will be the default config values, | |
// and will be overriden with the values (of the same keys) | |
// defined in the secrets.json file. | |
const config = { | |
host: 'http://localhost:3000/' | |
}; | |
try { | |
// This tries to load the secret configs. In the event that the | |
// file is missing, we will just get an empty object. | |
const secrets = require('./secrets.json'); | |
// Override default config with values from the secrets file. | |
Object.assign(config, secrets); | |
} catch (e) {} | |
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"host": "https://example.com" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const webpack = require('webpack'); | |
module.exports = { | |
// ...other webpack configs | |
externals: (context, request, callback) => { | |
// Let's say our sensitive configs are stored in the file 'config/secrets.json'. | |
// That file is ignored and not committed into the repo. | |
if (request === './secrets.json') { | |
fs.stat(path.join(context, request), (err, stat) => { | |
// We check if that file secrets file exists in the filesystem. If not, | |
// we "mock" it out with an empty object. | |
if (err) { | |
return callback(null, '{}'); | |
} | |
callback(); | |
}); | |
} else { | |
callback(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment