build(frontend): create well-known based on new i18n.json

This commit is contained in:
Sarah Vaupel 2024-09-19 13:02:46 +02:00
parent 793ed1af32
commit 734c82eff7
2 changed files with 26 additions and 44 deletions

5
config/i18n.json Normal file
View File

@ -0,0 +1,5 @@
{
"_i18n": true,
"de-de-formal": "Ein webbasiertes Schulungsverwaltungssystem",
"en-eu": "A web-based training management system"
}

View File

@ -29,10 +29,14 @@ import webpackJson from 'webpack/package.json' assert { type: "json" };
const webpackVersion = webpackJson.version.split('.').slice(0, 2).join('.');
import packageJson from './package.json' assert { type: "json" };
const packageVersion = packageJson.version;
import i18nJson from './config/i18n.json' assert { type: 'json' };
async function webpackConfig() {
const wellKnownCacheDir = resolve('.cache/well-known');
const assetsDirectory = resolve('assets');
const wellKnownDir = 'well-known';
const wellKnownCacheDir = '.cache/well-known';
const staticDir = 'static';
const assetsDir = 'assets';
const faviconsDir = `${assetsDir}/favicons`;
return {
module: {
@ -118,8 +122,8 @@ async function webpackConfig() {
serialize: yaml.dump
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [ resolve('static'),
resolve('well-known'),
cleanOnceBeforeBuildPatterns: [ resolve(staticDir),
resolve(wellKnownDir),
]
}),
new webpack.IgnorePlugin({
@ -129,47 +133,28 @@ async function webpackConfig() {
new webpack.DefinePlugin({
VERSION: JSON.stringify(packageVersion)
}),
/* TODO: deprecated due to removal of online favicon generation; is this still needed?
...(() => {
const langs = new Set();
function findLangs(json) {
function findLangs(json, langs = new Set()) {
if (json && json._i18n) {
Object.keys(json).forEach(key => {
if (key !== '_i18n') {
langs.add(key);
}
})
} else if (Array.isArray(json)) {
json.forEach(elem => findLangs(elem));
} else if (typeof json === 'object') {
Object.keys(json).forEach(key => findLangs(json[key]));
}
}
findLangs(faviconJson);
function selectLang(lang, json) {
if (json && json._i18n) {
return json[lang];
} else if (Array.isArray(json)) {
return json.map(elem => selectLang(lang, elem));
} else if (typeof json === 'object') {
return Object.fromEntries(Object.entries(json).map(([k, v]) => [k, selectLang(lang, v)]));
} else {
return json;
console.error('Invalid i18nJson format!');
}
return langs;
}
const langJsons = {};
Array.from(langs).forEach(lang => {
langJsons[lang] = selectLang(lang, faviconJson);
});
findLangs(i18nJson, langs);
const cacheHash = crypto.createHash('sha256');
cacheHash.update(JSON.stringify(langJsons));
cacheHash.update(JSON.stringify(langs));
const cacheFiles = new Set([
...(Array.from(langs).map(lang => resolve(langJsons[lang].masterPicture))),
resolve('config/robots.txt'),
resolve('assets/favicon.svg'),
]);
for (const cacheFile of cacheFiles) {
@ -180,7 +165,7 @@ async function webpackConfig() {
let cachedVersion = undefined;
const versionFile = resolve('.well-known-cache', `${cacheDigest}.version`);
const versionFile = resolve(wellKnownCacheDir, `${cacheDigest}.version`);
try {
if (fs.existsSync(versionFile)) {
cachedVersion = fs.readFileSync(versionFile, 'utf8');
@ -192,32 +177,24 @@ async function webpackConfig() {
const versionDigest = cacheHash.digest('hex');
return Array.from(langs).map(lang => {
const tmpobj = tmp.fileSync({ postfix: ".json" });
fs.writeSync(tmpobj.fd, JSON.stringify(faviconConfig));
fs.close(tmpobj.fd);
return [
new CopyPlugin({
patterns: [
{ from: 'config/robots.txt', to: resolve('well-known', lang, 'robots.txt') },
{ from: 'config/robots.txt', to: resolve(wellKnownDir, lang, 'robots.txt') },
{ from: `${faviconsDir}/include.html`, to: resolve(wellKnownDir, lang, 'html_code.html') },
{ from: `${faviconsDir}/*.png`, to: resolve(wellKnownDir, lang, '[name][ext]') },
]
}),
{ apply: compiler => compiler.hooks.afterEmit.tap('AfterEmitPlugin', compilation => {
const imgFiles = globSync(resolve('well-known', lang) + '/*.@(png)');
const imgFilesArgs = Array.from(imgFiles).join(" ");
execSync(`exiftool -overwrite_original -all= ${imgFilesArgs}`, { stdio: 'inherit' });
})
}
];
}).flat(1);
})() */
})()
],
output: {
chunkFilename: '[chunkhash].js',
filename: '[chunkhash].js',
path: resolve('static', `wp-${webpackVersion}`),
publicPath: `/static/res/wp-${webpackVersion}/`,
path: resolve(staticDir, `wp-${webpackVersion}`),
publicPath: `/${staticDir}/res/wp-${webpackVersion}/`,
hashFunction: 'shake256',
hashDigestLength: 36
},