-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathwebpack.config.js
194 lines (175 loc) · 6.62 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// This file is part of the Prusa Link Web
// Copyright (C) 2021 Prusa Research a.s. - www.prusa3d.com
// SPDX-License-Identifier: GPL-3.0-or-later
const path = require("path");
const packageFile = require("./package.json");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const PreprocessingPlugin = require("./tools/preprocessing");
const devServer = require("./tools/server");
const DEFAULT_NAME = "Original Prusa 3D Printer";
const BACKEND_URL = process.env.BACKEND_URL;
function withDefault(value, defaultValue) {
return value === undefined ? defaultValue : value;
}
module.exports = (env, args) => {
const buildLocales = env.locales;
var printerName = env["PRINTER_NAME"] || DEFAULT_NAME;
var config = {
PRINTER_NAME: printerName,
PRINTER_CODE: printerName.split(" ").slice(-1)[0].toLowerCase(),
PRINTER_TYPE: env["PRINTER_TYPE"] || "fdm", // "fdm" | "sla"
FILE_EXTENSIONS: env["FILE_EXTENSIONS"] || [".gcode"],
APP_NAME: env["APP_NAME"] || "PrusaLink",
APP_TITLE: env["APP_TITLE"] || env["PRINTER_NAME"] || DEFAULT_NAME,
APP_VERSION: packageFile.version,
MODE: env.dev ? "development" : "production",
UPDATE_INTERVAL: env["UPDATE_INTERVAL"] || 1000,
CONNECTION_UPDATE_INTERVAL: env["CONNECTION_UPDATE_INTERVAL"] || 5000,
HTTP_APIKEY: env["HTTP_APIKEY"] || env["http-apikey"] || false,
HTTP_BASIC: env["HTTP_BASIC"] || env["http-basic"] || false,
LOCAL_STORAGE_NAME: env["LOCAL_STORAGE_NAME"] || "PrusaLink G-codes",
WITH_STORAGES: env["WITH_STORAGES"] || ["local", "sdcard"],
WITH_FILES: withDefault(env["WITH_FILES"], true),
WITH_SETTINGS: env["WITH_SETTINGS"] || false,
WITH_CONTROLS: env["WITH_CONTROLS"] || false,
WITH_REMOTE_UPLOAD: withDefault(env["WITH_REMOTE_UPLOAD"], true),
WITH_START_PRINT_AFTER_UPLOAD: withDefault(env["WITH_START_PRINT_AFTER_UPLOAD"], true),
WITH_SERIAL: withDefault(env["WITH_SERIAL"], true),
WITH_CONNECTION: withDefault(env["WITH_CONNECTION"], true),
WITH_LOGS: env["WITH_LOGS"] || false,
WITH_EMBEDDED_SVGS: env["WITH_EMBEDDED_SVGS"] || false,
WITH_COMMAND_SELECT: withDefault(env["WITH_COMMAND_SELECT"], true),
WITH_V1_API: withDefault(env["WITH_V1_API"], false),
WITH_PRINT_BUTTON: withDefault(env["WITH_PRINT_BUTTON"], true),
WITH_PREVIEW_LAZY_QUEUE: withDefault(env["WITH_PREVIEW_LAZY_QUEUE"], false),
WITH_DOWNLOAD_BUTTON: withDefault(env["WITH_DOWNLOAD_BUTTON"], true),
WITH_FAILURES_BEFORE_OFFLINE_SCREEN: withDefault(env["WITH_FAILURES_BEFORE_OFFLINE_SCREEN"], 3),
WITH_TELEMETRY_NOZZLE_DIAMETER: withDefault(env["WITH_TELEMETRY_NOZZLE_DIAMETER"], false),
WITH_TELEMETRY_MATERIAL: withDefault(env["WITH_TELEMETRY_MATERIAL"], false),
WITH_API_KEY_AUTH: withDefault(env["WITH_API_KEY_AUTH"], false),
WITH_CAMERAS: withDefault(env["WITH_CAMERAS"], false),
WITH_API_KEY_SETTING: withDefault(env["WITH_API_KEY_SETTING"], false),
WITH_NAME_SORTING_ONLY: withDefault(env["WITH_NAME_SORTING_ONLY"], false),
WITH_SYSTEM_UPDATES: withDefault(env["WITH_SYSTEM_UPDATES"], false),
WITH_SYSTEM_VERSION: withDefault(env["WITH_SYSTEM_VERSION"], false),
WITH_PRINTER_SETTINGS: withDefault(env["WITH_PRINTER_SETTINGS"], false),
WITH_USER_SETTINGS: withDefault(env["WITH_USER_SETTINGS"], false),
WITH_SERIAL: withDefault(env["WITH_SERIAL"], false),
};
config["TPL_ASSETS_PATH"] = config["PRINTER_CODE"] == "m1" ? "../assets/m1" : "../assets";
const env_variables = Object.fromEntries(
Object.entries(config)
.map(([key, value]) => [`process.env.${key}`, JSON.stringify(value)])
);
if (buildLocales) {
console.log(`===== ${config.APP_TITLE} =====`);
console.log("building locales");
console.log(`=============================`);
} else {
console.log(`===== ${config.APP_TITLE} =====`);
console.log(config);
console.log(`=============================`);
}
const preprocessing = new PreprocessingPlugin({
config,
templates_dir: path.resolve(__dirname, "templates"),
assets_dir: path.resolve(__dirname, "src" + config["TPL_ASSETS_PATH"].slice(2)),
output_dir: path.resolve(__dirname, "src/views"),
});
let commitHash = process.env['GIT_COMMIT_HASH'] || require('child_process')
.execSync('git rev-parse --short HEAD')
.toString()
.trim();
return {
mode: config.MODE,
entry: "./src/index.js",
output: {
filename: "[name].[contenthash].js",
path: path.resolve(__dirname, "dist"),
publicPath: "",
},
devtool: env.dev ? "source-map" : false,
plugins: [
preprocessing,
new webpack.ProgressPlugin(),
new webpack.DefinePlugin(env_variables),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: "[name].[contenthash].css",
}),
new HtmlWebpackPlugin({
template: "./src/views/index.html",
minify: !env.dev,
}),
new webpack.DefinePlugin({
__COMMIT_HASH__: JSON.stringify(commitHash)
})
],
module: {
rules: [
{
test: /.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
importLoaders: 1,
},
},
"postcss-loader",
],
},
{
test: /\.html$/i,
loader: "html-loader",
},
buildLocales ?
{
test: /\.html$/i,
loader: path.resolve(__dirname, "tools/loaders/locale_loader_html"),
include: path.resolve(__dirname, "src/views/"),
} : {},
{
test: /\.(png|jpe?g|gif|svg|woff2?)$/i,
use: [
{
loader: "file-loader",
},
],
},
buildLocales ?
{
test: /\.js/,
loader: path.resolve(__dirname, "tools/loaders/locale_loader_js"),
exclude: path.resolve(__dirname, "node_modules"),
} : {},
],
},
optimization: {
minimizer: [new TerserPlugin()],
},
devServer: {
host: "0.0.0.0",
port: 9000,
...(
BACKEND_URL ? {
proxy: {
"/": BACKEND_URL
}
}: {
static: path.join(__dirname, "dist"),
compress: true,
onAfterSetupMiddleware: function (server) {
devServer(server.app, config);
preprocessing.startWatcher(server);
},
}
)
},
};
};