Skip to content

Commit fe56ef1

Browse files
committed
Builder config
1 parent 976f34d commit fe56ef1

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

website/build.mjs

Lines changed: 25 additions & 35 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

@@ -75,7 +65,7 @@ async function getDocsMenuItems(version) {
7565
if (docsMenuItems[version]) return docsMenuItems[version];
7666

7767
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`;
68+
const jsonPath = BRANCH ? `${ config.docsDir }docs/menu.json` : `${ config.docsDir }${ version }/docs/menu.json`;
7969
const exists = await isExists(jsonPath);
8070
if (!exists) {
8171
echo(chalk.yellow(`Menu JSON file not found: ${ jsonPath }`));
@@ -187,7 +177,7 @@ let blogMenuCache = '';
187177
async function buildBlogMenu() {
188178
if (blogMenuCache !== '') return blogMenuCache;
189179

190-
const mdFiles = await getAllMdFiles(BLOG_DIR);
180+
const mdFiles = await getAllMdFiles(config.blogDir);
191181
mdFiles.reverse();
192182
let index = '---\ndisableContentMenu: true\n---\n# Blog\n\n';
193183
let menu = '<ul>';
@@ -205,14 +195,14 @@ async function buildBlogMenu() {
205195

206196
const match = mdPath.match(/(?<date>\d{4}-\d{2}-\d{2})-/);
207197
const date = match && match.groups ? match.groups.date : null;
208-
const htmlFileName = mdPath.replace(BLOG_DIR, '').replace(/\.md$/i, '');
198+
const htmlFileName = mdPath.replace(config.blogDir, '').replace(/\.md$/i, '');
209199
menu += `<li><a href="./blog/${ htmlFileName }">${ date }: ${ firstH1.text }</a></li>`;
210200
index += `## [${ firstH1.text }](./blog/${
211201
htmlFileName })\n\n*${ date }*\n\n${ fileMetadata.preview ?? '' }\n\n`;
212202
}
213203
menu += '</ul>';
214204
blogMenuCache = menu;
215-
const blogIndexPath = path.join(BLOG_DIR, 'index.md');
205+
const blogIndexPath = path.join(config.blogDir, 'index.md');
216206
await fs.writeFile(blogIndexPath, index, 'utf8');
217207
echo(chalk.green(`File created: ${ blogIndexPath }`));
218208

@@ -228,7 +218,7 @@ async function getVersionTags() {
228218
async function getVersionFromMdFile(mdPath) {
229219
const match = mdPath.match(/\/web\/(?<version>[^/]+)\/docs\//);
230220
if (match && match.groups && match.groups.version) {
231-
return match.groups.version
221+
return match.groups.version;
232222
}
233223
return DEFAULT_VERSION;
234224
}
@@ -245,23 +235,23 @@ async function buildPlaygrounds(template, versions) {
245235
}
246236

247237
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');
238+
const bundleScript = `<script nomodule src="${ config.bundlesPath }/${ version.label }/${ config.bundleName }"></script>`;
239+
const bundleESModulesScript = `<script type="module" src="${ config.bundlesPath }/${ version.label }/${ config.bundleNameESModules }"></script>`;
240+
const playgroundContent = await readFile(`${ config.srcDir }playground.html`);
241+
const versionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
252242
let playground = template.replace('{content}', playgroundContent);
253243
playground = playground.replace('{base}', BASE);
254244
playground = playground.replace('{title}', 'Playground - ');
255245
playground = playground.replace('{base}', BASE);
256246
playground = playground.replace('{core-js-bundle}', bundleScript);
257247
playground = playground.replace('{core-js-bundle-esmodules}', bundleESModulesScript);
258248
const playgroundWithVersion = playground.replace('{versions-menu}', versionsMenu);
259-
const playgroundFilePath = path.join(RESULT_DIR, version.label, 'playground.html');
249+
const playgroundFilePath = path.join(config.resultDir, version.label, 'playground.html');
260250

261251
if (version.default) {
262-
const defaultVersionsMenu = await buildVersionsMenu(versions, version, 'playground');
252+
const defaultVersionsMenu = await buildVersionsMenu(versions, version.label, 'playground');
263253
const defaultVersionPlayground = playground.replace('{versions-menu}', defaultVersionsMenu);
264-
const defaultPlaygroundPath = path.join(RESULT_DIR, 'playground.html');
254+
const defaultPlaygroundPath = path.join(config.resultDir, 'playground.html');
265255
await fs.writeFile(defaultPlaygroundPath, defaultVersionPlayground, 'utf8');
266256
echo(chalk.green(`File created: ${ defaultPlaygroundPath }`));
267257
} else {
@@ -274,17 +264,17 @@ async function buildPlayground(template, version, versions) {
274264
async function createDocsIndexes(versions) {
275265
if (BRANCH) {
276266
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');
267+
const firstDocPath = path.join(config.resultDir, `${ menuItems[0].url }.html`.replace('{docs-version}/', ''));
268+
const indexFilePath = path.join(config.resultDir, 'docs/', 'index.html');
279269
await fs.copy(firstDocPath, indexFilePath);
280270
echo(chalk.green(`File created: ${ indexFilePath }`));
281271
return;
282272
}
283273

284274
for (const version of versions) {
285275
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');
276+
const firstDocPath = path.join(config.resultDir, `${ menuItems[0].url }.html`.replace('{docs-version}', version.label));
277+
const indexFilePath = path.join(config.resultDir, `${ version.label }/docs/`, 'index.html');
288278
await fs.copy(firstDocPath, indexFilePath);
289279
echo(chalk.green(`File created: ${ indexFilePath }`));
290280
}
@@ -306,7 +296,7 @@ async function getVersions() {
306296
default: true,
307297
}];
308298
}
309-
const versions = await readJSON(VERSIONS_FILE);
299+
const versions = await readJSON(config.versionsFile);
310300
echo(chalk.green('Got versions from file'));
311301

312302
return versions;
@@ -324,13 +314,13 @@ let isDocs = false;
324314
let isChangelog;
325315

326316
async function build() {
327-
const template = await readFile(TEMPLATE_PATH);
317+
const template = await readFile(config.templatePath);
328318
await buildBlogMenu();
329-
const mdFiles = await getAllMdFiles(DOCS_DIR);
319+
const mdFiles = await getAllMdFiles(config.docsDir);
330320
const versions = await getVersions();
331321
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>`;
322+
const bundleScript = `<script nomodule src="${ config.bundlesPath }/${ defaultVersion }/${ config.bundleName }"></script>`;
323+
const bundleESModulesScript = `<script type="module" src="${ config.bundlesPath }/${ defaultVersion }/${ config.bundleNameESModules }"></script>`;
334324

335325
let currentVersion = '';
336326
let versionsMenu = '';
@@ -350,8 +340,8 @@ async function build() {
350340
versionsMenu = await buildVersionsMenu(versions, currentVersion, 'docs');
351341
}
352342

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

357347
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/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);

0 commit comments

Comments
 (0)