-
Notifications
You must be signed in to change notification settings - Fork 12
/
webpack.config.js
166 lines (159 loc) · 4.86 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
var webpack = require("webpack");
const path = require("path");
const fs = require('fs');
// include the js minification plugin
const TerserPlugin = require('terser-webpack-plugin');
// include the css extraction and minification plugins
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
// include the HTML templating plugin
const HTMLWebpackPlugin = require("html-webpack-plugin");
const HtmlBeautifyPlugin = require('html-beautify-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
function generateHtmlPlugins (templateDir) {
const templateFiles = fs.readdirSync(path.resolve(__dirname, templateDir))
return templateFiles.map(item => {
// Split names and extension
const parts = item.split('.')
const name = parts[0]
const extension = parts[1]
return new HTMLWebpackPlugin({
filename: `${name}.html`,
template: path.resolve(__dirname, `${templateDir}/${name}.${extension}`)
})
})
}
// We will call the function like this:
const htmlPlugins = generateHtmlPlugins('./src/pages')
module.exports = {
entry: ["./src/js/index.js", "./src/scss/main.scss"],
output: {
filename: "js/index.[hash:8].js",
path: path.join(__dirname, "./build/")
},
devServer: {
contentBase: "./build"
},
module: {
rules: [
// perform js babelization on all .js files
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
publicPath: '../'
}
}]
},
{
test: /\.(svg|png|jpe?g)/i,
use: [{
loader: "url-loader",
options: {
name: "./images/[name].[ext]",
limit: 5000
}
},
{
loader: "img-loader"
}
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
sourceMap: true
}
}
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: "css/main.[contenthash:8].css"
}),
new CopyWebpackPlugin([{
from: 'src/images',
to: 'images'
}]),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
]
.concat(htmlPlugins)
.concat(new HtmlBeautifyPlugin({
config: {
html: {
end_with_newline: true,
indent_size: 2,
indent_with_tabs: true,
indent_inner_html: true,
preserve_newlines: true,
unformatted: ['p', 'i', 'b', 'span']
}
}
})),
optimization: {
minimizer: [
// enable the js minification plugin
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
output: {
comments: false,
},
},
}),
// enable the css minification plugin
new OptimizeCSSAssetsPlugin({})
]
},
resolve: {
alias: {
jquery: "jquery/src/jquery"
}
}
};