forked from bdefore/universal-redux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge-configs.js
118 lines (96 loc) · 5.2 KB
/
merge-configs.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
const path = require('path');
const util = require('util');
const lodash = require('lodash');
const webpack = require('webpack');
const mergeBabel = require('./merge-babel-config');
const mergeWebpack = require('webpack-config-merger');
const baseWebpackConfig = require('../config/webpack.config.js');
const baseDevConfig = mergeWebpack(baseWebpackConfig.common, baseWebpackConfig.development);
const baseProdConfig = mergeWebpack(baseWebpackConfig.common, baseWebpackConfig.production);
const baseToolsConfig = require('../config/webpack-isomorphic-tools.config.js');
const WebpackErrorNotificationPlugin = require('webpack-error-notification');
const isProduction = process.env.NODE_ENV === 'production';
function inspect(obj) {
const utilOptions = {
depth: 12,
colors: true
};
console.log(util.inspect(obj, utilOptions));
}
module.exports = (userConfig) => {
// derive root and sourceDir, alowing for absolute, relative, or not provided
const root = userConfig.root ? userConfig.root[0] === '/' ? userConfig.root : path.resolve(__dirname, '../..', userConfig.root) : path.resolve(__dirname, '../../..');
const sourceDir = userConfig.sourceDir ? userConfig.sourceDir[0] === '/' ? userConfig.sourceDir : path.resolve(root, userConfig.sourceDir) : path.resolve(root, './src');
// merge with base config
const universalReduxConfig = lodash.merge(require('../config/universal-redux.config.js')(root, sourceDir), userConfig);
// merge with base webpack config
const baseConfig = isProduction ? baseProdConfig : baseDevConfig;
const combinedWebpackConfig = mergeWebpack(baseConfig, universalReduxConfig.webpack.config);
combinedWebpackConfig.context = root;
combinedWebpackConfig.resolve.root = sourceDir;
// derive webpack output destination from staticPath
combinedWebpackConfig.output.path = universalReduxConfig.server.staticPath + '/dist';
// add babel for js transpiling
const babelConfig = mergeBabel(universalReduxConfig.babelConfig, universalReduxConfig.verbose);
combinedWebpackConfig.module.loaders.unshift({ test: /\.jsx?$/, exclude: /node_modules/, loaders: babelConfig });
// gather tools config
const userToolsConfig = require(path.resolve(universalReduxConfig.toolsConfigPath));
const combinedToolsConfig = lodash.merge(baseToolsConfig, userToolsConfig);
// bury it here rather than pollute the project directory
combinedToolsConfig.webpack_assets_file_path = 'node_modules/universal-redux/webpack-assets.json';
// add tools settings to combined weback config
const WebpackIsomorphicToolsPlugin = require('webpack-isomorphic-tools/plugin');
const toolsPlugin = new WebpackIsomorphicToolsPlugin(combinedToolsConfig);
combinedWebpackConfig.module.loaders.push({ test: toolsPlugin.regular_expression('images'), loader: 'url-loader?limit=10240' });
combinedWebpackConfig.plugins.push(isProduction ? toolsPlugin : toolsPlugin.development());
// turn on linting per webpack build, unless directed not to
if (universalReduxConfig.lint.enabled !== false && !isProduction) {
combinedWebpackConfig.module.loaders[0].loaders.push('eslint-loader');
const lintConfigPath = universalReduxConfig.lint.config || path.resolve(__dirname, '../.eslintrc');
combinedWebpackConfig.eslint = {
configFile: lintConfigPath
};
}
// turn on desktop notifications if user elects to
if (universalReduxConfig.notifications === true && !isProduction) {
combinedWebpackConfig.plugins.push(new WebpackErrorNotificationPlugin());
}
// add default settings that are used by server via process.env
const definitions = {
__LOGGER__: false,
__DEVTOOLS__: !isProduction,
__DEVELOPMENT__: !isProduction
};
// override with user settings
lodash.each(universalReduxConfig.globals, (value, key) => { definitions[key] = JSON.stringify(value); });
combinedWebpackConfig.plugins.push(new webpack.DefinePlugin(definitions));
// add routes, reducer and rootComponent aliases so that client has access to them
combinedWebpackConfig.resolve.alias = combinedWebpackConfig.resolve.alias || {};
combinedWebpackConfig.resolve.alias.routes = universalReduxConfig.routes;
if (universalReduxConfig.redux.middleware) {
combinedWebpackConfig.resolve.alias.middleware = universalReduxConfig.redux.middleware;
} else {
combinedWebpackConfig.resolve.alias.middleware = path.resolve(__dirname, '../lib/helpers/empty.js');
}
if (universalReduxConfig.rootComponent) {
combinedWebpackConfig.resolve.alias.rootComponent = universalReduxConfig.rootComponent;
} else {
combinedWebpackConfig.resolve.alias.rootComponent = path.resolve(__dirname, '../lib/helpers/rootComponent.js');
}
// add project level vendor libs
if (universalReduxConfig.webpack.vendorLibraries && isProduction) {
lodash.each(universalReduxConfig.webpack.vendorLibraries, (lib) => {
combinedWebpackConfig.entry.vendor.push(lib);
});
}
// output configuration files if user wants verbosity
if (universalReduxConfig.verbose) {
console.log('\nWebpack config:');
inspect(combinedWebpackConfig);
console.log('\nIsomorphic tools config:');
inspect(combinedToolsConfig);
}
universalReduxConfig.webpack.config = combinedWebpackConfig;
return universalReduxConfig;
};