-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.ts
99 lines (96 loc) · 2.64 KB
/
webpack.config.ts
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
import CopyWebpackPlugin from "copy-webpack-plugin";
import * as path from "path";
import ImageMinimizerPlugin from "image-minimizer-webpack-plugin";
import { Configuration, WebpackPluginInstance } from "webpack";
import imagesJson from "./assets/templates/images.json";
const srcFolder = path.resolve("./src");
const configuration: Configuration = {
devtool: "source-map",
entry: ["background", "options"].reduce(
(entries, name) =>
Object.assign(entries, {
[name]: [
"core-js/stable",
"regenerator-runtime/runtime",
path.join(srcFolder, name),
],
}),
{},
),
mode: "production",
module: {
rules: [
{
exclude: /(node_modules|bower_components)/,
test: /\.tsx?$/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-typescript"],
},
},
},
],
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "./dist"),
},
plugins: [
new CopyWebpackPlugin({
patterns: ["options"].reduce(
(config, name) => {
config.push(
...["html", "css"].map((ext) => ({
from: path.join(srcFolder, name, `index.${ext}`),
to: `${name}.${ext}`,
})),
);
return config;
},
[
{ from: "./manifest.json" },
...Object.keys(imagesJson).map((from) => ({ from })),
],
),
}) as unknown as WebpackPluginInstance,
],
optimization: {
minimizer: [
...Object.entries(imagesJson).map(([imgSrc, imgConfig]) => {
const fileType = path.extname(imgSrc).substring(1);
return new ImageMinimizerPlugin<unknown>({
generator: imgConfig.sizes.map((size) => ({
type: "asset",
implementation: async (original, options) => {
const result = await ImageMinimizerPlugin.sharpGenerate(
original,
options,
);
const fileExt = path.extname(result.filename);
result.filename = `${imgConfig.name}${size}${fileExt}`;
return result;
},
options: {
encodeOptions: {
[fileType]: {
quality: 100,
},
},
resize: {
enabled: true,
height: size,
width: size,
},
},
})),
});
}),
],
},
resolve: {
extensions: [".js", ".ts"],
modules: [srcFolder, path.resolve("./node_modules")],
},
};
export default configuration;