Localization refactoring:
* the front-end now use global constants, based on the translation key * build-client.js use the ESBuild "define" directive to replace these globals at compile time, by the english value * build:client must now be called after build:languages * moving the loadLoc and loc backend functions in a separate lib
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
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
|
||||
@ -11,15 +12,40 @@ const clientFiles = [
|
||||
'admin-plugin-client-plugin'
|
||||
]
|
||||
|
||||
function loadLocs() {
|
||||
// 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 globalFile = path.resolve(__dirname, 'client', '@types', 'global.d.ts')
|
||||
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())
|
||||
|
||||
const configs = clientFiles.map(f => ({
|
||||
entryPoints: [ path.resolve(__dirname, 'client', f + '.ts') ],
|
||||
alias: {
|
||||
'shared': path.resolve(__dirname, 'shared/')
|
||||
},
|
||||
define: {
|
||||
PLUGIN_CHAT_PACKAGE_NAME: JSON.stringify(packagejson.name),
|
||||
PLUGIN_CHAT_SHORT_NAME: JSON.stringify(packagejson.name.replace(/^peertube-plugin-/, ''))
|
||||
},
|
||||
define,
|
||||
bundle: true,
|
||||
minify: true,
|
||||
// FIXME: sourcemap:`true` does not work for now, because peertube does not serve static files.
|
||||
|
Reference in New Issue
Block a user