-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
154 lines (149 loc) · 4.52 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const getFilePaths = require('./get-file-paths');
const postcssImport = require('postcss-import');
const postcssPresetEnv = require('postcss-preset-env')();
const cssnano = require('cssnano')();
// TODO: extract prod logic
const isProd = process.env.NODE_ENV === 'production';
const hbsRegex = /\.(handlebars|hbs)$/;
// TODO: update webpack dev server (when the fix is available)
// Problem: HMR does not work for multiple html entries currently
// see: https://github.com/webpack/webpack/issues/7829
module.exports = {
mode: isProd ? 'production' : 'development',
devtool: isProd ? false : 'inline-source-map',
entry: {
// Global JS and CSS
// Prefixed by "_" to avoid accidental name
// collisions with generated page files
_global: path.resolve(__dirname, 'src', 'global.js'),
// Pages' JS and CSS
...getFilePaths(
path.resolve(__dirname, 'src', 'pages'),
/\.js$/
).reduce((accumulator, {fileName, filePath}) => {
// Rename home.scss and home.js to index.css and index.js
// respectively. See the explanation below
accumulator[fileName === 'home' ? 'index' : fileName] = filePath;
return accumulator;
}, {})
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([{
from: path.resolve(__dirname, 'static'),
to: path.resolve(__dirname, 'dist', 'static')
}]),
new MiniCssExtractPlugin(),
...getFilePaths(
path.resolve(__dirname, 'src', 'pages'),
hbsRegex
).map(({fileName, filePath}) => {
return new HtmlWebpackPlugin({
inject: false,
hash: true,
template: filePath,
// In order to have `home` page accessible as a root page,
// rename home.hbs -> index.html. The same is done for
// the css and javascript files to keep everything consistent
filename: `${fileName === 'home' ? 'index' : fileName}.html`,
});
}),
],
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
// Handlebars
{
test: hbsRegex,
use: {
loader: 'handlebars-loader',
options: {
// Notice: inline requires, like in html-loader, aren't currently
// supported well: https://github.com/pcardune/handlebars-loader/issues/37,
// so something like <img src="./some-img.png"/> won't work
helperDirs: [
path.resolve(__dirname, 'src', 'helpers')
],
partialDirs: [
path.resolve(__dirname, 'src', 'partials')
]
}
}
},
// Javascript
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime']
}
}
},
// CSS / SCSS
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !isProd,
},
},
'css-loader',
{
loader: 'postcss-loader',
options: {
// required, when {Function}/require is used (Complex Options)
// see: https://github.com/postcss/postcss-loader#plugins
ident: 'postcss',
autoprefixer: {
browsers: ['last 2 versions']
},
plugins: (loader) => isProd ? [
postcssImport({root: loader.resourcePath}),
postcssPresetEnv,
cssnano,
] : [
postcssImport({root: loader.resourcePath}),
postcssPresetEnv,
]
}
},
'sass-loader',
]
},
// Images
{
test: /\.(gif|png|jpe?g|svg)$/,
use: [
'file-loader',
{
loader: 'url-loader',
options: {
limit: 8192
}
},
'image-webpack-loader'
]
},
// Fonts
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
]
}
};