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.
This commit is contained in:
Bruno Windels 2020-08-12 16:37:55 +02:00
parent d283369627
commit c3dc01283d

View file

@ -43,6 +43,7 @@ const PROJECT_NAME = "Hydrogen Chat";
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); const __dirname = dirname(__filename);
const projectDir = path.join(__dirname, "../"); const projectDir = path.join(__dirname, "../");
const cssDir = path.join(projectDir, "src/ui/web/css/");
const targetDir = path.join(projectDir, "target"); const targetDir = path.join(projectDir, "target");
const {debug, noOffline, legacy} = process.argv.reduce((params, param) => { const {debug, noOffline, legacy} = process.argv.reduce((params, param) => {
@ -76,6 +77,10 @@ async function build() {
themes.push(themeName); 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); await buildHtml(doc, version, bundleName);
if (legacy) { if (legacy) {
await buildJsLegacy(bundleName); 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) { async function buildHtml(doc, version, bundleName) {
// transform html file // transform html file
// change path to main.css to css bundle // 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"); await fs.writeFile(path.join(targetDir, `${PROJECT_ID}.css`), result.css, "utf8");
} }
function removeOrEnableScript(scriptNode, enable) { function removeOrEnableScript(scriptNode, enable) {
if (enable) { if (enable) {
scriptNode.attr("type", "text/javascript"); scriptNode.attr("type", "text/javascript");
@ -234,9 +259,7 @@ function removeOrEnableScript(scriptNode, enable) {
async function removeDirIfExists(targetDir) { async function removeDirIfExists(targetDir) {
try { try {
const files = await fs.readdir(targetDir); await fs.rmdir(targetDir, {recursive: true});
await Promise.all(files.map(filename => fs.unlink(path.join(targetDir, filename))));
await fs.rmdir(targetDir);
} catch (err) { } catch (err) {
if (err.code !== "ENOENT") { if (err.code !== "ENOENT") {
throw err; 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)); build().catch(err => console.error(err));