-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
179 lines (170 loc) · 4.47 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
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OfflinePlugin = require('offline-plugin');
const autoprefixer = require('autoprefixer');
const path = require('path');
// Helpers
const isProd = env => env === 'production';
const isDev = env => env === 'development';
const getIfDev = env => x => (isDev(env) ? x : undefined);
const getIfProd = env => x => (isProd(env) ? x : undefined);
const removeUndefined = array => array.filter(x => x !== undefined);
// Config constants
const SOURCE_DIR = 'src';
const OUTPUT_DIR = 'dist';
const SOURCE_HTML_FILE = 'index.html';
const SOURCE_SCRIPT_PATH = './scripts';
const SOURCE_FAVICON_FILE = 'favicon.png';
const OUTPUT_STYLE_FILE = 'styles.css';
const OUTPUT_SCRIPT_FILE = 'scripts.js';
const RULES_TEST = {
JS: /\.js$/,
HTML: /\.html$/,
CSS: /\.css$/,
SCSS: /\.scss$/,
IMAGE: /\.(jpe?g|png|gif|svg)$/,
FONT: /\.(eot(\?v=\d+\.\d+\.\d+)?|ttf(\?v=\d+\.\d+\.\d+)?|svg(\?v=\d+\.\d+\.\d+)?|woff(2)*)$/,
};
const DEV_SOURCE_MAP = 'eval-source-map';
const PROD_SOURCE_MAP = false;
const getBabelLoader = env => ({
loader: 'babel-loader',
options: {
comments: isDev(env),
compact: isDev(env),
minified: isDev(env),
},
});
const getEslintLoader = env => ({
loader: 'eslint-loader',
options: {
fix: true,
emitError: isProd(env),
emitWarning: isDev(env),
},
});
const getHtmlLoader = () => ({
loader: 'html-loader',
});
const getStyleLoader = env => ({
loader: 'style-loader',
options: {
sourceMap: isDev(env),
},
});
const getCssLoader = env => ({
loader: 'css-loader',
options: {
minimize: isProd(env),
sourceMap: isDev(env),
},
});
const getSassLoader = env => ({
loader: 'sass-loader',
options: {
sourceMap: isDev(env),
},
});
const getPostcssLoader = env => ({
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer({
browsers: ['> 1%', 'last 2 versions'],
})],
sourceMap: isDev(env),
},
});
const getUrlLoader = () => ({
loader: 'url-loader',
options: {
limit: 10000,
},
});
const getImageWebpackLoader = () => ({
loader: 'image-webpack-loader',
options: {
progressive: true,
optipng: { optimizationLevel: 7 },
pngquant: { quality: '65-90' },
},
});
const getRules = (env) => {
const ifProd = getIfProd(env);
const ifDev = getIfDev(env);
const commonStyleLoaders = removeUndefined([
ifDev(getStyleLoader(env)),
getCssLoader(env),
getPostcssLoader(env),
]);
const cssLoaders = commonStyleLoaders;
const scssLoaders = [...commonStyleLoaders, getSassLoader(env)];
return [
{
test: RULES_TEST.JS,
use: [
getBabelLoader(env),
getEslintLoader(env),
],
},
{
test: RULES_TEST.HTML,
use: getHtmlLoader(),
},
{
test: RULES_TEST.CSS,
use: isProd(env) ? ExtractTextPlugin.extract({ use: cssLoaders }) : cssLoaders,
},
{
test: RULES_TEST.SCSS,
use: isProd(env) ? ExtractTextPlugin.extract({ use: scssLoaders }) : scssLoaders,
},
{
test: RULES_TEST.IMAGE,
use: removeUndefined([
getUrlLoader(),
ifProd(getImageWebpackLoader()),
]),
},
{
test: RULES_TEST.FONT,
use: getUrlLoader(),
},
];
};
const getPlugins = (env) => {
const ifProd = getIfProd(env);
const ifDev = getIfDev(env);
return removeUndefined([
new HtmlWebpackPlugin({ template: SOURCE_HTML_FILE, favicon: SOURCE_FAVICON_FILE }),
ifProd(new ExtractTextPlugin({ filename: OUTPUT_STYLE_FILE })),
ifProd(new CopyWebpackPlugin([{ from: './manifest.json' }])),
ifDev(new webpack.HotModuleReplacementPlugin()),
ifDev(new webpack.NamedModulesPlugin()),
ifDev(new webpack.NoEmitOnErrorsPlugin()),
ifDev(new StyleLintPlugin({ syntax: 'scss' })),
new OfflinePlugin(),
]);
};
module.exports = env => ({
context: path.resolve(__dirname, SOURCE_DIR),
entry: SOURCE_SCRIPT_PATH,
output: {
path: path.resolve(__dirname, OUTPUT_DIR),
publicPath: '',
filename: OUTPUT_SCRIPT_FILE,
},
module: {
rules: getRules(env),
},
plugins: getPlugins(env),
devtool: isDev(env) ? DEV_SOURCE_MAP : PROD_SOURCE_MAP,
devServer: {
host: '0.0.0.0', // Equivalent to localhost but can be accessed externally
hot: true,
noInfo: true,
overlay: true,
},
});