Skip to content

Commit c0c2fcf

Browse files
committed
Builder config
1 parent 976f34d commit c0c2fcf

File tree

6 files changed

+67
-66
lines changed

6 files changed

+67
-66
lines changed

website/build.mjs

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@ import { JSDOM } from 'jsdom';
44
import { Marked } from 'marked';
55
import { gfmHeadingId, getHeadingList } from 'marked-gfm-heading-id';
66
import markedAlert from 'marked-alert';
7-
8-
const DOCS_DIR = 'docs/web/';
9-
const BLOG_DIR = 'docs/web/blog/';
10-
const RESULT_DIR = 'website/dist/';
11-
const TEMPLATES_DIR = 'website/templates/';
12-
const SRC_DIR = 'website/src/';
13-
const VERSIONS_FILE = 'website/config/versions.json';
14-
const TEMPLATE_PATH = `${ TEMPLATES_DIR }index.html`;
15-
const BUNDLES_PATH = './bundles';
16-
const BUNDLE_NAME = 'core-js-bundle.js';
17-
const BUNDLE_NAME_ESMODULES = 'core-js-bundle-esmodules.js';
7+
import config from './config/config.mjs';
188

199
const args = process.argv;
2010
const lastArg = args.at(-1);
@@ -25,7 +15,7 @@ const BASE = BRANCH ? `/branches/${ BRANCH }/` : '/';
2515
async function getDefaultVersion() {
2616
if (BRANCH) return BRANCH;
2717

28-
const versions = await readJSON(VERSIONS_FILE);
18+
const versions = await readJSON(config.versionsFile);
2919
return versions.find(v => v.default)?.label;
3020
}
3121

@@ -53,9 +43,8 @@ async function buildDocsMenu(item) {
5343
result += '</ul></li>';
5444
return result;
5545
}
56-
const defaultVersion = BRANCH || DEFAULT_VERSION;
5746

58-
return `<li><a href="${ item.url }" class="with-docs-version" data-default-version="${ defaultVersion }">${ item.title }</a></li>`;
47+
return `<li><a href="${ item.url }" class="with-docs-version" data-default-version="${ DEFAULT_VERSION }">${ item.title }</a></li>`;
5948
}
6049

