-
Notifications
You must be signed in to change notification settings - Fork 13
/
config-overrides.js
65 lines (55 loc) · 2.23 KB
/
config-overrides.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* config-overrides.js */
const path = require('path');
const themes = require('./themes.config');
const themeContent = require('./themes.content');
const merge = require('webpack-merge');
const multipleThemesCompile = require('webpack-multiple-themes-compile/src');
const multipleEntry = require('react-app-rewire-multiple-entry')
// Validate themes before compiling
function validateThemes(themes) {
// Check that themes start with "light" or "dark"
const themeNames = Object.keys(themes)
for (var i = 0; i < themeNames.length; i++) {
const name = themeNames[i]
if( !(name.startsWith("light") || name.startsWith("dark")) ) {
throw new Error("Invalid themes. Please check README for details.")
}
}
console.log("Valid themes found.")
}
validateThemes(themes)
module.exports = {
webpack: function(config, env) {
// This is to re-format CRA's entries to make them compatible with multipleThemesCompile()
const appEntries = multipleEntry([{entry : 'src/index.js'}])
appEntries.addMultiEntry(config)
// Add theming
let multiTheme = multipleThemesCompile({
themesConfig: themes,
lessContent: (themeName, config) => themeContent(themeName),
styleLoaders: [
{ loader: 'css-loader' },
{
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true
}
}
}
],
cwd: path.resolve('./')
})
/* Store the rule to load less/css files to put it where CRA stores these kind of rules
* (otherwise, duplicate rules with existing and the compiler will complain) */
var lessRule = multiTheme["module"]["rules"][0]
delete multiTheme["module"]["rules"]
// Change the location of css files to import them by name without knowing the id
multiTheme["plugins"][0]["options"]["chunkFilename"] = "css/[name].css"
// Merge default CRA config with themes' config. Now, we just need to fix the lessRule.
var newConfig = merge(config, multiTheme)
// Add themes' rule to the beginning of existing 'oneOf' to make it the first to be applied
newConfig["module"]["rules"][2]["oneOf"].unshift(lessRule)
return newConfig
}
};