From d283369627561ec79954e32b41c814cfe9ef0805 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 12 Aug 2020 16:35:48 +0200 Subject: [PATCH] adjust theme paths in html during build also discover themes from index.html to be used elsewhere in build later on --- scripts/build.mjs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 90c137f5..72b11dea 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -67,7 +67,16 @@ async function build() { if (legacy) { bundleName = `${PROJECT_ID}-legacy.js`; } - await buildHtml(version, bundleName); + + + const devHtml = await fs.readFile(path.join(projectDir, "index.html"), "utf8"); + const doc = cheerio.load(devHtml); + const themes = []; + findThemes(doc, themeName => { + themes.push(themeName); + }); + + await buildHtml(doc, version, bundleName); if (legacy) { await buildJsLegacy(bundleName); await buildCssLegacy(); @@ -82,11 +91,29 @@ async function build() { console.log(`built ${PROJECT_ID}${legacy ? " legacy" : ""} ${version} successfully`); } -async function buildHtml(version, bundleName) { +async function findThemes(doc, callback) { + doc("link[rel~=stylesheet][title]").each((i, el) => { + const theme = doc(el); + const href = theme.attr("href"); + const themesPrefix = "/themes/"; + const prefixIdx = href.indexOf(themesPrefix); + if (prefixIdx !== -1) { + const themeNameStart = prefixIdx + themesPrefix.length; + const themeNameEnd = href.indexOf("/", themeNameStart); + const themeName = href.substr(themeNameStart, themeNameEnd - themeNameStart); + callback(themeName, theme); + } + }); +} + +async function buildHtml(doc, version, bundleName) { // transform html file - const devHtml = await fs.readFile(path.join(projectDir, "index.html"), "utf8"); - const doc = cheerio.load(devHtml); - doc("link[rel=stylesheet]").attr("href", `${PROJECT_ID}.css`); + // change path to main.css to css bundle + doc("link[rel=stylesheet]:not([title])").attr("href", `${PROJECT_ID}.css`); + // change paths to all theme stylesheets + findThemes(doc, (themeName, theme) => { + theme.attr("href", `themes/${themeName}/bundle.css`); + }); doc("script#main").replaceWith( `` + ``);