Skip to content

Commit a84d8bd

Browse files
committed
Script for local website building
1 parent 279a949 commit a84d8bd

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"prepare": "npm run zxi scripts/prepare.mjs",
2020
"build-compat": "npm run zxi scripts/build-compat/index.mjs",
2121
"build-web": "npm run zxi time website/index.mjs",
22+
"build-web-local": "npm run zxi time website/local.mjs",
2223
"bundle": "run-s bundle-package bundle-tests",
2324
"bundle-package": "npm run zxi time scripts/bundle-package/bundle-package.mjs",
2425
"bundle-tests": "npm run zxi time cd scripts/bundle-tests/bundle-tests.mjs",

website/build.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { Marked } from 'marked';
55
import { gfmHeadingId, getHeadingList } from 'marked-gfm-heading-id';
66
import markedAlert from 'marked-alert';
77
import config from './config/config.mjs';
8-
import { fs } from 'zx';
8+
import { argv, fs } from 'zx';
99

1010
const { copy, mkdir, readFile, readJson, readdir, writeFile } = fs;
1111

12-
const args = process.argv;
13-
const lastArg = args.at(-1);
14-
const BRANCH = lastArg.startsWith('branch=') ? lastArg.slice('branch='.length) : undefined;
12+
const branchArg = argv._.find(item => item.startsWith('branch='));
13+
const BRANCH = branchArg ? branchArg.slice('branch='.length) : undefined;
14+
const LOCAL = argv._.includes('local');
15+
const BASE = LOCAL && BRANCH ? '/core-js/website/dist/' : BRANCH ? `/branches/${ BRANCH }/` : '/';
1516
const DEFAULT_VERSION = await getDefaultVersion();
16-
const BASE = BRANCH ? `/branches/${ BRANCH }/` : '/';
1717

1818
let htmlFileName = '';
1919
let docsMenu = '';

