-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
228 lines (225 loc) · 6.79 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const path = require('path');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const jsonImporter = require('node-sass-json-importer');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
target: 'web',
entry: {
// link all scss, css and js files (you want linked in your index.html) here.
// js files are linked in <body> and css files in <head>.
index: ['./src/scss/index.scss', './src/js/index.js'],
lemon: ['./src/scss/index.scss', './src/js/index.js'],
},
mode: 'development',
output: {
// path for ALL outputs.
path: path.resolve(__dirname, 'dist'),
// subdirectory and filename for js-files.
filename: 'js/[name].bundle.js'
},
plugins: [
// clean dist folder before building
new CleanWebpackPlugin(),
// extracting css files to files from css-loader.
new MiniCssExtractPlugin({
// subdirectory and filename for css-files (also scss-files).
filename: 'css/[name].css',
}),
// build html from template html file
new HtmlWebpackPlugin({
template: './src/html/index.html',
filename: './index.html',
}),
new HtmlWebpackPlugin({
template: './src/html/lemon.html',
filename: './html/lemon.html',
}),
/* any other html file:
new HtmlWebpackPlugin({
template: './src/html/file2.html',
filename: './html/file2.html',
}),
*/
// add postcss with autoprefixer.
new webpack.LoaderOptionsPlugin({
options: {
postcss: [
// autoprefixer for automatic adding of browser prefixes, find the browserslist in package.json
autoprefixer()
]
}
}),
],
devtool: 'source-map',
module: {
rules: [
{ // copying index.html to dist and urlrewriting (img and source tag only)
test: /\.html$/,
use: [
{
loader: "html-loader",
options: {
attributes: {
list: [
{
tag: 'img',
attribute: 'src',
type: 'src'
},
{
tag: 'img',
attribute: 'srcset',
type: 'srcset',
},
{
tag: 'source',
attribute: 'src',
type: 'src',
},
{
tag: 'source',
attribute: 'srcset',
type: 'srcset',
},
],
},
}
}
]
},
{ // process sass, scss and css files. filename specified above.
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: 'dist/',
}
},
{
loader: 'css-loader',
options: {
sourceMap: true,
//url: false,
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
implementation: require("sass"),
sassOptions: {
importer: jsonImporter({
convertCase: true
}),
}
}
}
],
},
{ // image loader for index.html
test: /\.(png|jpe?g|gif)$/i,
use: (info) => ([
{
loader: 'file-loader',
options: {
// specifies output path relative to output.path
outputPath: 'img',
// specifies path to prefix url rewriting by css-loader. if the url was url(image.jpg) it is now url(publicPath/image.jpg).
publicPath: path.basename(info.issuer) === 'index.html' ? './img/' : '../img/',
}
},
{ // image compression
loader: 'image-webpack-loader',
options: {
disable: true,
mozjpeg: {
progressive: true,
quality: 65
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: true,
},
pngquant: {
quality: [0.65, 0.90],
speed: 4,
enabled: true
},
gifsicle: {
interlaced: false,
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
},
]),
},
{ // video loader for index.html
test: /\.webm$/i,
use: (info) => ([
{
loader: 'file-loader',
options: {
// specifies output path relative to output.path
outputPath: 'vid',
// specifies path to prefix url rewriting by css-loader. if the url was url(image.jpg) it is now url(publicPath/image.jpg).
publicPath: path.basename(info.issuer) === 'index.html' ? './video/' : '../video/',
}
},
]),
},
{ // svg loader
test: /\.svg$/i,
exclude: /font\/\S*\.svg((\?)?#\S*)?$/i,
use: (info) => ([
{
loader: 'file-loader',
options: {
// specifies output path relative to output.path
outputPath: 'svg',
// specifies path to prefix url rewriting by css-loader. if the url was url(image.svg) it is now url(publicPath/image.svg).
publicPath: path.basename(info.issuer) === 'index.html' ? './svg/' : '../svg/',
}
},
]),
},
{
test: /font\/\S*\.(woff(2)?|ttf|eot|svg)((\?)?#\S*)?$/i,
use: {
loader: 'file-loader',
options: {
outputPath: 'font',
publicPath: '../font/',
}
}
},
{ // js loader.
test: /\.js$/,
exclude: /node_modules/,
use: "babel-loader"
}
]
},
// to fix the relative path issues for scss mixins!
// now just reference any image-file in scss modules with ~img or ~svg.
// extend this aliases for any local file you want to reference in scss.
resolve: {
alias: {
img: path.resolve(__dirname, 'src', 'img'),
svg: path.resolve(__dirname, 'src', 'svg'),
font: path.resolve(__dirname, 'src', 'font')
}
}
};