-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.babel.js
201 lines (181 loc) · 4.49 KB
/
webpack.config.babel.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
201
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
import validate from 'webpack-validator';
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import stylelint from 'stylelint';
import precss from 'precss';
import postcssImport from 'postcss-import';
import webpack from 'webpack';
import merge from 'webpack-merge';
import NodePathReplacePlugin from './NodePathReplacePlugin.js';
export const cssModuleNames = '[name]__[local]___[hash:base64:5]';
function getCssLoaders() {
const cssLoaderConfig = [
'css?modules&camelcase',
'importLoaders=1',
`localIdentName=${cssModuleNames}`,
].join('&');
return [cssLoaderConfig, 'postcss'];
}
function htmlWebpackPlugin(minify) {
return new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src', 'index.html'),
inject: 'body',
minify: minify && {
collapseWhitespace: true,
},
});
}
function productionBuild(base) {
return merge(base, {
entry: [
'./src/index.jsx',
],
module: {
loaders: [
{ test: /\.css$/, loader: ExtractTextPlugin.extract(getCssLoaders()) },
],
},
plugins: [
...base.plugins,
htmlWebpackPlugin(true),
new ExtractTextPlugin('style.[contenthash:8].css', { allChunks: true }),
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false,
},
output: {
comments: false, // Also removes licences
},
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
],
resolve: {
extensions: ['', '.js', '.jsx'],
},
});
}
function addCssLoaders(base) {
return merge(base, {
module: {
loaders: [
{ test: /\.css$/, loaders: getCssLoaders() },
],
},
});
}
function unitTestConfig(base) {
return merge.smart(base, {
entry: [],
target: 'node',
module: {
loaders: [
{ test: /\.css$/, loaders: ['fake-style'] },
],
},
plugins: [
...base.plugins,
new NodePathReplacePlugin(),
],
});
}
function addStyleLoader(base) {
return merge.smart(base, {
module: {
loaders: [
{ test: /\.css$/, loaders: ['style'] },
],
},
});
}
function addHotLoaderConfig(base) {
return merge(base, {
entry: [
'webpack-dev-server/client?http://0.0.0.0:8080',
'webpack/hot/only-dev-server',
'./src/index.jsx',
],
plugins: [
...base.plugins,
new webpack.HotModuleReplacementPlugin(),
],
});
}
function startConfig(base) {
return merge(base, {
plugins: [
...base.plugins,
htmlWebpackPlugin(false),
],
});
}
function addLoadersPlugins(base) {
const production = process.env.NODE_ENV === 'production';
const target = process.env.npm_lifecycle_event;
if (production) {
return productionBuild(base);
}
const withCssLoaders = addCssLoaders(base);
if (target === 'test') {
return unitTestConfig(withCssLoaders);
}
const withStyleLoader = addStyleLoader(withCssLoaders);
if (target !== 'storybook') {
const withHotLoaderConfig = addHotLoaderConfig(withStyleLoader);
if (target === 'start') {
return startConfig(withHotLoaderConfig);
}
return withHotLoaderConfig;
}
return withStyleLoader;
}
function addOutput(base) {
const target = process.env.npm_lifecycle_event;
if (target !== 'storybook') {
return merge(base, {
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.[hash:8].js',
},
});
}
return base;
}
function buildConfig(base) {
return [addOutput, addLoadersPlugins].reduce(
(config, builder) => builder(config),
base
);
}
const webpackConfig = buildConfig({
module: {
loaders: [
{ test: /\.jsx?$/, exclude: /node_modules/, loaders: ['babel'] },
{ test: /\.(jpe?g|png|gif)$/i, loader: 'file-loader?name=[name].[hash:8].[ext]' },
],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
},
}),
new webpack.ResolverPlugin(
new webpack.ResolverPlugin.FileAppendPlugin(['/index.css'])
),
],
postcss: (webpackEnv) => [
stylelint,
postcssImport({
addDependencyTo: webpackEnv,
}),
precss,
],
devServer: {
stats: 'errors-only',
},
});
console.log(JSON.stringify(webpackConfig, null, 2));
export default validate(webpackConfig);