-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake-webpack-config.js
120 lines (110 loc) · 2.79 KB
/
make-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
import path from 'path';
import webpack from 'webpack';
import loadersByExtension from './utils/loadersByExtension.js';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
const autoprefixer = 'autoprefixer?browsers=last 2 version';
const loadersByExt = loadersByExtension({
'json': 'json',
'png|jpg|gif': 'url?limit=5000',
'woff|woff2': 'url?limit=1',
'svg': 'url?limit=10000',
});
const loadersCommon = [
{
test: /\.jsx?$/,
loaders: ['babel'],
include: path.join(__dirname, 'app'),
},
{
test: require.resolve('react'),
loader: 'imports?shim=es5-shim/es5-shim&sham=es5-shim/es5-sham',
},
];
const loadersDev = [
{
test: /\.css$/,
loader: 'style!css!' + autoprefixer,
},
{
test: /\.scss$/,
loader: 'style!css!' + autoprefixer + '!sass',
},
];
const loadersProd = [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css!' + autoprefixer),
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css!' + autoprefixer + '!sass'),
},
];
/** options
* @option optimize {bool} // optimize js if true
* @option sourcemaps {bool} // generate sourcemaps if true (!rewrite devtool!)
* @option devtool {string} // specify devtool
*/
module.exports = (options) => {
const config = {
entry: [
'./app/app',
],
output: {
path: path.join(__dirname, 'static/build'),
filename: 'bundle.js',
publicPath: '/static/build/',
},
plugins: [
new webpack.DefinePlugin({
OPTIMIZED: !!options.optimize,
DEBUG: !options.optimize,
}),
new webpack.DefinePlugin({
'process.env': {
BROWSER: JSON.stringify(true),
},
}),
],
resolve: {
root: path.join(__dirname, 'app'),
extensions: ['', '.js', '.jsx'],
modulesDirectories: [
'app',
'node_modules',
],
},
resolveLoader: {
root: [
path.join(__dirname, 'node_modules'),
__dirname,
],
},
module: {
loaders: loadersByExt.concat(loadersCommon),
},
};
if (options.optimize) {
config.module.loaders = config.module.loaders.concat(loadersProd);
config.plugins.push(
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.DedupePlugin(),
new ExtractTextPlugin('styles.css'),
);
options.devtool = null;
options.sourcemaps = null;
} else {
config.module.loaders = config.module.loaders.concat(loadersDev);
config.entry.unshift('webpack-hot-middleware/client');
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
);
}
if (options.sourcemaps) {
config.devtool = '#inline-source-map';
} else if (options.devtool) {
config.devtool = options.devtool;
}
return config;
};