Skip to content

Commit b44320b

Browse files
committed
Improvements for single branch build
1 parent 3cea73a commit b44320b

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

website/build.mjs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ async function buildDocsMenu(item) {
4545
result += '</ul></li>';
4646
return result;
4747
}
48+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
4849

49-
return `<li><a href="${ item.url }" class="with-docs-version" data-default-version="${ DEFAULT_VERSION }">${ item.title }</a></li>`;
50+
return `<li><a href="${ item.url }" class="with-docs-version" data-default-version="${ defaultVersion }">${ item.title }</a></li>`;
5051
}
5152

5253
const docsMenus = [];
@@ -88,15 +89,17 @@ async function buildDocsMenuForVersion(version) {
8889
}
8990

9091
async function buildPlaygroundMenuForVersion(versions, currentVersion) {
91-
let defaultVersionTag = `<a href="./playground">${ DEFAULT_VERSION } (default)</a>`;
92+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
93+
let defaultVersionTag = `<a href="./playground">${ defaultVersion } (default)</a>`;
9294
if (currentVersion === '') {
93-
currentVersion = `${ DEFAULT_VERSION } (default)`;
94-
defaultVersionTag = `<a href="./playground" class="active">${ DEFAULT_VERSION } (default)</a>`;
95+
currentVersion = `${ defaultVersion } (default)`;
96+
defaultVersionTag = `<a href="./playground" class="active">${ defaultVersion } (default)</a>`;
9597
}
9698
let versionsMenuHtml = `<div class="dropdown versions-menu"><div class="dropdown-wrapper"><a href="#" class="current">${
9799
currentVersion }</a><div class="dropdown-block">${ defaultVersionTag }`;
98100
if (versions.length >= 1) {
99101
for (const v of versions) {
102+
if (v === BRANCH) continue;
100103
const activityClass = v === currentVersion ? ' class="active"' : '';
101104
versionsMenuHtml += `<a href="./${ v }/playground"${ activityClass }>${ v }</a>`;
102105
}
@@ -107,9 +110,11 @@ async function buildPlaygroundMenuForVersion(versions, currentVersion) {
107110
}
108111

109112
async function buildVersionsMenuList(versions, currentVersion) {
110-
let versionsMenuHtml = `<div class="dropdown-block"><a href="./docs/">${ DEFAULT_VERSION } (default)</a>`;
113+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
114+
let versionsMenuHtml = `<div class="dropdown-block"><a href="./docs/">${ defaultVersion } (default)</a>`;
111115
if (versions.length >= 1) {
112116
for (const v of versions) {
117+
if (v === BRANCH) continue;
113118
const activityClass = v === currentVersion ? ' class="active"' : '';
114119
versionsMenuHtml += `<a href="./${ v }/docs/"${ activityClass }>${ v }</a>`;
115120
}
@@ -151,6 +156,7 @@ const linkRenderer = {
151156
};
152157

153158
function buildMenus(html) {
159+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
154160
const headings = getHeadingList().filter(({ level }) => level > 1);
155161
let result = '<div class="wrapper">';
156162
if (isBlog) {
@@ -163,7 +169,7 @@ function buildMenus(html) {
163169
result += `<div class="table-of-contents sticky"><div class="mobile-trigger"></div>
164170
${ headings.map(({ id, raw, level }) => `<div class="toc-link"><a href="${
165171
htmlFileName.replace('.html', '') }#${ id }" class="h${
166-
level } with-docs-version" data-default-version="${ DEFAULT_VERSION }">${
172+
level } with-docs-version" data-default-version="${ defaultVersion }">${
167173
raw }</a></div>`).join('\n') }
168174
</div>`;
169175
}
@@ -235,13 +241,14 @@ let versionsCache = [];
235241
async function getVersionsFromMdFiles(mdFiles) {
236242
if (versionsCache.length) return versionsCache;
237243

244+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
238245
const versions = [];
239246
for (const mdPath of mdFiles) {
240247
const match = mdPath.match(/\/web\/(?<version>[^/]+)\/docs\//);
241248
if (match && match.groups && match.groups.version) {
242249
versions.push(match.groups.version);
243250
} else {
244-
versions.push(DEFAULT_VERSION);
251+
versions.push(defaultVersion);
245252
}
246253
}
247254
versionsCache = versions;
@@ -275,17 +282,20 @@ async function buildPlayground(template, version, versions) {
275282
playground = playground.replace('{core-js-bundle-esmodules}', `${ bundleESModulesScript }`);
276283
const playgroundWithVersion = playground.replace('{versions-menu}', `${ versionsMenu }`);
277284
const playgroundFilePath = path.join(RESULT_DIR, version, 'playground.html');
278-
await fs.mkdir(path.dirname(playgroundFilePath), { recursive: true });
279-
await fs.writeFile(playgroundFilePath, playgroundWithVersion, 'utf8');
280-
if (version === DEFAULT_VERSION) {
285+
if (version !== BRANCH) {
286+
await fs.mkdir(path.dirname(playgroundFilePath), { recursive: true });
287+
await fs.writeFile(playgroundFilePath, playgroundWithVersion, 'utf8');
288+
echo(chalk.green(`File created: ${ playgroundFilePath }`));
289+
}
290+
291+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
292+
if (version === defaultVersion) {
281293
const defaultVersionsMenu = await buildPlaygroundMenuForVersion(versions, '');
282294
const defaultVersionPlayground = playground.replace('{versions-menu}', `${ defaultVersionsMenu }`);
283295
const defaultPlaygroundPath = path.join(RESULT_DIR, 'playground.html');
284296
await fs.writeFile(defaultPlaygroundPath, defaultVersionPlayground, 'utf8');
285297
echo(chalk.green(`File created: ${ defaultPlaygroundPath }`));
286298
}
287-
288-
echo(chalk.green(`File created: ${ playgroundFilePath }`));
289299
}
290300

291301
async function createDocsIndexes(versions) {
@@ -319,8 +329,9 @@ async function build() {
319329
const mdFiles = await getAllMdFiles(DOCS_DIR);
320330
const versions = await getVersionsFromMdFiles(mdFiles);
321331
const uniqueVersions = [...new Set(versions)];
322-
const bundleScript = `<script nomodule src="${ BUNDLES_PATH }/${ DEFAULT_VERSION }/${ BUNDLE_NAME }"></script>`;
323-
const bundleESModulesScript = `<script type="module" src="${ BUNDLES_PATH }/${ DEFAULT_VERSION }/${ BUNDLE_NAME_ESMODULES }"></script>`;
332+
const defaultVersion = BRANCH ? BRANCH : DEFAULT_VERSION;
333+
const bundleScript = `<script nomodule src="${ BUNDLES_PATH }/${ defaultVersion }/${ BUNDLE_NAME }"></script>`;
334+
const bundleESModulesScript = `<script type="module" src="${ BUNDLES_PATH }/${ defaultVersion }/${ BUNDLE_NAME_ESMODULES }"></script>`;
324335

325336
let currentVersion = '';
326337
let versionsMenu = '';

0 commit comments

Comments
 (0)