From c3dc01283d7f48e167d4ae9cc9acdb2d7d4ff6ba Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 12 Aug 2020 16:37:55 +0200 Subject: [PATCH] copy theme assets anything that is not a css file, which will be bundled woff(2) files are treated specially since we only need WOFF for legacy (IE11) and only WOFF2 for any modern browser. --- scripts/build.mjs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 72b11dea..08e44715 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -43,6 +43,7 @@ const PROJECT_NAME = "Hydrogen Chat"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const projectDir = path.join(__dirname, "../"); +const cssDir = path.join(projectDir, "src/ui/web/css/"); const targetDir = path.join(projectDir, "target"); const {debug, noOffline, legacy} = process.argv.reduce((params, param) => { @@ -76,6 +77,10 @@ async function build() { themes.push(themeName); }); + // also creates the directories where the theme css bundles are placed in, + // so do it first + const themeAssets = await copyThemeAssets(themes, legacy); + await buildHtml(doc, version, bundleName); if (legacy) { await buildJsLegacy(bundleName); @@ -106,6 +111,27 @@ async function findThemes(doc, callback) { }); } +async function copyThemeAssets(themes, legacy) { + const assets = []; + // create theme directories and copy assets + await fs.mkdir(path.join(targetDir, "themes")); + for (const theme of themes) { + assets.push(`themes/${theme}/bundle.css`); + const themeDstFolder = path.join(targetDir, `themes/${theme}`); + await fs.mkdir(themeDstFolder); + const themeSrcFolder = path.join(cssDir, `themes/${theme}`); + await copyFolder(themeSrcFolder, themeDstFolder, file => { + const isUnneededFont = legacy ? file.endsWith(".woff2") : file.endsWith(".woff"); + if (!file.endsWith(".css") && !isUnneededFont) { + assets.push(file.substr(cssDir.length)); + return true; + } + return false; + }); + } + return assets; +} + async function buildHtml(doc, version, bundleName) { // transform html file // change path to main.css to css bundle @@ -223,7 +249,6 @@ async function buildCssLegacy() { await fs.writeFile(path.join(targetDir, `${PROJECT_ID}.css`), result.css, "utf8"); } - function removeOrEnableScript(scriptNode, enable) { if (enable) { scriptNode.attr("type", "text/javascript"); @@ -234,9 +259,7 @@ function removeOrEnableScript(scriptNode, enable) { async function removeDirIfExists(targetDir) { try { - const files = await fs.readdir(targetDir); - await Promise.all(files.map(filename => fs.unlink(path.join(targetDir, filename)))); - await fs.rmdir(targetDir); + await fs.rmdir(targetDir, {recursive: true}); } catch (err) { if (err.code !== "ENOENT") { throw err; @@ -244,4 +267,18 @@ async function removeDirIfExists(targetDir) { } } +async function copyFolder(srcRoot, dstRoot, filter) { + const dirEnts = await fs.readdir(srcRoot, {withFileTypes: true}); + for (const dirEnt of dirEnts) { + const dstPath = path.join(dstRoot, dirEnt.name); + const srcPath = path.join(srcRoot, dirEnt.name); + if (dirEnt.isDirectory()) { + await fs.mkdir(dstPath); + await copyFolder(srcPath, dstPath, filter); + } else if (dirEnt.isFile() && filter(srcPath)) { + await fs.copyFile(srcPath, dstPath); + } + } +} + build().catch(err => console.error(err));