Skip to content

Commit e56cb71

Browse files
committed
Create a universal ESM bundle for highlight.js #7790
1 parent 6423e0c commit e56cb71

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

buildScripts/buildHighlightJs.mjs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {execSync} from 'child_process';
2+
import {fileURLToPath} from 'url';
3+
import fs from 'fs-extra';
4+
import os from 'os';
5+
import path from 'path';
6+
import * as terser from 'terser';
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
const neoPath = path.resolve(__dirname, '../');
10+
11+
const languages = ['bash', 'css', 'javascript', 'json', 'scss', 'xml'];
12+
const outputDir = path.resolve(neoPath, 'dist/highlight');
13+
const outputFile = path.join(outputDir, 'highlight.custom.js');
14+
const minOutputFile = path.join(outputDir, 'highlight.custom.min.js');
15+
const tempDir = path.resolve(neoPath, 'tmp/highlightjs');
16+
17+
const gitCmd = os.platform().startsWith('win') ? 'git.exe' : 'git';
18+
const nodeCmd = os.platform().startsWith('win') ? 'node.exe' : 'node';
19+
const npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm';
20+
21+
async function main() {
22+
console.log('Building custom highlight.js bundle...');
23+
24+
try {
25+
// 1. Clean up the temporary directory
26+
console.log(`Cleaning up ${tempDir}`);
27+
fs.removeSync(tempDir);
28+
fs.ensureDirSync(tempDir);
29+
30+
// 2. Clone the highlight.js repository
31+
console.log(`Cloning highlight.js into ${tempDir}`);
32+
const cloneCommand = `${gitCmd} clone --depth 1 https://github.com/highlightjs/highlight.js.git ${tempDir}`;
33+
execSync(cloneCommand, { stdio: 'inherit' });
34+
35+
// 3. Install dependencies
36+
console.log(`Installing dependencies in ${tempDir}`);
37+
const installCommand = `${npmCmd} install`;
38+
execSync(installCommand, { cwd: tempDir, stdio: 'inherit' });
39+
40+
// 4. Run the build script
41+
console.log('Running build script...');
42+
const buildCommand = [
43+
nodeCmd,
44+
'tools/build.js',
45+
'-n',
46+
...languages
47+
].join(' ');
48+
49+
execSync(buildCommand, { cwd: tempDir, stdio: 'inherit' });
50+
51+
// 5. Copy and minify the generated bundle
52+
const generatedFile = path.join(tempDir, 'build/highlight.js');
53+
console.log(`Copying ${generatedFile} to ${outputFile}`);
54+
const bundleContent = fs.readFileSync(generatedFile, 'utf-8');
55+
fs.ensureDirSync(outputDir);
56+
fs.writeFileSync(outputFile, bundleContent);
57+
58+
console.log(`Minifying ${outputFile}`);
59+
const minifiedContent = await terser.minify(bundleContent);
60+
fs.writeFileSync(minOutputFile, minifiedContent.code);
61+
62+
63+
// 6. Clean up the temporary directory
64+
console.log(`Cleaning up ${tempDir}`);
65+
fs.removeSync(tempDir);
66+
67+
console.log(`Custom highlight.js bundle created at: ${outputFile}`);
68+
console.log(`Minified highlight.js bundle created at: ${minOutputFile}`);
69+
} catch (error) {
70+
console.error(`Error building highlight.js bundle: ${error}`);
71+
// In case of error, leave the temporary directory for inspection
72+
}
73+
}
74+
75+
main();

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build-all" : "node ./buildScripts/buildAll.mjs -f -n",
2222
"build-all-questions" : "node ./buildScripts/buildAll.mjs -f",
2323
"build-dist-esm" : "node ./buildScripts/buildESModules.mjs",
24+
"build-highlightjs" : "node ./buildScripts/buildHighlightJs.mjs",
2425
"build-themes" : "node ./buildScripts/buildThemes.mjs -f",
2526
"build-threads" : "node ./buildScripts/webpack/buildThreads.mjs -f",
2627
"bundle-parse5" : "node ./buildScripts/bundleParse5.mjs",
@@ -31,6 +32,7 @@
3132
"create-class" : "node ./buildScripts/createClass.mjs",
3233
"create-component" : "node ./buildScripts/createComponent.mjs",
3334
"generate-docs-json" : "node ./buildScripts/docs/jsdocx.mjs",
35+
"postinstall" : "node ./buildScripts/buildHighlightJs.mjs",
3436
"prepare-release" : "node ./buildScripts/prepareRelease.mjs",
3537
"server-start" : "webpack serve -c ./buildScripts/webpack/webpack.server.config.mjs --open",
3638
"test" : "playwright test -c test/playwright/playwright.config.mjs",

0 commit comments

Comments
 (0)