forked from cgcostume/cgmath.cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.mjs
152 lines (133 loc) · 4.8 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
import fs from 'fs';
import glob from 'glob';
import path from 'path';
import webpack from 'webpack';
import yaml from 'yaml';
const parseYAMLThenStringifySync = (filename) => {
const data = fs.readFileSync(`./source/${filename}`, 'utf8');
return JSON.stringify(yaml.parse(data));
}
const rev = fs.readFileSync('.git/HEAD').toString().trim();
const gitCommitHash = rev.indexOf(':') === -1 ? 'unknown' :
fs.readFileSync('.git/' + rev.substring(5)).toString().trim();
const data = {
revision: JSON.stringify(gitCommitHash),
lectures: parseYAMLThenStringifySync('lectures.yml')
};
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import md from 'markdown-it-texmath';
import mdVideo from 'markdown-it-video';
import mdEmoji from 'markdown-it-emoji';
import mdDeflist from 'markdown-it-deflist';
import mdAttrs from 'markdown-it-attrs';
import ImageMinimizerPlugin from 'image-minimizer-webpack-plugin';
export default function (env, __dirname) {
const pugFiles = glob.sync(path.join(__dirname, 'source', '{/,/units/}*.pug'));
console.log(`pug entries in "${__dirname}":`, 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,
minify: {
conservativeCollapse: false
}
}));
});
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: 'webp-generated/**/*.webp', to: '[path]/[name][ext]', force: false },
/* third party scripts */
{ from: '../node_modules/jquery/dist/jquery.min.js', to: '[name][ext]' },
]
}),
new webpack.DefinePlugin({
data: data,
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
}),
],
output: {
path: path.resolve(__dirname, "./build"),
library: undefined,
},
module: {
rules: [
{
test: /\.pug$/,
include: /source/,
exclude: /(node_modules)/,
use: [{
loader: 'pug-loader',
}],
},
{
test: /\.md$/,
use: [
{
loader: 'html-loader',
options: { esModule: false }
},
{
loader: 'markdown-it-loader',
options: {
use: [ md, mdVideo, mdEmoji, mdDeflist, mdAttrs ],
html: true,
breaks: false
}
},
],
},
{
test: /\.(jpe?g|png)$/i,
type: 'asset/resource',
generator: { filename: 'webp-generated/[name]_[hash:4][ext]' }
},
{
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 ] } ] ],
},
}]
})
]
}
};
}