Skip to content

Commit fa037e7

Browse files
committed
Versions path fix
1 parent 1711d67 commit fa037e7

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

website/build.mjs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDefaultVersion } from './scripts/helpers.mjs';
1+
import { expandVersionsConfig, getDefaultVersion } from './scripts/helpers.mjs';
22
import fm from 'front-matter';
33
import { JSDOM } from 'jsdom';
44
import { Marked } from 'marked';
@@ -20,14 +20,6 @@ let docsMenu = '';
2020
let isBlog = false;
2121
let isDocs = false;
2222

23-
async function getDefaultVersionTarget(versionFile, defaultTarget = null) {
24-
if (defaultTarget) return defaultTarget;
25-
26-
const versions = await readJson(versionFile);
27-
const defaultVersion = versions.find(v => v.default);
28-
return defaultVersion?.branch ?? defaultVersion?.tag;
29-
}
30-
3123
async function getAllMdFiles(dir) {
3224
const entries = await readdir(dir, { withFileTypes: true });
3325
const files = [];
@@ -89,12 +81,16 @@ async function buildDocsMenuForVersion(version) {
8981
return menu;
9082
}
9183

84+
function getBundlesPath(version) {
85+
return path.join(config.bundlesPath, version.branch ?? version.tag);
86+
}
87+
9288
async function buildVersionsMenuList(versions, currentVersion, section) {
9389
let versionsMenuHtml = '<div class="dropdown-block">';
9490
for (const v of versions) {
9591
const activityClass = v.label === currentVersion && !v.default ? ' class="active"' : '';
9692
const defaultBadge = v.default ? ' (default)' : '';
97-
const versionPath = v.default ? '' : `${ v.path ?? v.label }/`;
93+
const versionPath = v.default ? '' : `${ v.path }/`;
9894
versionsMenuHtml += `<a href="./${ versionPath }${ section }"${ activityClass }>${ v.label }${ defaultBadge }</a>`;
9995
}
10096
versionsMenuHtml += '</div>';
@@ -227,9 +223,9 @@ async function buildPlaygrounds(template, versions) {
227223
}
228224

229225
async function buildPlayground(template, version, versions) {
230-
const versionPath = version.branch ?? version.tag;
231-
const bundleScript = `<script nomodule src="${ config.bundlesPath }/${ versionPath }/${ config.bundleName }"></script>`;
232-
const bundleESModulesScript = `<script type="module" src="${ config.bundlesPath }/${ versionPath }/${ config.bundleNameESModules }"></script>`;
226+
const bundlesPath = getBundlesPath(version);
227+
const bundleScript = `<script nomodule src="${ bundlesPath }/${ config.bundleName }"></script>`;
228+
const bundleESModulesScript = `<script type="module" src="${ bundlesPath }/${ config.bundleNameESModules }"></script>`;
233229
const babelScript = '<script src="./babel.min.js"></script>';
234230
const playgroundContent = await readFileContent(`${ config.srcDir }playground.html`);
235231
const versionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
@@ -240,7 +236,7 @@ async function buildPlayground(template, version, versions) {
240236
playground = playground.replace('{core-js-bundle-esmodules}', bundleESModulesScript);
241237
playground = playground.replace('{babel-script}', babelScript);
242238
const playgroundWithVersion = playground.replace('{versions-menu}', versionsMenu);
243-
const playgroundFilePath = path.join(config.resultDir, versionPath, 'playground.html');
239+
const playgroundFilePath = path.join(config.resultDir, version.path, 'playground.html');
244240

245241
if (version.default) {
246242
const defaultVersionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
@@ -256,11 +252,11 @@ async function buildPlayground(template, version, versions) {
256252
}
257253

258254
async function createDocsIndexes(versions) {
259-
if (BRANCH) versions = [{ label: '' }];
255+
if (BRANCH) versions = [{ path: '' }];
260256

261257
for (const version of versions) {
262-
if (version.default) continue;
263-
const versionPath = version.path ?? version.label;
258+
if (version.default && versions.length > 1) continue;
259+
const versionPath = version.path;
264260
const menuItems = await getDocsMenuItems(versionPath);
265261
const firstDocPath = path.join(config.resultDir,
266262
`${ menuItems[0].url }.html`.replace(`{docs-version}${ BRANCH ? '/' : '' }`, versionPath));
@@ -273,15 +269,17 @@ async function createDocsIndexes(versions) {
273269
async function getVersions() {
274270
if (BRANCH) {
275271
return [{
276-
label: BRANCH,
277-
default: true,
278272
branch: BRANCH,
273+
default: true,
274+
label: BRANCH,
275+
path: BRANCH,
279276
}];
280277
}
278+
281279
const versions = await readJson(config.versionsFile);
282280
echo(chalk.green('Got versions from file'));
283281

284-
return versions;
282+
return expandVersionsConfig(versions);
285283
}
286284

287285
function getTitle(content) {
@@ -294,7 +292,8 @@ async function build() {
294292
await buildBlogMenu();
295293
const mdFiles = await getAllMdFiles(config.docsDir);
296294
const versions = await getVersions();
297-
const bundlesPath = `${ config.bundlesPath }/${ await getDefaultVersionTarget(config.versionsFile, BRANCH) }`;
295+
const [defaultVersion] = versions;
296+
const bundlesPath = getBundlesPath(defaultVersion);
298297
const bundleScript = `<script nomodule src="${ bundlesPath }/${ config.bundleName }"></script>`;
299298
const bundleESModulesScript = `<script type="module" src="${ bundlesPath }/${ config.bundleNameESModules }"></script>`;
300299

website/config/versions.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
"default": true,
55
"branch": "master"
66
},
7-
{
8-
"label": "v3",
9-
"branch": "master"
10-
},
117
{
128
"label": "v4 (alpha)",
13-
"path": "v4",
14-
"branch": "v4"
9+
"branch": "v4",
10+
"path": "v4"
1511
}
1612
]

website/scripts/helpers.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,23 @@ export async function readJSON(filePath) {
154154
const buffer = await readFile(filePath);
155155
return JSON.parse(buffer);
156156
}
157+
158+
export function expandVersionsConfig(config) {
159+
let defaultIndex = null;
160+
const $config = config.map(({ label, branch, path, default: $default }, index) => {
161+
if ($default) {
162+
if (defaultIndex !== null) throw new Error('Duplicate default');
163+
defaultIndex = index;
164+
}
165+
return {
166+
branch,
167+
default: false,
168+
label,
169+
path: path ?? label,
170+
};
171+
});
172+
173+
if (defaultIndex === null) throw new Error('Missed default');
174+
175+
return [{ ...$config[defaultIndex], default: true }, ...$config];
176+
}

website/scripts/runner.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/* eslint-disable no-console -- needed for logging */
22
import {
3-
hasDocs, copyBlogPosts, copyBabelStandalone, copyCommonFiles, buildWeb, getDefaultVersion, readJSON, buildAndCopyCoreJS,
3+
hasDocs,
4+
copyBlogPosts,
5+
copyBabelStandalone,
6+
copyCommonFiles,
7+
buildWeb,
8+
getDefaultVersion,
9+
readJSON,
10+
buildAndCopyCoreJS,
11+
expandVersionsConfig,
412
} from './helpers.mjs';
513
import childProcess from 'node:child_process';
614
import { cp, readdir, readlink } from 'node:fs/promises';
@@ -182,7 +190,7 @@ async function getVersions(targetBranch) {
182190
const versions = await readJSON(VERSIONS_FILE);
183191
console.timeEnd('Got versions');
184192

185-
return versions;
193+
return expandVersionsConfig(versions);
186194
}
187195

188196
try {
@@ -194,6 +202,7 @@ try {
194202
if (!BRANCH) {
195203
const versions = await getVersions(targetBranch);
196204
for (const version of versions) {
205+
if (version.default) continue;
197206
await copyDocsToBuilder(version);
198207
await buildAndCopyCoreJS(version, BUILD_SRC_DIR, BUNDLES_DIR, true);
199208
}

0 commit comments

Comments
 (0)