I'm trying to find best way to support theme styling in my angular 4 app. I'm using SASS variables and theme setup relies on 4 SASS vars: primary and secondary colour, background-image and switch for dark/light theme.
Now, I want to change theme dynamically from the app. I have this for now:
I'm using sass-vars-loader library (https://github.com/epegzz/sass-vars-loader), and load sass variables from JSON file into my _vars.scss during app build. And when I change those vars from the component I write new values to JSON file and trigger app recompile as I'm watching for changes on that file.
This is succesfuly recompiling and loading new variables into _vars.scss during the development, but my question is how can I support that in the production.
To be able to configure webpack, I ejected webpack.config.js with $ng eject
thus not using angular-cli commands anymore.
// webpack.config.js
...
{
"test": /\.scss$|\.sass$/,
"use": [
"style-loader",
{
"loader": "css-loader",
"options": {
"sourceMap": false,
"importLoaders": 1
}
},
{
"loader": "postcss-loader",
"options": {
"ident": "postcss",
"plugins": postcssPlugins
}
},
{
"loader": "sass-loader",
"options": {
"sourceMap": false,
"precision": 8,
"includePaths": []
}
},
// Reads Sass vars from JSON
{ loader: "@epegzz/sass-vars-loader", options: {
files: [
path.resolve(__dirname, 'src/theme.json')
]
}
}
]
},
...
"devServer": {
setup: function(app) {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.post("/theme", function(req, res) {
var payload = req.body,
theme;
fs.writeFileSync('src/theme.json', JSON.stringify(payload, null, 3));
res.send("Success");
});
},
}
I suppose I need to create custom node.js server which will serve my app and watch for changes, jsut like the webpack-dev-server is doing during development.
Any help is appreciated, Thanks.