I managed to work out how to do this (although I'm not sure it's the best solution).
I am making use of the webpack starter kit from here: angular2-webpack-starter
- I first installed string-replace-webpack-plugin using npm
I then created a properties.json file with contents like this:
{
"branding": {
"title": "My awesome website",
"colour": "#00cc00"
}
}
I then added this at the top of my webpack.common.js file:
const properties = require('./properties.json');
const StringReplacePlugin = require('string-replace-webpack-plugin');
const string_replacement_loader = StringReplacePlugin.replace({
replacements: [
{
pattern: /\${(.*)}/g,
replacement: function (match, p1, offset, string) {
return eval('properties.' + p1);
}
}
]});
I then added this in the loaders section of my webpack.common.js file:
{
test: /\.css$/,
loaders: [
'to-string-loader',
'css-loader',
string_replacement_loader
]
},
{
test: /\.html$/,
loaders: [
'raw-loader',
string_replacement_loader
],
exclude: [helpers.root('src/index.html')]
},
I then added this to the plugins section of my webpack.common.js file:
new StringReplacePlugin()
I was then able to insert tokens like ${branding.title} and ${branding.colour} into my html and css files and everything seems to come out as expected.
There is one downside to this solution in that (if you haven't already noticed) the above excludes src/index.html. I got around this by adjusting the definition of METADATA in webpack.common.js as follows:
const METADATA = {
title: 'Angular2 Webpack Starter by @gdi2290 from @AngularClass',
baseUrl: '/',
isDevServer: helpers.isWebpackDevServer(),
properties: properties
};
I was then able to refer to my properties within src/index.html as follows:
<title><%= webpackConfig.metadata.properties.branding.title %></title>
Of course in this particular case I could just have replaced the title attribute within the definition of METADATA itself as follows:
const METADATA = {
title: properties.branding.title,
baseUrl: '/',
isDevServer: helpers.isWebpackDevServer()
};
And then refer to it within src/index.html as follows (which is how the starter kit defines it within this file by default):
<title><%= webpackConfig.metadata.title %></title>