website/local.mjs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* eslint-disable no-console -- needed for logging */
2+
import childProcess from 'node:child_process';
3+
import { constants } from 'node:fs';
4+
import fs from 'node:fs/promises';
5+
import { promisify } from 'node:util';
6+
import path from 'node:path';
7+
8+
const exec = promisify(childProcess.exec);
9+
const { access, cp, readdir } = fs;
10+
11+
const BUNDLES_DIR = 'bundles';
12+
const BABEL_PATH = 'website/node_modules/@babel/standalone/babel.min.js';
13+
14+
const BUILD_SRC_DIR = `./`;
15+
const BUILD_WEBSITE_SRC_DIR = `${ BUILD_SRC_DIR }/website`;
16+
17+
async function isExists(target) {
18+
try {
19+
await access(target, constants.F_OK);
20+
return true;
21+
} catch {
22+
return false;
23+
}
24+
}
25+
26+
async function buildWeb(branch) {
27+
console.log('Building web');
28+
console.time('Built web');
29+
let command = `npm run build-web branch=${ branch } local`;
30+
const stdout = await exec(command, { cwd: BUILD_SRC_DIR });
31+
console.timeEnd('Built web');
32+
return stdout;
33+
}
34+
35+
async function installDependencies(dir = BUILD_SRC_DIR) {
36+
console.log('Installing dependencies...');
37+
console.time('Installed dependencies');
38+
await exec('npm ci', { cwd: dir });
39+
console.timeEnd('Installed dependencies');
40+
}
41+
42+
async function hasDocs(version) {
43+
const target = version.branch ? `origin/${ version.branch }` : version.tag;
44+
console.log(`Checking if docs exist in "${ target }"...`);
45+
try {
46+
await exec(`git ls-tree -r --name-only ${ target } | grep "docs/web/docs/"`, { cwd: BUILD_SRC_DIR });
47+
} catch {
48+
throw new Error(`Docs not found in "${ target }".`);
49+
}
50+
}
51+
52+
async function buildAndCopyCoreJS(version) {
53+
const target = version.branch ?? version.tag;
54+
const name = version.path ?? version.label;
55+
console.log(`Building and copying core-js for ${ target }`);
56+
const targetBundlePath = `${ BUNDLES_DIR }/${ target }/`;
57+
58+
if (await isExists(targetBundlePath)) {
59+
console.time('Core JS bundles copied');
60+
const bundlePath = `${ targetBundlePath }core-js-bundle.js`;
61+
const destBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle.js`;
62+
const esmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
63+
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
64+
await cp(bundlePath, destBundlePath);
65+
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
66+
console.timeEnd('Core JS bundles copied');
67+
return;
68+
}
69+
70+
console.time('Core JS bundles built');
71+
await installDependencies();
72+
await exec('npm run bundle-package', { cwd: BUILD_SRC_DIR });
73+
const bundlePath = `${ BUILD_SRC_DIR }packages/core-js-bundle/minified.js`;
74+
const destPath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle.js`;
75+
const destBundlePath = `${ targetBundlePath }core-js-bundle.js`;
76+
await cp(bundlePath, destPath);
77+
await cp(bundlePath, destBundlePath);
78+
79+
await exec('npm run bundle-package esmodules', { cwd: BUILD_SRC_DIR });
80+
const esmodulesBundlePath = `${ BUILD_SRC_DIR }packages/core-js-bundle/minified.js`;
81+
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
82+
const destEsmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
83+
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
84+
await cp(esmodulesBundlePath, destEsmodulesBundlePath);
85+
console.timeEnd('Core JS bundles built');
86+
}
87+
88+
async function copyBabelStandalone() {
89+
console.log('Copying Babel standalone');
90+
await installDependencies(BUILD_WEBSITE_SRC_DIR);
91+
console.time('Copied Babel standalone');
92+
const babelPath = `${ BUILD_SRC_DIR }${ BABEL_PATH }`;
93+
const destPath = `${ BUILD_SRC_DIR }website/src/public/babel.min.js`;
94+
await cp(babelPath, destPath);
95+
console.timeEnd('Copied Babel standalone');
96+
}
97+
98+
async function copyBlogPosts() {
99+
console.log('Copying blog posts...');
100+
console.time('Copied blog posts');
101+
const fromDir = `${ BUILD_SRC_DIR }docs/`;
102+
const toDir = `${ BUILD_SRC_DIR }docs/web/blog/`;
103+
const entries = await readdir(fromDir, { withFileTypes: true });
104+
for (const entry of entries) {
105+
if (entry.isFile()) {
106+
const srcFile = path.join(fromDir, entry.name);
107+
const destFile = path.join(toDir, entry.name);
108+
await cp(srcFile, destFile);
109+
}
110+
}
111+
console.timeEnd('Copied blog posts');
112+
}
113+
114+
async function copyCommonFiles() {
115+
console.log('Copying common files...');
116+
console.time('Copied common files');
117+
const fromDir = `${ BUILD_SRC_DIR }`;
118+
const toDir = `${ BUILD_SRC_DIR }docs/web/`;
119+
await cp(`${ fromDir }CHANGELOG.md`, `${ toDir }changelog.md`);
120+
await cp(`${ fromDir }CONTRIBUTING.md`, `${ toDir }contributing.md`);
121+
await cp(`${ fromDir }SECURITY.md`, `${ toDir }security.md`);
122+
console.timeEnd('Copied common files');
123+
}
124+
125+
async function getCurrentBranch() {
126+
const { stdout } = await exec('git rev-parse --abbrev-ref HEAD', { cwd: BUILD_SRC_DIR });
127+
return stdout.trim();
128+
}
129+
130+
try {
131+
console.time('Finished in');
132+
const targetBranch = await getCurrentBranch();
133+
134+
const version = { branch: targetBranch, label: targetBranch };
135+
await hasDocs(version);
136+
await buildAndCopyCoreJS(version);
137+
138+
await copyBabelStandalone();
139+
await copyBlogPosts();
140+
await copyCommonFiles();
141+
await buildWeb(targetBranch);
142+
console.timeEnd('Finished in');
143+
} catch (error) {
144+
console.error(error);
145+
}

0 commit comments

Comments
 (0)