forked from padloc/padloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
173 lines (160 loc) · 6.55 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
const { resolve, join } = require("path");
const { EnvironmentPlugin, optimize } = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const { version } = require("./package.json");
const sharp = require("sharp");
const rootDir = resolve(__dirname, "../..");
const assetsDir = resolve(rootDir, process.env.PL_ASSETS_DIR || "assets");
const { name, icon_background, background_color, terms_of_service } = require(join(assetsDir, "manifest.json"));
module.exports = {
entry: resolve(__dirname, "src/index.ts"),
output: {
path: resolve(__dirname, "www"),
filename: "[name].js",
chunkFilename: "[name].chunk.js",
},
mode: "development",
devtool: "source-map",
stats: "minimal",
resolve: {
extensions: [".ts", ".js", ".css", ".svg", ".png", ".jpg"],
alias: {
assets: resolve(__dirname, assetsDir),
},
},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader",
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
use: ["file-loader"],
},
{
test: /\.txt|md$/i,
use: "raw-loader",
},
],
},
externals: {
cordova: "cordova",
"cordova-plugin-qrscanner": "window",
},
plugins: [
new EnvironmentPlugin({
PL_SERVER_URL: `http://localhost:${process.env.PL_SERVER_PORT || 3000}`,
PL_BILLING_ENABLED: null,
PL_BILLING_DISABLE_PAYMENT: null,
PL_BILLING_STRIPE_PUBLIC_KEY: null,
PL_SUPPORT_EMAIL: "[email protected]",
PL_VERSION: version,
PL_VENDOR_VERSION: version,
PL_DISABLE_SW: true,
PL_AUTH_DEFAULT_TYPE: null,
PL_APP_NAME: name,
PL_TERMS_OF_SERVICE: terms_of_service,
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: name,
template: resolve(__dirname, "src/index.html"),
meta: {
"Content-Security-Policy": {
"http-equiv": "Content-Security-Policy",
content: `default-src 'self' ${process.env.PL_SERVER_URL} https://api.pwnedpasswords.com blob:; style-src 'self' 'unsafe-inline'; object-src 'self' blob:; frame-src 'self'; img-src 'self' blob: data: https:;`,
},
},
}),
new optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
{
apply(compiler) {
compiler.hooks.emit.tapPromise("Prepare App Icons", async (compilation) => {
const iconPath = join(assetsDir, "app-icon.png");
const { width } = await sharp(iconPath).metadata();
const iosPadding = Math.floor(width / 10);
const androidPadding = Math.floor(width * 0.5);
const iosIcon = await sharp(iconPath)
.flatten({ background: icon_background })
.extend({
top: iosPadding,
right: iosPadding,
bottom: iosPadding,
left: iosPadding,
background: icon_background,
})
.toBuffer();
const androidIcon = await sharp(iconPath)
.extend({
top: androidPadding,
right: androidPadding,
bottom: androidPadding,
left: androidPadding,
background: { r: 0, b: 0, g: 0, alpha: 0 },
})
.toBuffer();
const iosIconSizes = [
20, 29, 40, 50, 57, 58, 60, 72, 76, 80, 87, 100, 114, 120, 144, 152, 167, 180,
];
const androidIconSizes = [36, 48, 72, 96, 144, 192];
await Promise.all([
...iosIconSizes.map(async (size) => {
const icon = await sharp(iosIcon)
.resize({
width: size,
height: size,
})
.png({ quality: 100 })
.toBuffer();
compilation.assets[`res/icons/ios/app-icon-${size}.png`] = {
source: () => icon,
size: () => Buffer.byteLength(icon),
};
}),
...androidIconSizes.map(async (size) => {
const icon = await sharp(androidIcon)
.resize({
width: size,
height: size,
})
.png({ quality: 100 })
.toBuffer();
compilation.assets[`res/icons/android/app-icon-${size}.png`] = {
source: () => icon,
size: () => Buffer.byteLength(icon),
};
}),
]);
const storeIcon = await sharp(iosIcon)
.resize({
width: 1024,
height: 1024,
})
.jpeg({ quality: 100 })
.toBuffer();
compilation.assets[`res/icons/ios/app-icon-1024.jpg`] = {
source: () => storeIcon,
size: () => Buffer.byteLength(storeIcon),
};
const colors = `<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">${icon_background || background_color || "#ffffff"}</color>
</resources>`;
compilation.assets["res/icons/android/colors.xml"] = {
source: () => colors,
size: () => colors.length,
};
return true;
});
},
},
],
};