Replace string tokens in the contents of a file.
npm install string-replace-webpack-plugin`` javascript`
var StringReplacePlugin = require("string-replace-webpack-plugin");
module.exports = {
module: {
loaders: [
// configure replacements for file patterns
{
test: /index.html$/,
loader: StringReplacePlugin.replace({
replacements: [
{
pattern: //ig,
replacement: function (match, p1, offset, string) {
return secrets.web[p1];
}
}
]})
}
]
},
plugins: [
// an instance of the plugin must be present
new StringReplacePlugin()
]
}
This allows for arbitrary strings to be replaced as part of the module build process. The original intent is to replace API
keys in modules prior to deployment.
` javascript`
StringReplacePlugin.replace([nextLoaders: string], options, [prevLoaders: string])
* nextLoaders loaders to follow the replacementoptions
* replacements
* disables the pluginpattern
* a regex to match against the file contentsreplacement
* an ECMAScript string replacement functionprevLoaders` loaders to apply prior to the replacement
*
MIT (http://www.opensource.org/licenses/mit-license.php)