-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
build-client.js
88 lines (76 loc) · 2.7 KB
/
build-client.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
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
const path = require('path')
const esbuild = require('esbuild')
const fs = require('fs')
const packagejson = require('./package.json')
const sourcemap = process.env.NODE_ENV === 'dev' ? 'inline' : false
const clientFiles = [
// Client files list, without the file extension:
'common-client-plugin',
'admin-plugin-client-plugin'
]
function loadLocs(globalFile) {
// Loading english strings, so we can inject them as constants.
const refFile = path.resolve(__dirname, 'dist', 'languages', 'en.reference.json')
if (!fs.existsSync(refFile)) {
throw new Error('Missing english reference file, please run "npm run build:languages" before building the client')
}
const english = require(refFile)
// Reading client/@types/global.d.ts, to have a list of needed localized strings.
const r = {}
const globalFileContent = '' + fs.readFileSync(globalFile)
const matches = globalFileContent.matchAll(/^declare const LOC_(\w+)\b/gm)
for (const match of matches) {
const key = match[1].toLowerCase()
if (!(key in english) || (typeof english[key] !== 'string')) {
throw new Error('Missing english string key=' + key)
}
r['LOC_' + match[1]] = JSON.stringify(english[key])
}
return r
}
const define = Object.assign({
PLUGIN_CHAT_PACKAGE_NAME: JSON.stringify(packagejson.name),
PLUGIN_CHAT_SHORT_NAME: JSON.stringify(packagejson.name.replace(/^peertube-plugin-/, ''))
}, loadLocs(path.resolve(__dirname, 'client', '@types', 'global.d.ts')))
const configs = clientFiles.map(f => ({
entryPoints: [ path.resolve(__dirname, 'client', f + '.ts') ],
alias: {
'shared': path.resolve(__dirname, 'shared/')
},
define,
bundle: true,
minify: true,
// FIXME: sourcemap:`true` does not work for now, because peertube does not serve static files.
// See https://github.com/Chocobozzz/PeerTube/issues/5185
sourcemap,
format: 'esm',
target: 'safari11',
outfile: path.resolve(__dirname, 'dist/client', f + '.js'),
}))
const defineBuiltin = Object.assign(
{},
loadLocs(path.resolve(__dirname, 'conversejs', 'lib', '@types', 'global.d.ts'))
)
configs.push({
entryPoints: ["./conversejs/builtin.ts"],
define: defineBuiltin,
bundle: true,
minify: true,
sourcemap,
target: 'safari11',
outfile: path.resolve(__dirname, 'dist/client/static', 'builtin.js'),
})
configs.push({
entryPoints: ["./client/settings.ts"],
bundle: true,
minify: true,
sourcemap,
target: 'safari11',
outfile: path.resolve(__dirname, 'dist/client/settings', 'settings.js'),
})
const promises = configs.map(c => esbuild.build(c))
Promise.all(promises)
.catch(() => process.exit(1))