-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets-pipeline.mjs
93 lines (86 loc) · 2.88 KB
/
assets-pipeline.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
import * as esbuild from "esbuild";
import {Compress} from "gzipper";
import sharp from "sharp";
import fg from "fast-glob";
import path from "path";
import {sassPlugin} from "esbuild-sass-plugin";
import autoprefixer from "autoprefixer";
import postcss from "postcss";
import { purgeCSSPlugin } from "@fullhuman/postcss-purgecss";
const assetsFolder = "src/main/resources/public";
const assetsBuildFolder = "build/resources/main/public";
const compressPlugin = {
name: "compress",
setup(build) {
build.onEnd(() => {
const assetsBuildFolder = build.initialOptions.outdir;
const verbose = build.initialOptions.logLevel === "verbose" || build.initialOptions.logLevel === "debug";
const compressOptions = {
brotli: true,
deflate: true,
deflateLevel: 9,
gzip: true,
gzipLevel: 9,
exclude: ["jpeg", "jpg", "png", "webp"],
verbose: verbose,
};
new Compress(assetsBuildFolder, assetsBuildFolder, compressOptions).run();
});
},
};
const webpPlugin = {
name: "webp",
setup(build) {
build.onEnd(() => {
fg.async([`${build.initialOptions.outdir}/**/*.{png,jpg}`], {caseSensitiveMatch: false, dot: true})
.then(images =>
images.forEach(image => {
const imagePath = path.parse(image);
const webpOutputImage = `${imagePath.dir}/${imagePath.name}.webp`;
const avifOutputImage = `${imagePath.dir}/${imagePath.name}.avif`;
sharp(image).toFormat("webp").toFile(webpOutputImage);
sharp(image).toFormat("avif").toFile(avifOutputImage);
})
);
});
}
};
await esbuild.build({
entryPoints: [
`${assetsFolder}/stylesheets/main.scss`,
`${assetsFolder}/javascripts/main.js`,
`${assetsFolder}/images/**/*.*`,
],
bundle: true,
minify: true,
allowOverwrite: true,
metafile: true,
legalComments: "none",
entryNames: "[dir]/[name].[hash]",
outdir: assetsBuildFolder,
logLevel: "info",
loader: {
".webp": "copy",
".jpg": "copy",
".png": "copy",
".ico": "copy",
},
plugins: [
sassPlugin({
async transform(source) {
const {css} = await postcss([
autoprefixer,
purgeCSSPlugin({
content: ["./src/**/*.kte", `${assetsFolder}/**/*.js`],
safelist: {
deep: [/footnotes$/]
}
})
]).process(source, {from: undefined, map: false});
return css;
}
}),
compressPlugin,
webpPlugin,
],
});