-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.babel.js
170 lines (163 loc) · 5.88 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
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineManifestPlugin = require('inline-manifest-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const PurifyPlugin = require('purifycss-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const ReloadPlugin = require('reload-html-webpack-plugin');
const WebpackMD5Hash = require('webpack-md5-hash');
const {getIfUtils, removeEmpty, propIf} = require('webpack-config-utils');
// Loads an HtmlWebpackPlugin for each template in the assets/views directory
const loadHtmlPlugin = (src, base = '') => {
const files = fs.readdirSync(src);
return files.map((file) => {
const ext = path.extname(file);
if(ext === '.pug') {
const name = path.basename(file, ext);
return new HtmlWebpackPlugin({
template: `${src}/${file}`,
filename: `${base}${name}.html`
});
}
});
};
module.exports = (env) => {
const {ifProd, ifNotProd} = getIfUtils(env);
const isBuild = Boolean(env.build);
const withDocs = Boolean(env.docs);
return {
entry: [
'./assets/scripts/index.js'
],
output: {
filename: ifProd('assets/scripts/bundle-[chunkhash:8].js', 'scripts/bundle.js'),
path: path.resolve('build'),
pathinfo: ifNotProd(),
publicPath: process.env.PUBLIC_PATH || '/'
},
devtool: ifProd('source-map', 'eval'),
devServer: {
stats: 'errors-only',
historyApiFallback: ifNotProd()
},
module: {
rules: [
{
test: /\.css$/,
loader: ifProd(
ExtractTextPlugin.extract({
fallbackLoader: 'style-loader',
loader: 'css-loader!postcss-loader'
}), 'style-loader!css-loader!postcss-loader')
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
cacheDirectory: ifNotProd()
}
},
{
test: /\.pug$/,
loader: 'pug-loader'
},
{
test: /\.(jpe?g|png|gif)$/,
loaders: [
'url-loader?limit=10000&name=images/[name].[ext]',
'image-webpack-loader?bypassOnDebug&optimizationLevel=2&interlaced=false'
]
},
{
test: /\.svg$/,
loaders: [
'file-loader?name=images/[name].[ext]',
'image-webpack-loader?bypassOnDebug&optimizationLevel=2&interlaced=false'
]
},
{
test: /\.woff$/,
loader: 'url-loader',
query: {
name: 'fonts/[name].[ext]',
limit: 5000,
mimetype: 'application/font-woff'
}
},
{
test: /\.ttf$|\.eot$/,
loader: 'file-loader',
query: {
name: 'fonts/[name].[ext]'
}
}
]
},
plugins: removeEmpty([
ifNotProd(new ProgressBarPlugin()),
isBuild ? undefined : ifNotProd(new ReloadPlugin()), // eslint-disable-line
new webpack.LoaderOptionsPlugin({
options: {
postcss: [
require('postcss-import'),
require('postcss-mixins'),
require('postcss-cssnext')
]
}
}),
ifProd(new ExtractTextPlugin('assets/styles/styles-[chunkhash:8].css')),
ifProd(new PurifyPlugin({
basePath: __dirname,
paths: propIf(env.docs || false,
[
'assets/templates/**/*.pug',
'docs/**/*.pug'
],
[
'assets/templates/**/*.pug'
]
),
purifyOptions: {
minify: true
}
})),
new StyleLintPlugin({
configFile: '.stylelintrc',
files: 'assets/styles/**/*.css'
}),
ifProd(new InlineManifestPlugin()),
ifProd(new webpack.optimize.CommonsChunkPlugin({
name: 'manifest'
})),
new webpack.optimize.OccurrenceOrderPlugin(),
new WebpackMD5Hash(),
...loadHtmlPlugin(propIf(
withDocs || false,
'./docs',
'./assets/templates/views'
)),
...ifNotProd(loadHtmlPlugin('./docs', 'docs/'), []),
new CopyPlugin(removeEmpty([
// Copy fonts to build directory
{from: 'fonts', to: 'fonts'},
{from: 'favicons', to: '../build'},
ifNotProd({from: 'images', to: 'images'})
]), {
ignore: [
'.*'
]
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: ifProd('"prod"', '"dev"')
}
}),
new webpack.NamedModulesPlugin()
])
};
};