This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
/
webpack.config.js
200 lines (181 loc) · 4.73 KB
/
webpack.config.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Native
const path = require('path');
// Packages
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const getenv = require('getenv');
const outputPath = path.join(__dirname, 'app', 'build');
module.exports = env => {
let babelConfig = {
cacheDirectory: true,
babelrc: false,
sourceMaps: true,
presets: ['react'],
plugins: [
'flow-react-proptypes',
'syntax-trailing-function-commas',
'transform-class-properties',
'transform-decorators-legacy',
'transform-export-extensions',
'transform-object-rest-spread',
'transform-runtime',
],
};
if (env.prod) {
babelConfig = Object.assign({}, babelConfig, {
presets: [...babelConfig.presets, 'react-optimize'],
passPerPreset: true,
compact: true,
comments: false,
});
} else if (env.hmr) {
babelConfig = Object.assign({}, babelConfig, {
presets: ['react'],
plugins: [
'react-hot-loader/babel',
'flow-react-proptypes',
'syntax-trailing-function-commas',
'transform-class-properties',
'transform-decorators-legacy',
'transform-export-extensions',
'transform-object-rest-spread',
'transform-runtime',
],
});
}
const moduleConfig = {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: babelConfig,
},
],
},
{
test: /\.json/,
loader: 'json-loader',
},
{
test: /\.node$/,
use: 'node-loader',
},
],
};
const commonPlugins = [
new webpack.BannerPlugin({
banner: `process.env.NODE_ENV = ${JSON.stringify(getenv.string('NODE_ENV', 'development'))};`,
raw: true,
entryOnly: false,
}),
//prints more readable module names in the browser console on HMR updates
new webpack.NamedModulesPlugin(),
];
const config = [
{
name: 'renderer',
entry: './src/renderer.js',
target: 'electron-renderer',
output: {
path: outputPath,
filename: 'renderer.js',
},
node: {
__dirname: false,
__filename: false,
process: false,
Buffer: false,
setImmediate: false,
},
externals: [
nodeExternals({
modulesDir: './app/node_modules',
}),
],
devtool: env.dev ? 'eval-source-map' : 'source-map',
module: moduleConfig,
resolve: {
extensions: ['.svg', '.js', '.jsx', '.json'],
alias: {
xde: path.join(__dirname, 'src'),
},
},
plugins: [...commonPlugins],
},
{
name: 'electron',
entry: './src/main.js',
target: 'electron-main',
output: {
path: outputPath,
filename: 'main.js',
},
node: {
__dirname: false,
__filename: false,
process: false,
Buffer: false,
setImmediate: false,
},
externals(context, request, callback) {
callback(null, request.startsWith('.') ? false : `require('${request}')`);
},
devtool: env.dev ? 'eval-source-map' : 'source-map',
resolve: {
modules: ['node_modules'],
alias: {
xde: path.join(__dirname, 'src'),
},
},
module: moduleConfig,
plugins: [
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false,
}),
...commonPlugins,
],
},
];
// Hot Loading
if (env.hmr) {
const devServerPort = getenv.int('DEVSERVER_PORT', 8282);
let rendererConfig = config[0];
rendererConfig.entry = ['react-hot-loader/patch', './src/renderer-hot.js'];
rendererConfig.output = Object.assign({}, rendererConfig.output, {
publicPath: `http://localhost:${devServerPort}/`,
});
rendererConfig.devServer = {
//activate hot reloading
hot: true,
//match the output path
contentBase: outputPath,
//match the output publicPath
publicPath: '/',
// dev server port
port: devServerPort,
};
rendererConfig.plugins = [
//activates HMR
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
...rendererConfig.plugins,
];
config[0] = rendererConfig;
}
// Prod Setup
if (env.prod) {
// Stubbed here for future prod customizations
const rendererConfig = config[0];
const mainConfig = config[1];
rendererConfig.plugins = [...rendererConfig.plugins];
mainConfig.plugins = [...mainConfig.plugins];
config[0] = rendererConfig;
config[1] = mainConfig;
}
return config;
};