-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy pathDevBuild.js
313 lines (284 loc) · 10.8 KB
/
DevBuild.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import path, { dirname } from "node:path"
import fs from "fs-extra"
import { getCanonicalPlatformName, getTutanotaAppVersion, getValidArchitecture, runStep, writeFile } from "./buildUtils.js"
import "zx/globals"
import * as env from "./env.js"
import { preludeEnvPlugin } from "./env.js"
import { fileURLToPath } from "node:url"
import * as LaunchHtml from "./LaunchHtml.js"
import os from "node:os"
import { checkOfflineDatabaseMigrations } from "./checkOfflineDbMigratons.js"
import { buildRuntimePackages } from "./packageBuilderFunctions.js"
import { domainConfigs } from "./DomainConfigs.js"
import { sh } from "./sh.js"
import { rolldown } from "rolldown"
import { resolveLibs } from "./RollupConfig.js"
import { nodeGypPlugin } from "./nodeGypPlugin.js"
import { napiPlugin } from "./napiPlugin.js"
const buildSrc = dirname(fileURLToPath(import.meta.url))
const projectRoot = path.resolve(path.join(buildSrc, ".."))
export async function runDevBuild({ stage, host, desktop, clean, ignoreMigrations, app }) {
const isCalendarBuild = app === "calendar"
const tsConfig = isCalendarBuild ? "tsconfig-calendar-app.json" : "tsconfig.json"
const buildDir = isCalendarBuild ? "build-calendar-app" : "build"
const liboqsIncludeDir = "libs/webassembly/include"
console.log("Building dev for", app)
if (clean) {
await runStep("Clean", async () => {
await fs.emptyDir(buildDir)
await fs.rm(liboqsIncludeDir, { recursive: true, force: true })
})
}
await runStep("Validate", () => {
if (ignoreMigrations) {
console.warn("CAUTION: Offline migrations are not being validated.")
} else {
checkOfflineDatabaseMigrations()
}
})
await runStep("Packages", async () => {
await buildRuntimePackages()
})
const version = await getTutanotaAppVersion()
await runStep("Types", async () => {
await sh`npx tsc --project ${tsConfig} --incremental ${true} --noEmit true`
})
/**
* @param host {string|null}
* @return {DomainConfigMap}
*/
function updateDomainConfigForHostname(host) {
if (host == null) {
return { ...domainConfigs }
} else {
const url = new URL(host)
const { protocol, hostname } = url
const port = parseInt(url.port)
// the URL object does not include the port if it is the schema's default
const uri = port ? `${protocol}//${hostname}:${port}` : `${protocol}//${hostname}`
return {
...domainConfigs,
[url.hostname]: {
firstPartyDomain: true,
partneredDomainTransitionUrl: uri,
apiUrl: uri,
paymentUrl: `${uri}/braintree.html`,
webauthnUrl: `${uri}/webauthn`,
legacyWebauthnUrl: `${uri}/webauthn`,
webauthnMobileUrl: `${uri}/webauthnmobile`,
legacyWebauthnMobileUrl: `${uri}/webauthnmobile`,
webauthnRpId: hostname,
u2fAppId: `${uri}/u2f-appid.json`,
giftCardBaseUrl: `${uri}/giftcard`,
referralBaseUrl: `${uri}/signup`,
websiteBaseUrl: "https://tuta.com",
},
}
}
}
const extendedDomainConfigs = updateDomainConfigForHostname(host)
await buildWebPart({ stage, host, version, domainConfigs: extendedDomainConfigs, app })
if (desktop) {
await buildDesktopPart({ version, app })
}
}
/**
* @param p {object}
* @param p.stage {string}
* @param p.host {string|null}
* @param p.version {string}
* @param p.domainConfigs {DomainConfigMap}
* @param p.app {string}
* @return {Promise<void>}
*/
async function buildWebPart({ stage, host, version, domainConfigs, app }) {
const isCalendarBuild = app === "calendar"
const buildDir = isCalendarBuild ? "build-calendar-app" : "build"
const resolvedBuildDir = path.resolve(buildDir)
const entryFile = isCalendarBuild ? "src/calendar-app/calendar-app.ts" : "src/mail-app/app.ts"
const workerFile = isCalendarBuild ? "src/calendar-app/workerUtils/worker/calendar-worker.ts" : "src/mail-app/workerUtils/worker/mail-worker.ts"
await runStep("Web: Rolldown", async () => {
const { rollupWasmLoader } = await import("@tutao/tuta-wasm-loader")
const bundle = await rolldown({
input: { app: entryFile, worker: workerFile },
define: {
// Need it at least until inlining enums is supported
LOAD_ASSERTIONS: "false",
},
external: "fs", // qrcode-svg tries to import it on save()
plugins: [
resolveLibs(),
rollupWasmLoader({
webassemblyLibraries: [
{
name: "liboqs.wasm",
command: "make -f Makefile_liboqs build",
workingDir: "libs/webassembly/",
outputPath: path.join(resolvedBuildDir, `/wasm/liboqs.wasm`),
},
{
name: "argon2.wasm",
command: "make -f Makefile_argon2 build",
workingDir: "libs/webassembly/",
outputPath: path.join(resolvedBuildDir, `/wasm/argon2.wasm`),
},
],
}),
],
})
await bundle.write({
dir: `./${buildDir}/`,
format: "esm",
// Setting source map to inline for web part because source maps won't be loaded correctly on mobile because requests from dev tools are not
// intercepted, so we can't serve the files.
sourcemap: "inline",
// overwrite the files rather than keeping all versions in the build folder
chunkFileNames: "[name]-chunk.js",
})
})
// Do assets last so that server that listens to index.html changes does not reload too early
await runStep("Web: Assets", async () => {
await prepareAssets(stage, host, version, domainConfigs, buildDir)
await fs.promises.writeFile(
`${buildDir}/worker-bootstrap.js`,
`import "./polyfill.js"
import "./worker.js"
`,
)
})
}
async function buildDesktopPart({ version, app }) {
const isCalendarBuild = app === "calendar"
const buildDir = isCalendarBuild ? "build-calendar-app" : "build"
await runStep("Desktop: Rolldown", async () => {
const bundle = await rolldown({
input: ["src/common/desktop/DesktopMain.ts", "src/common/desktop/sqlworker.ts"],
platform: "node",
external: [
"electron",
"memcpy", // optional dep of oxmsg
],
plugins: [
resolveLibs(),
nodeGypPlugin({
rootDir: projectRoot,
platform: getCanonicalPlatformName(process.platform),
architecture: getValidArchitecture(process.platform, process.arch),
nodeModule: "better-sqlite3",
environment: "electron",
}),
napiPlugin({
nodeModule: "@tutao/node-mimimi",
platform: getCanonicalPlatformName(process.platform),
architecture: getValidArchitecture(process.platform, process.arch),
}),
preludeEnvPlugin(env.create({ staticUrl: null, version, mode: "Desktop", dist: false, domainConfigs })),
],
})
await bundle.write({
dir: `./${buildDir}/desktop`,
format: "esm",
sourcemap: true,
// overwrite the files rather than keeping all versions in the build folder
chunkFileNames: "[name]-chunk.js",
})
})
await runStep("Desktop: assets", async () => {
const desktopIconsPath = "./resources/desktop-icons"
await fs.copy(desktopIconsPath, `./${buildDir}/desktop/resources/icons`, { overwrite: true })
await fs.move(`./${buildDir}/desktop/resources/icons/logo-solo-dev.png`, `./${buildDir}/desktop/resources/icons/logo-solo-red.png`, { overwrite: true })
await fs.move(`./${buildDir}/desktop/resources/icons/logo-solo-dev-small.png`, `./${buildDir}/desktop/resources/icons/logo-solo-red-small.png`, {
overwrite: true,
})
const templateGenerator = (await import("./electron-package-json-template.js")).default
const packageJSON = await templateGenerator({
nameSuffix: "-debug",
version,
updateUrl: `http://localhost:9000/client/${buildDir}`,
iconPath: path.join(desktopIconsPath, "logo-solo-red.png"),
sign: false,
architecture: "x64",
})
const content = JSON.stringify(packageJSON, null, 2)
await fs.createFile(`./${buildDir}/package.json`)
await fs.writeFile(`./${buildDir}/package.json`, content, "utf-8")
await fs.mkdir(`${buildDir}/desktop`, { recursive: true })
// The preload scripts are run as commonjs scripts and are a special environment so we just copy them directly.
await fs.copyFile("src/common/desktop/preload.js", `${buildDir}/desktop/preload.js`)
await fs.copyFile("src/common/desktop/preload-webdialog.js", `${buildDir}/desktop/preload-webdialog.js`)
})
}
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const root = __dirname.split(path.sep).slice(0, -1).join(path.sep)
async function createBootstrap(env, buildDir) {
let jsFileName
let htmlFileName
switch (env.mode) {
case "App":
jsFileName = "index-app.js"
htmlFileName = "index-app.html"
break
case "Browser":
jsFileName = "index.js"
htmlFileName = "index.html"
break
case "Desktop":
jsFileName = "index-desktop.js"
htmlFileName = "index-desktop.html"
}
const imports = [{ src: "polyfill.js" }, { src: jsFileName }]
const template = `window.whitelabelCustomizations = null
window.env = ${JSON.stringify(env, null, 2)}
if (env.staticUrl == null && window.tutaoDefaultApiUrl) {
// overriden by js dev server
window.env.staticUrl = window.tutaoDefaultApiUrl
}
import('./app.js')`
await writeFile(`./${buildDir}/${jsFileName}`, template)
const html = await LaunchHtml.renderHtml(imports, env)
await writeFile(`./${buildDir}/${htmlFileName}`, html)
}
function getStaticUrl(stage, mode, host) {
if (stage === "local" && mode === "Browser") {
// We would like to use web app build for both JS server and actual server. For that we should avoid hardcoding URL as server
// might be running as one of testing HTTPS domains. So instead we override URL when the app is served from JS server
// (see DevServer).
// This is only relevant for browser environment.
return null
} else if (stage === "test") {
return "https://app.test.tuta.com"
} else if (stage === "prod") {
return "https://app.tuta.com"
} else if (stage === "local") {
return "http://" + os.hostname() + ":9000"
} else {
// host
return host
}
}
/**
* @param stage {string}
* @param host {string|null}
* @param version {string}
* @param domainConfigs {DomainConfigMap}
* @param buildDir {string}
* @return {Promise<void>}
*/
export async function prepareAssets(stage, host, version, domainConfigs, buildDir) {
await Promise.all([
await fs.emptyDir(path.join(root, `${buildDir}/images`)),
fs.copy(path.join(root, "/resources/favicon"), path.join(root, `/${buildDir}/images`)),
fs.copy(path.join(root, "/resources/images/"), path.join(root, `/${buildDir}/images`)),
fs.copy(path.join(root, "/resources/pdf/"), path.join(root, `/${buildDir}/pdf`)),
fs.copy(path.join(root, "/resources/desktop-icons"), path.join(root, `/${buildDir}/icons`)),
fs.copy(path.join(root, "/resources/wordlibrary.json"), path.join(root, `${buildDir}/wordlibrary.json`)),
fs.copy(path.join(root, "/src/braintree.html"), path.join(root, `${buildDir}/braintree.html`)),
])
// write empty file
await fs.writeFile(`${buildDir}/polyfill.js`, "")
/** @type {EnvMode[]} */
const modes = ["Browser", "App", "Desktop"]
for (const mode of modes) {
await createBootstrap(env.create({ staticUrl: getStaticUrl(stage, mode, host), version, mode, dist: false, domainConfigs }), buildDir)
}
}