Skip to content

Commit f828fe0

Browse files
author
Eduard Kyvenko
committed
refactor($webpack-config): Refactored webpack configuration to look simpler
Removed redundant imports and plugins and applied better formatting
1 parent 794d3af commit f828fe0

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

config/webpack.config.dev.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const autoprefixer = require('autoprefixer');
2-
const webpack = require('webpack');
2+
const HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');
3+
const DefinePlugin = require('webpack/lib/DefinePlugin');
34
const HtmlWebpackPlugin = require('html-webpack-plugin');
4-
const paths = require('../config/paths');
55
const getClientEnvironment = require('./env');
6+
const paths = require('../config/paths');
67

78
module.exports = {
89

@@ -43,6 +44,7 @@ module.exports = {
4344
module: {
4445
noParse: /\.elm$/,
4546
rules: [
47+
4648
{
4749
test: /\.elm$/,
4850
exclude: [ /elm-stuff/, /node_modules/ ],
@@ -59,14 +61,34 @@ module.exports = {
5961
}
6062
]
6163
},
64+
6265
{
6366
test: /\.css$/,
6467
use: [
65-
{loader: 'style-loader'},
66-
{loader: 'css-loader'},
67-
{loader: 'postcss-loader'}
68+
{
69+
loader: 'style-loader'
70+
},
71+
{
72+
loader: 'css-loader'
73+
},
74+
{
75+
loader: 'postcss-loader', options: {
76+
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
77+
plugins: () => [
78+
autoprefixer({
79+
browsers: [
80+
'>1%',
81+
'last 4 versions',
82+
'Firefox ESR',
83+
'not ie < 9'
84+
]
85+
})
86+
]
87+
}
88+
}
6889
]
6990
},
91+
7092
{
7193
exclude: [
7294
/\.html$/,
@@ -81,6 +103,7 @@ module.exports = {
81103
name: 'static/media/[name].[hash:8].[ext]'
82104
}
83105
},
106+
84107
// "file" loader for svg
85108
{
86109
test: /\.svg$/,
@@ -92,26 +115,15 @@ module.exports = {
92115
]
93116
},
94117
plugins: [
95-
new webpack.DefinePlugin(getClientEnvironment()),
118+
119+
new DefinePlugin(getClientEnvironment()),
120+
96121
new HtmlWebpackPlugin({
97122
inject: true,
98123
template: paths.template,
99124
favicon: paths.favicon
100125
}),
101-
new webpack.HotModuleReplacementPlugin(),
102-
new webpack.LoaderOptionsPlugin({
103-
options: {
104-
postcss: [
105-
autoprefixer({
106-
browsers: [
107-
'>1%',
108-
'last 4 versions',
109-
'Firefox ESR',
110-
'not ie < 9'
111-
]
112-
})
113-
]
114-
}
115-
}),
126+
127+
new HotModuleReplacementPlugin(),
116128
]
117129
};

config/webpack.config.prod.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const autoprefixer = require('autoprefixer');
22
const webpack = require('webpack');
3-
const paths = require('../config/paths');
3+
const DefinePlugin = require('webpack/lib/DefinePlugin');
4+
const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
45
const HtmlWebpackPlugin = require('html-webpack-plugin');
56
const ExtractTextPlugin = require('extract-text-webpack-plugin');
67
const CleanWebpackPlugin = require('clean-webpack-plugin');
78
const AssetsPlugin = require('assets-webpack-plugin');
89
const getClientEnvironment = require('./env');
10+
const paths = require('../config/paths');
911

1012
const root = process.cwd();
1113

@@ -53,8 +55,25 @@ module.exports = {
5355
use: ExtractTextPlugin.extract({
5456
fallbackLoader: 'style-loader',
5557
loader: [
56-
{loader: 'css-loader'},
57-
{loader: 'postcss-loader'}
58+
{
59+
loader: 'css-loader'
60+
},
61+
{
62+
loader: 'postcss-loader',
63+
options: {
64+
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
65+
plugins: () => [
66+
autoprefixer({
67+
browsers: [
68+
'>1%',
69+
'last 4 versions',
70+
'Firefox ESR',
71+
'not ie < 9'
72+
]
73+
})
74+
]
75+
}
76+
}
5877
]
5978
})
6079
},
@@ -86,7 +105,7 @@ module.exports = {
86105

87106
new AssetsPlugin({ path: paths.dist }),
88107

89-
new webpack.DefinePlugin(getClientEnvironment()),
108+
new DefinePlugin(getClientEnvironment()),
90109

91110
// Remove the content of the ./dist/ folder.
92111
new CleanWebpackPlugin([ 'dist' ], {
@@ -96,7 +115,7 @@ module.exports = {
96115
}),
97116

98117
// Minify the compiled JavaScript.
99-
new webpack.optimize.UglifyJsPlugin({
118+
new UglifyJsPlugin({
100119
compress: {
101120
warnings: false
102121
},
@@ -105,21 +124,6 @@ module.exports = {
105124
}
106125
}),
107126

108-
new webpack.LoaderOptionsPlugin({
109-
options: {
110-
postcss: [
111-
autoprefixer({
112-
browsers: [
113-
'>1%',
114-
'last 4 versions',
115-
'Firefox ESR',
116-
'not ie < 9'
117-
]
118-
})
119-
]
120-
}
121-
}),
122-
123127
new HtmlWebpackPlugin({
124128
inject: true,
125129
template: paths.template,

0 commit comments

Comments
 (0)