This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
webpack.config.js
133 lines (114 loc) · 4.11 KB
/
webpack.config.js
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
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const EventHooksPlugin = require("event-hooks-webpack-plugin");
const fs = require("fs");
const path = require("path");
const del = require("del");
module.exports = (env, argv) => ({
context: path.resolve(__dirname),
entry: "./gui/js/index.js",
output: {
filename: "bundle.js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: { minimize: true },
},
],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.(jpg|png|gif|svg)$/,
use: [
{
loader: "url-loader",
options: {
limit: 10000,
name: "[name].[ext]",
outputPath: "img/",
publicPath: "img/",
},
},
{
loader: "image-webpack-loader",
options: {
pngquant: {
quality: "20-40",
},
},
},
],
},
],
},
optimization: {
minimize: true,
},
resolve: {
alias: {
react: "preact/compat",
"react-dom": "preact/compat",
},
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebPackPlugin({
template: "./gui/index.html",
filename: "./index.html",
inlineSource: ".(js|css)$", // embed all javascript and css inline
}),
new CleanWebpackPlugin({
protectWebpackAssets: (argv.mode === "production"),
cleanAfterEveryBuildPatterns: ["**/*.js", "**/*.html", "**/*.css", "**/*.js.gz", "**/*.css.gz"],
}),
new HtmlWebpackInlineSourcePlugin(),
new MiniCssExtractPlugin(),
new CompressionPlugin(),
new EventHooksPlugin({
done: () => {
if (argv.mode === "production") {
const source = "./dist/index.html.gz";
const destination = "./src/generated/html.h";
const wstream = fs.createWriteStream(destination);
wstream.on("error", function (err) {
console.log(err);
});
const data = fs.readFileSync(source);
wstream.write("#ifndef HTML_H\n");
wstream.write("#define HTML_H\n\n");
wstream.write("#include <Arduino.h>\n\n");
wstream.write(`#define html_len ${data.length}\n\n`);
wstream.write("const uint8_t html[] PROGMEM = {");
for (let i = 0; i < data.length; i++) {
if (i % 1000 == 0) {wstream.write("\n");}
wstream.write(`0x${(`00${data[i].toString(16)}`).slice(-2)}`);
if (i < data.length - 1) {wstream.write(",");}
}
wstream.write("\n};");
wstream.write("\n\n#endif\n");
wstream.end();
del([source]);
del("./dist/");
}
},
}),
],
});