Skip to content

Commit 6cb8949

Browse files
committed
Builder config
1 parent 976f34d commit 6cb8949

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

website/build.mjs

Lines changed: 22 additions & 32 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

@@ -245,9 +235,9 @@ 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`);
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`);
251241
const versionsMenu = await buildVersionsMenu(versions, version, 'playground');
252242
let playground = template.replace('{content}', playgroundContent);
253243
playground = playground.replace('{base}', BASE);
@@ -256,12 +246,12 @@ async function buildPlayground(template, version, versions) {
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) {
262252
const defaultVersionsMenu = await buildVersionsMenu(versions, version, '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+
};

0 commit comments

Comments
 (0)