-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.common.mjs
172 lines (145 loc) · 5.82 KB
/
webpack.common.mjs
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
import fs from 'fs';
import glob from 'glob';
import path from 'path';
import webpack from 'webpack';
import git from 'git-rev-sync';
import yaml from 'yaml';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
// import ImageMinimizerPlugin from 'image-minimizer-webpack-plugin';
/* For now, all image sources are specified at pug-compile-time based on the respective yaml files. This makes it
difficult to use webpack loader for webp-optimization, since this implies require with expressions... */
import imagemin from 'imagemin';
import webp from 'imagemin-webp';
const parseYAMLThenStringifySync = (filename) => {
const data = fs.readFileSync(`./source/data/${filename}`, 'utf8');
return JSON.stringify(yaml.parse(data));
}
const createBibliographyFromBibFilesSync = (filenames) => {
const entries = {};
filenames.forEach((filename) => {
const entry = fs.readFileSync(filename, 'utf8');
const key = path.basename(filename, '.bib');
entries[key] = entry;
});
return JSON.stringify(entries);
}
export default (env, __dirname) => {
const bibFiles = glob.sync(path.join('source/data/bibliography', '/*.bib'));
const data = {
revision: JSON.stringify(git.short(__dirname)),
config: parseYAMLThenStringifySync('config.yml'),
contact: parseYAMLThenStringifySync('contact.yml'),
header: parseYAMLThenStringifySync('header.yml'),
publications: parseYAMLThenStringifySync('publications.yml'),
repositories: parseYAMLThenStringifySync('repositories.yml'),
teaching_activities: parseYAMLThenStringifySync('teaching-activities.yml'),
bibliography: createBibliographyFromBibFilesSync(bibFiles)
};
// Pug Configuration
const pugFiles = glob.sync(path.join(__dirname, 'source', '/*.pug'));
console.log(`collecting pug files in "${path.join(__dirname, 'source')}":`, pugFiles);
const templates = [];
pugFiles.forEach((template) => {
const filename = path.relative(path.join(__dirname, 'source'), template);
templates.push(new HtmlWebpackPlugin({
filename: filename.replace('.pug', '.html'),
template: filename,
inject: false
}));
});
// Image Optimization (webp)
const images = glob.sync(path.join(__dirname, 'source/images', '/*.{jpg,jpeg,png}'));
console.log(`optimizing images in "${path.join(__dirname, 'source/images')}":`, images);
imagemin(images, {
destination: 'source/images',
plugins: [
webp({ quality: [88, 96] })
]
})
return {
context: path.resolve(__dirname, 'source'),
entry: {
'styles': ['./styles/main.scss'],
'bootstrap': ['./scripts/bootstrap.mjs'],
'scripts': ['./scripts/scripts.mjs'],
},
plugins: [
...templates,
new CopyWebpackPlugin({
patterns: [
{ from: 'images/**/*.webp', to: '[path]/[name][ext]', force: false },
{ from: 'favicon*', to: '[path]/[name][ext]', force: false },
{ from: 'resources/**/*', to: '[path]/[name][ext]', force: false },
{ from: 'data/bibliography/*.bib', to: 'bibliography/[name][ext]', force: false },
{ from: 'vcard.vcf', to: '[name][ext]', force: false },
/* third party scripts and assets */
// { from: '../node_modules/jquery/dist/jquery.min.js', to: '[name][ext]' },
{ from: '../node_modules/glightbox/dist/css/glightbox.min.css', to: '[name][ext]' },
]
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
new webpack.DefinePlugin({
data: data,
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
})
],
output: {
path: path.resolve(__dirname, 'build'),
library: undefined,
},
module: {
rules: [
// {
// test: /\.(jpe?g|png)$/i,
// type: 'asset/resource',
// generator: { filename: 'webp-generated/[name][ext]' }
// },
{
test: /\.pug$/,
include: /source/,
exclude: /(node_modules)/,
use: [{
loader: 'pug-loader',
}],
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
],
},
]
},
// optimization: {
// minimizer: [
// '...',
// new ImageMinimizerPlugin({
// minimizer: {
// implementation: ImageMinimizerPlugin.imageminMinify,
// options: {
// plugins: ['imagemin-mozjpeg', 'imagemin-pngquant']
// }
// },
// generator: [
// { // use `?as=webp`
// preset: 'webp',
// implementation: ImageMinimizerPlugin.imageminGenerate,
// options: {
// plugins: [['imagemin-webp', { quality: [88, 96] }]],
// },
// }]
// })
// ]
// }
};
}