Last active
January 21, 2021 14:25
-
-
Save fivethreeo/e1f8bef6ddfd18fffb94d38692394d38 to your computer and use it in GitHub Desktop.
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
/** | |
* The passed list of environment variables will be removed from the nodejs | |
* instance of webpack.DefinePlugin, so they can be resolved dynamically at | |
* runtime. | |
* @example | |
* // Include this in the plugins array exported by razzle.config.js | |
* const nodeRuntimeVarsPlugin = createRazzlePluginNodeRuntimeVars('PORT', 'HOST'); | |
* @param {String} ...nodeRuntimeVars | |
* @return {Function} | |
*/ | |
const createRazzlePluginNodeRuntimeVars = (...nodeRuntimeVars) => ( | |
config, | |
{ target, dev }, | |
webpack, | |
) => { | |
if (target !== 'node') { | |
// Don't change dotenv build-time behavior for the client bundle | |
return config; | |
} | |
const definePluginIndex = config.plugins.findIndex( | |
plugin => plugin instanceof webpack.DefinePlugin && plugin.definitions, | |
); | |
if (typeof definePluginIndex !== 'undefined') { | |
const nodeRunetimeVarsBlacklist = nodeRuntimeVars.map( | |
name => `process.env.${name}`, | |
); | |
const filteredDefinitions = config.plugins[definePluginIndex].definitions.entries().reduce((result, entry)=>{ | |
if (!nodeRunetimeVarsBlacklist.includes(entry[0])) { | |
result[entry[0]] = entry[1]; | |
} | |
}, {}); | |
config.plugins[definePluginIndex] = new webpack.DefinePlugin( | |
filteredDefinitions, | |
); | |
} | |
return 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
'use strict'; | |
const createRazzlePluginNodeRuntimeVars = require('razzle-plugin-node-runtime-vars.js'); | |
module.exports = { | |
plugins: [createRazzlePluginNodeRuntimeVars('PORT', 'HOST')], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In razzle canary