-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.babel.js
86 lines (82 loc) · 2.22 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
import ExtractCssChunks from 'extract-css-chunks-webpack-plugin'
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import { resolve } from 'path'
import glob from 'glob'
import webpack from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
/**
* Create an instance of HtmlWebpackPlugin for each .pug file inside templates/
* Excludes files starting with an underscore
* @returns Array of HtmlWebpackPlugin instances
*/
function instantiateHtmlPlugins() {
const templateNames = glob.sync('templates/**/[^_]*.pug')
return templateNames.map(
template =>
new HtmlWebpackPlugin({
filename: template.replace(/templates\/(.+)\.pug$/, '$1.html'),
template
})
)
}
export default (env, argv) => {
const isDev = argv.mode === 'development'
const options = { sourceMap: isDev }
const devPlugins = [new webpack.HotModuleReplacementPlugin()]
const prodPlugins = [new OptimizeCssAssetsPlugin()]
const plugins = [
new ExtractCssChunks({
filename: isDev ? '[name].css' : '[name].[chunkhash].css',
hot: isDev
}),
...instantiateHtmlPlugins()
].concat(isDev ? devPlugins : prodPlugins)
return {
entry: { theme: './js/index.js' },
output: {
path: resolve(__dirname, 'dist'),
filename: isDev ? '[name].js' : '[name].[chunkhash].js'
},
devServer: {
hot: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.s?css$/,
use: [
{ loader: ExtractCssChunks.loader, options },
{ loader: 'css-loader', options },
{ loader: 'postcss-loader', options },
{ loader: 'sass-loader', options }
]
},
{
test: /\.(jpe?g|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$/,
loader: 'file-loader'
},
{
test: /\.pug$/,
loader: 'pug-loader'
}
]
},
plugins,
stats: {
children: false,
chunks: false,
colors: true,
hash: false,
modules: false,
version: false
},
devtool: isDev ? 'source-map' : false
}
}