-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-webpack.config.cjs
103 lines (97 loc) · 3.9 KB
/
angular-webpack.config.cjs
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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin')
module.exports = (config, _options, _targetOptions) => {
config.module = config.module || {}
config.module.rules = config.module.rules || []
// exclude raw ts source import from angular compilation
config.module.rules
.filter(r => r.test ? 'ex.ts'.match(r.test) : false)
.forEach(ts => {
ts.resourceQuery = (str => {
if (str.includes('raw'))
return false
return true
})
})
config.module.rules.unshift({
resourceQuery: /raw/,
type: 'asset/source'
})
config.module.rules.forEach(r => {
if (r.test instanceof RegExp) {
const exp = r.test
const repl = exp.source.replace(/[\(\|]svg[\|\)]/i, (a) => (a.toLowerCase().replace('|svg', '').replace('svg|', '') || '|'))
if (repl != exp.source) {
r.test = new RegExp(repl, exp.flags)
}
}
})
config.module.rules.push({
test: /\.meta\.((gif)|(png)|(jpe?g))$/i,
type: 'asset/resource',
generator: {
filename: (pathData) => {
const info = pathData.module.resourceResolveData
const params = new URLSearchParams(info.query)
const format = params.get('format') || path.extname(info.path).split('.').pop()
const extension = (ext => ext === 'jpeg' ? 'jpg' : ext)(format.toLowerCase())
const fileName = '[name].[contenthash:8].meta[ext]'
return fileName.replace('[ext]', `.${extension}`)
}
}
})
config.module.rules.push({
test: /(?<!\.meta)\.((png)|(jpe?g))(\?v=\d+\.\d+\.\d+)?$/i,
type: 'asset/resource',
generator: {
filename: (_pathData) => '[name].[contenthash:8].webp'
},
use: [{
loader: 'webpack-image-resize-loader',
options: {}
}]
})
config.module.rules.push({
test: /(?<!\.meta)\.((eot)|(woff)|(woff2)|(ttf)|(svg)|(mp4)|(gif))$/i,
type: 'asset/resource',
generator: {
filename: (pathData) => {
const info = pathData.module.resourceResolveData
const params = new URLSearchParams(info.query)
const format = params.get('format') || path.extname(info.path).split('.').pop()
const extension = (ext => ext === 'gif' ? 'webp' : ext)(format.toLowerCase())
const fileName = '[name].[contenthash:8][ext]'
return fileName.replace('[ext]', `.${extension}`)
}
}
})
if (config.optimization) {
config.optimization.minimizer = config.optimization.minimizer || []
config.optimization.minimizer.push(new ImageMinimizerPlugin({
test: /(?<!\.meta)\.((png))(\?v=\d+\.\d+\.\d+)?$/i,
minimizer: {
implementation: ImageMinimizerPlugin.imageminGenerate,
options: {
plugins: [
['imagemin-webp', { quality: 100, method: 4, lossless: true }],
['imagemin-gif2webp', { quality: 100, method: 4, lossy: false }]
]
}
}
}))
config.optimization.minimizer.push(new ImageMinimizerPlugin({
test: /(?<!\.meta)\.((jpe?g)|(gif))(\?v=\d+\.\d+\.\d+)?$/i,
minimizer: {
implementation: ImageMinimizerPlugin.imageminGenerate,
options: {
plugins: [
['imagemin-webp', { quality: 95, method: 4, lossless: false }],
['imagemin-gif2webp', { quality: 95, method: 4, lossy: true }]
]
}
}
}))
}
return config
}