6150
async function isExists(target) {
@@ -75,7 +64,7 @@ async function getDocsMenuItems(version) {
7564
if (docsMenuItems[version]) return docsMenuItems[version];
7665

7766
echo(chalk.green(`Getting menu items from file for version: ${ version }`));
78-
const jsonPath = BRANCH ? `${ DOCS_DIR }docs/menu.json` : `${ DOCS_DIR }${ version }/docs/menu.json`;
67+
const jsonPath = BRANCH ? `${ config.docsDir }docs/menu.json` : `${ config.docsDir }${ version }/docs/menu.json`;
7968
const exists = await isExists(jsonPath);
8069
if (!exists) {
8170
echo(chalk.yellow(`Menu JSON file not found: ${ jsonPath }`));
@@ -104,10 +93,10 @@ async function buildDocsMenuForVersion(version) {
10493
async function buildVersionsMenuList(versions, currentVersion, section) {
10594
let versionsMenuHtml = '<div class="dropdown-block">';
10695
for (const v of versions) {
107-
const activityClass = v.label === currentVersion ? ' class="active"' : '';
96+
const activityClass = v.label === currentVersion && !v.default ? ' class="active"' : '';
10897
const defaultBadge = v.default ? ' (default)' : '';
10998
const versionPath = v.default ? '' : `${ v.label }/`;
110-
versionsMenuHtml += `<a href="./${ versionPath }${ section }/"${ activityClass }>${ v.label }${ defaultBadge }</a>`;
99+
versionsMenuHtml += `<a href="./${ versionPath }${ section }"${ activityClass }>${ v.label }${ defaultBadge }</a>`;
111100
}
112101
versionsMenuHtml += '</div>';
113102

@@ -146,7 +135,6 @@ const linkRenderer = {
146135
};
147136

148137
function buildMenus(html) {
149-
const defaultVersion = BRANCH || DEFAULT_VERSION;
150138
const headings = getHeadingList().filter(({ level }) => level > 1);
151139
let result = '<div class="wrapper">';
152140
if (isBlog) {
@@ -159,7 +147,7 @@ function buildMenus(html) {
159147
result += `<div class="table-of-contents sticky"><div class="mobile-trigger"></div>
160148
${ headings.map(({ id, raw, level }) => `<div class="toc-link"><a href="${
161149
htmlFileName.replace('.html', '') }#${ id }" class="h${
162-
level } with-docs-version" data-default-version="${ defaultVersion }">${
150+
level } with-docs-version" data-default-version="${ DEFAULT_VERSION }">${
163151
raw }</a></div>`).join('\n') }
164152
</div>`;
165153
}
@@ -187,7 +175,7 @@ let blogMenuCache = '';
187175
async function buildBlogMenu() {
188176
if (blogMenuCache !== '') return blogMenuCache;
189177

190-
const mdFiles = await getAllMdFiles(BLOG_DIR);
178+
const mdFiles = await getAllMdFiles(config.blogDir);
191179
mdFiles.reverse();
192180
let index = '---\ndisableContentMenu: true\n---\n# Blog\n\n';
193181
let menu = '<ul>';
@@ -205,14 +193,14 @@ async function buildBlogMenu() {
205193

206194
const match = mdPath.match(/(?<date>\d{4}-\d{2}-\d{2})-/);
207195
const date = match && match.groups ? match.groups.date : null;
208-
const htmlFileName = mdPath.replace(BLOG_DIR, '').replace(/\.md$/i, '');
196+
const htmlFileName = mdPath.replace(config.blogDir, '').replace(/\.md$/i, '');
209197
menu += `<li><a href="./blog/${ htmlFileName }">${ date }: ${ firstH1.text }</a></li>`;
210198
index += `## [${ firstH1.text }](./blog/${
211199
htmlFileName })\n\n*${ date }*\n\n${ fileMetadata.preview ?? '' }\n\n`;
212200
}
213201
menu += '</ul>';
214202
blogMenuCache = menu;
215-
const blogIndexPath = path.join(BLOG_DIR, 'index.md');
203+
const blogIndexPath = path.join(config.blogDir, 'index.md');
216204
await fs.writeFile(blogIndexPath, index, 'utf8');
217205
echo(chalk.green(`File created: ${ blogIndexPath }`));
218206

@@ -228,7 +216,7 @@ async function getVersionTags() {
228216
async function getVersionFromMdFile(mdPath) {
229217
const match = mdPath.match(/\/web\/(?<version>[^/]+)\/docs\//);
230218
if (match && match.groups && match.groups.version) {
231-
return match.groups.version
219+
return match.groups.version;
232220
}
233221
return DEFAULT_VERSION;
234222
}
@@ -245,23 +233,23 @@ async function buildPlaygrounds(template, versions) {
245233
}
246234

247235
async function buildPlayground(template, version, versions) {
248-
const bundleScript = `<script nomodule src="${ BUNDLES_PATH }/${ version.label }/${ BUNDLE_NAME }"></script>`;
249-
const bundleESModulesScript = `<script type="module" src="${ BUNDLES_PATH }/${ version.label }/${ BUNDLE_NAME_ESMODULES }"></script>`;
250-
const playgroundContent = await readFile(`${ SRC_DIR }playground.html`);
251-
const versionsMenu = await buildVersionsMenu(versions, version, 'playground');
236+
const bundleScript = `<script nomodule src="${ config.bundlesPath }/${ version.label }/${ config.bundleName }"></script>`;
237+
const bundleESModulesScript = `<script type="module" src="${ config.bundlesPath }/${ version.label }/${ config.bundleNameESModules }"></script>`;
238+
const playgroundContent = await readFile(`${ config.srcDir }playground.html`);
239+
const versionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
252240
let playground = template.replace('{content}', playgroundContent);
253241
playground = playground.replace('{base}', BASE);
254242
playground = playground.replace('{title}', 'Playground - ');
255243
playground = playground.replace('{base}', BASE);
256244
playground = playground.replace('{core-js-bundle}', bundleScript);
257245
playground = playground.replace('{core-js-bundle-esmodules}', bundleESModulesScript);
258246
const playgroundWithVersion = playground.replace('{versions-menu}', versionsMenu);
259-
const playgroundFilePath = path.join(RESULT_DIR, version.label, 'playground.html');
247+
const playgroundFilePath = path.join(config.resultDir, version.label, 'playground.html');
260248

261249
if (version.default) {
262-
const defaultVersionsMenu = await buildVersionsMenu(versions, version, 'playground');
250+
const defaultVersionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
263251
const defaultVersionPlayground = playground.replace('{versions-menu}', defaultVersionsMenu);
264-
const defaultPlaygroundPath = path.join(RESULT_DIR, 'playground.html');
252+
const defaultPlaygroundPath = path.join(config.resultDir, 'playground.html');
265253
await fs.writeFile(defaultPlaygroundPath, defaultVersionPlayground, 'utf8');
266254
echo(chalk.green(`File created: ${ defaultPlaygroundPath }`));
267255
} else {
@@ -274,17 +262,17 @@ async function buildPlayground(template, version, versions) {
274262
async function createDocsIndexes(versions) {
275263
if (BRANCH) {
276264
const menuItems = await getDocsMenuItems(BRANCH);
277-
const firstDocPath = path.join(RESULT_DIR, `${ menuItems[0].url }.html`.replace('{docs-version}/', ''));
278-
const indexFilePath = path.join(RESULT_DIR, 'docs/', 'index.html');
265+
const firstDocPath = path.join(config.resultDir, `${ menuItems[0].url }.html`.replace('{docs-version}/', ''));
266+
const indexFilePath = path.join(config.resultDir, 'docs/', 'index.html');
279267
await fs.copy(firstDocPath, indexFilePath);
280268
echo(chalk.green(`File created: ${ indexFilePath }`));
281269
return;
282270
}
283271

284272
for (const version of versions) {
285273
const menuItems = await getDocsMenuItems(version.label);
286-
const firstDocPath = path.join(RESULT_DIR, `${ menuItems[0].url }.html`.replace('{docs-version}', version.label));
287-
const indexFilePath = path.join(RESULT_DIR, `${ version.label }/docs/`, 'index.html');
274+
const firstDocPath = path.join(config.resultDir, `${ menuItems[0].url }.html`.replace('{docs-version}', version.label));
275+
const indexFilePath = path.join(config.resultDir, `${ version.label }/docs/`, 'index.html');
288276
await fs.copy(firstDocPath, indexFilePath);
289277
echo(chalk.green(`File created: ${ indexFilePath }`));
290278
}
@@ -306,7 +294,7 @@ async function getVersions() {
306294
default: true,
307295
}];
308296
}
309-
const versions = await readJSON(VERSIONS_FILE);
297+
const versions = await readJSON(config.versionsFile);
310298
echo(chalk.green('Got versions from file'));
311299

312300
return versions;
@@ -324,13 +312,12 @@ let isDocs = false;
324312
let isChangelog;
325313

326314
async function build() {
327-
const template = await readFile(TEMPLATE_PATH);
315+
const template = await readFile(config.templatePath);
328316
await buildBlogMenu();
329-
const mdFiles = await getAllMdFiles(DOCS_DIR);
317+
const mdFiles = await getAllMdFiles(config.docsDir);
330318
const versions = await getVersions();
331-
const defaultVersion = BRANCH || DEFAULT_VERSION;
332-
const bundleScript = `<script nomodule src="${ BUNDLES_PATH }/${ defaultVersion }/${ BUNDLE_NAME }"></script>`;
333-
const bundleESModulesScript = `<script type="module" src="${ BUNDLES_PATH }/${ defaultVersion }/${ BUNDLE_NAME_ESMODULES }"></script>`;
319+
const bundleScript = `<script nomodule src="${ config.bundlesPath }/${ DEFAULT_VERSION }/${ config.bundleName }"></script>`;
320+
const bundleESModulesScript = `<script type="module" src="${ config.bundlesPath }/${ DEFAULT_VERSION }/${ config.bundleNameESModules }"></script>`;
334321

335322
let currentVersion = '';
336323
let versionsMenu = '';
@@ -347,11 +334,11 @@ async function build() {
347334
if (currentVersion !== versionFromMdFile) {
348335
currentVersion = versionFromMdFile;
349336
docsMenu = await buildDocsMenuForVersion(currentVersion);
350-
versionsMenu = await buildVersionsMenu(versions, currentVersion, 'docs');
337+
versionsMenu = await buildVersionsMenu(versions, currentVersion, 'docs/');
351338
}
352339

353-
htmlFileName = mdPath.replace(DOCS_DIR, '').replace(/\.md$/i, '.html');
354-
const htmlFilePath = path.join(RESULT_DIR, htmlFileName);
340+
htmlFileName = mdPath.replace(config.docsDir, '').replace(/\.md$/i, '.html');
341+
const htmlFilePath = path.join(config.resultDir, htmlFileName);
355342
const htmlContent = isDocs || isBlog || isChangelog ? markedWithContents.parse(content) : marked.parse(content);
356343

357344
let resultHtml = template.replace('{content}', htmlContent.replaceAll('$', '&#36;'));

website/config/config.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
docsDir: 'docs/web/',
3+
blogDir: 'docs/web/blog/',
4+
resultDir: 'website/dist/',
5+
templatesDir: 'website/templates/',
6+
templatePath: 'website/templates/index.html',
7+
srcDir: 'website/src/',
8+
versionsFile: 'website/config/versions.json',
9+
bundlesPath: './bundles',
10+
bundleName: 'core-js-bundle.js',
11+
bundleNameESModules: 'core-js-bundle-esmodules.js',
12+
};

website/config/versions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[
22
{
3-
"label": "v3.4",
3+
"label": "v3.45",
44
"default": true,
55
"branch": "v3.45-docs"
66
},
77
{
8-
"label": "v3.4",
8+
"label": "v3.45",
99
"branch": "v3.45-docs"
1010
}
1111
]

website/scripts/runner.mjs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const BUILD_RESULT_DIR = 'result';
1414
const BUNDLES_DIR = 'bundles';
1515
const REPO = 'https://github.com/zloirock/core-js.git';
1616
const BUILDER_BRANCH = 'web-3';
17-
const DEFAULT_VERSION = await getDefaultVersion();
18-
const VERSIONS_FILE = 'website/config/versions.json';
1917

2018
const args = process.argv;
2119
const lastArg = args.at(-1);
@@ -26,7 +24,8 @@ const BUILD_ID = new Date().toISOString().replaceAll(/\D/g, '-') + Math.random()
2624
const BUILD_DIR = `${ BUILDS_ROOT_DIR }/${ BUILD_ID }/`;
2725
const BUILD_SRC_DIR = `${ BUILD_DIR }${ SRC_DIR }/`;
2826
const BUILD_DOCS_DIR = `${ BUILD_DIR }builder/`;
29-
const SITE_FILES_DIR = `${ BUILD_DIR }${ SRC_DIR }/website/dist/`;
27+
const SITE_FILES_DIR = `${ BUILD_SRC_DIR }/website/dist/`;
28+
const VERSIONS_FILE = `${ BUILD_SRC_DIR }website/config/versions.json`;
3029

3130
async function getDefaultVersion() {
3231
const versions = await readJSON(VERSIONS_FILE);
@@ -135,7 +134,7 @@ async function copyDocsToBuilder(version) {
135134
console.time(`Copied docs to builder for "${ target }"`);
136135
await checkoutVersion(version);
137136
const fromDir = `${ BUILD_SRC_DIR }docs/web/docs/`;
138-
const toDir = `${ BUILD_DOCS_DIR }${ target }/docs/`;
137+
const toDir = `${ BUILD_DOCS_DIR }${ version.label }/docs/`;
139138
await copyDocs(fromDir, toDir);
140139
console.timeEnd(`Copied docs to builder for "${ target }"`);
141140
}
@@ -188,15 +187,16 @@ async function checkoutVersion(version) {
188187

189188
async function buildAndCopyCoreJS(version) {
190189
const target = version.branch ?? version.tag;
190+
const name = version.label;
191191
console.log(`Building and copying core-js for ${ target }`);
192192
const targetBundlePath = `${ BUNDLES_DIR }/${ target }/`;
193193

194194
if (await isExists(targetBundlePath)) {
195195
console.time('Core JS bundles copied');
196196
const bundlePath = `${ targetBundlePath }core-js-bundle.js`;
197-
const destBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ target }/core-js-bundle.js`;
197+
const destBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle.js`;
198198
const esmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
199-
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ target }/core-js-bundle-esmodules.js`;
199+
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
200200
await cp(bundlePath, destBundlePath);
201201
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
202202
console.timeEnd('Core JS bundles copied');
@@ -208,14 +208,14 @@ async function buildAndCopyCoreJS(version) {
208208
await installDependencies();
209209
await exec('npm run bundle-package', { cwd: BUILD_SRC_DIR });
210210
const bundlePath = `${ BUILD_SRC_DIR }packages/core-js-bundle/minified.js`;
211-
const destPath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ target }/core-js-bundle.js`;
211+
const destPath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle.js`;
212212
const destBundlePath = `${ targetBundlePath }core-js-bundle.js`;
213213
await cp(bundlePath, destPath);
214214
await cp(bundlePath, destBundlePath);
215215

216216
await exec('npm run bundle-package esmodules', { cwd: BUILD_SRC_DIR });
217217
const esmodulesBundlePath = `${ BUILD_SRC_DIR }packages/core-js-bundle/minified.js`;
218-
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ target }/core-js-bundle-esmodules.js`;
218+
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
219219
const destEsmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
220220
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
221221
await cp(esmodulesBundlePath, destEsmodulesBundlePath);
@@ -286,7 +286,8 @@ async function copyCommonFiles() {
286286
async function createLastDocsLink() {
287287
console.log('Creating last docs link...');
288288
console.time('Created last docs link');
289-
const absoluteBuildPath = path.resolve(`${ BUILD_DIR }${ BUILD_RESULT_DIR }/${ DEFAULT_VERSION }/docs/`);
289+
const defaultVersion = await getDefaultVersion();
290+
const absoluteBuildPath = path.resolve(`${ BUILD_DIR }${ BUILD_RESULT_DIR }/${ defaultVersion }/docs/`);
290291
const absoluteLastDocsPath = path.resolve(`${ BUILD_DIR }${ BUILD_RESULT_DIR }/docs/`);
291292
await exec(`ln -s ${ absoluteBuildPath } ${ absoluteLastDocsPath }`);
292293
console.timeEnd('Created last docs link');
@@ -306,10 +307,11 @@ async function readJSON(filePath) {
306307
}
307308
}
308309

309-
async function getVersions() {
310+
async function getVersions(targetBranch) {
310311
console.log('Getting versions...');
311312
console.time('Got versions');
312-
const versions = await readJSON(`${ BUILD_SRC_DIR }${ VERSIONS_FILE }`);
313+
await exec(`git checkout origin/${ targetBranch }`, { cwd: BUILD_SRC_DIR });
314+
const versions = await readJSON(VERSIONS_FILE);
313315
console.timeEnd('Got versions');
314316

315317
return versions;
@@ -322,7 +324,7 @@ async function run() {
322324

323325
const targetBranch = BRANCH || BUILDER_BRANCH;
324326
if (!BRANCH) {
325-
const versions = await getVersions();
327+
const versions = await getVersions(targetBranch);
326328
for (const version of versions) {
327329
await copyDocsToBuilder(version);
328330
await buildAndCopyCoreJS(version);

website/src/js/main.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function init() {
3636
const contentMenuTrigger = document.querySelector('.table-of-contents .mobile-trigger');
3737
const sectionMenu = document.querySelector('.docs-menu');
3838
const sectionMenuTrigger = document.querySelector('.docs-menu .mobile-trigger');
39-
let isDocs, docsVersion;
39+
let isDocs, docsVersion, isPlayground;
4040
const currentPath = getRelativePath();
4141

4242
function toggleMenu() {
@@ -102,9 +102,9 @@ function init() {
102102
return isDocs;
103103
}
104104

105-
function hasDocsVersion() {
105+
function hasCurrentVersion() {
106106
if (docsVersion !== undefined) return docsVersion;
107-
docsVersion = !currentPath.startsWith('docs/');
107+
docsVersion = !currentPath.startsWith('docs/') && !currentPath.startsWith('playground');
108108
return docsVersion;
109109
}
110110

@@ -118,8 +118,8 @@ function init() {
118118
});
119119
}
120120

121-
function processDocsVersions() {
122-
const hasVersion = hasDocsVersion();
121+
function processVersions() {
122+
const hasVersion = hasCurrentVersion();
123123
if (!hasVersion) setDefaultVersion();
124124
if (!isDocsPage()) return;
125125
if (hasVersion) return;
@@ -143,7 +143,7 @@ function init() {
143143
}
144144
}
145145

146-
function highlightActiveMenuItem() {
146+
function highlightActiveDocsMenuItem() {
147147
if (!isDocsPage()) return;
148148

149149
let found = false;
@@ -226,8 +226,8 @@ function init() {
226226
hljs.highlightAll();
227227

228228
processStickyBlocks();
229-
processDocsVersions();
230-
highlightActiveMenuItem();
229+
processVersions();
230+
highlightActiveDocsMenuItem();
231231
openFirstCollapsibleMenuItem();
232232
}
233233

website/src/scss/parts/main.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ main {
481481
a.active {
482482
font-weight: 500;
483483
@include themify($themes) {
484-
color: themed('link-color-hover');
484+
color: themed('link-color2-hover');
485485
}
486486
}
487487
}

0 commit comments

Comments
 (0)