Skip to content

Commit c7a96b4

Browse files
committed
simplify building & fix issues
1 parent 1743f9d commit c7a96b4

File tree

5 files changed

+80
-141
lines changed

5 files changed

+80
-141
lines changed

tools/build_browser.js

Lines changed: 32 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,12 @@ async function buildBrowser(options) {
7373

7474
detailedGrammarSizes(languages);
7575

76-
const size = await buildBrowserHighlightJS(languages, { minify: options.minify });
76+
const size = await buildCore("highlight", languages, { minify: options.minify, format: "cjs" });
7777

7878
log("-----");
79-
log("Core :", size.core, "bytes");
80-
if (options.minify) { log("Core (min) :", size.core_min, "bytes"); }
8179
log("Languages (raw) :",
8280
languages.map((el) => el.data.length).reduce((acc, curr) => acc + curr, 0), "bytes");
83-
log("highlight.js :", size.full, "bytes");
81+
log("highlight.js :", size.fullSize, "bytes");
8482
if (options.minify) {
8583
log("highlight.min.js :", size.minified, "bytes");
8684
log("highlight.min.js.gz :", zlib.gzipSync(size.minifiedSrc).length, "bytes");
@@ -191,105 +189,65 @@ const builtInLanguagesPlugin = (languages) => ({
191189
load(id) {
192190
const escape = (s) => "grmr_" + s.replace("-", "_");
193191
if (id === "builtInLanguages") {
194-
return languages.map((lang) =>
192+
return languages.map((lang) =>
195193
`export { default as ${escape(lang.name)} } from ${JSON.stringify(lang.path)};`
196194
).join("\n");
197195
}
198196
return null;
199-
},
200-
})
201-
202-
async function buildBrowserHighlightJS(languages, { minify }) {
203-
log("Building highlight.js.");
197+
}
198+
});
204199

200+
async function buildCore(name, languages, options) {
205201
const header = buildHeader();
206-
207-
const outFile = `${process.env.BUILD_DIR}/highlight.js`;
208-
const minifiedFile = outFile.replace(/js$/, "min.js");
209-
const plugins = [...config.rollup.browser_core.input.plugins, builtInLanguagesPlugin(languages)];
210-
211-
const input = { ...config.rollup.browser_core.input, input: `src/stub.js`, plugins };
212-
const output = { ...config.rollup.browser_core.output, file: outFile };
213-
let librarySrc = await rollupCode(input, output);
214-
215-
216-
// we don't use this, we just use it to get a size approximation for the build stats
217-
const coreSrc = await rollupCode({ ...config.rollup.browser_core.input, input: `src/highlight.js`, plugins }, output);
218-
219-
// strip off the original top comment
220-
librarySrc = librarySrc.replace(/\/\*.*?\*\//s, "");
221-
222-
const fullSrc = `${header}\n${librarySrc}`;
223-
224-
const tasks = [];
225-
tasks.push(fs.writeFile(outFile, fullSrc, { encoding: "utf8" }));
226-
const shas = {
227-
"highlight.js": bundling.sha384(fullSrc)
202+
let relativePath = "";
203+
const input = {
204+
...(options.format === "es" ? config.rollup.node.input : config.rollup.browser_iife.input),
205+
input: `src/stub.js`
206+
};
207+
input.plugins = [
208+
...input.plugins,
209+
builtInLanguagesPlugin(languages)
210+
];
211+
const output = {
212+
...config.rollup.node.output,
213+
file: `${process.env.BUILD_DIR}/${name}.js`
228214
};
229215

230-
let minifiedSrc, minified, core_min;
231-
232-
if (minify) {
233-
const tersed = await Terser.minify(librarySrc, config.terser);
234-
235-
const coreTersed = await Terser.minify(coreSrc, config.terser);
236-
core_min = header.length + 1 + coreTersed.code.length;
237-
238-
minifiedSrc = `${header}\n${tersed.code}`;
239-
240-
// get approximate core minified size
241-
minified = minifiedSrc.length;
242-
243-
tasks.push(fs.writeFile(minifiedFile, minifiedSrc, { encoding: "utf8" }));
244-
shas["highlight.min.js"] = bundling.sha384(minifiedSrc);
216+
// optimize for no languages by not including the language loading stub
217+
if (languages.length === 0) {
218+
input.input = "src/highlight.js";
245219
}
246220

247-
await Promise.all(tasks);
248-
return {
249-
core: coreSrc.length,
250-
core_min,
251-
fullSrc,
252-
full: fullSrc.length,
253-
minifiedSrc,
254-
minified,
255-
shas,
256-
};
257-
}
221+
if (options.format === "es") {
222+
output.format = "es";
223+
output.file = `${process.env.BUILD_DIR}/es/${name}.js`;
224+
relativePath = "es/";
225+
}
258226

259-
async function buildBrowserESMHighlightJS(name, languages, options) {
260-
log("Building highlight.mjs.");
261-
const header = buildHeader();
262-
const input = { ...config.rollup.node.input, input: `src/stub.js`, plugins: [
263-
...config.rollup.node.input.plugins,
264-
builtInLanguagesPlugin(languages),
265-
] };
266-
const output = {
267-
...config.rollup.node.output,
268-
format: "es",
269-
file: `${process.env.BUILD_DIR}/es/${name}.js`,
270-
};
227+
log(`Building ${relativePath}${name}.js.`);
271228

272229
const index = await rollupCode(input, output);
273-
const sizeInfo = {}
274-
const writePromises = []
230+
const sizeInfo = { shas: [] };
231+
const writePromises = [];
275232
if (options.minify) {
276233
const { code } = await Terser.minify(index, {...config.terser, module: true})
277234
const src = `${header}\n${code}`;
278235
writePromises.push(fs.writeFile(output.file.replace(/js$/, "min.js"), src));
279236
sizeInfo.minified = src.length;
280237
sizeInfo.minifiedSrc = src;
238+
sizeInfo.shas[`${relativePath}${name}.min.js`] = bundling.sha384(src)
281239
}
282240
{
283241
const src = `${header}\n${index}`;
284242
writePromises.push(fs.writeFile(output.file, src));
285243
sizeInfo.fullSize = src.length;
286244
sizeInfo.fullSrc = src;
245+
sizeInfo.shas[`${relativePath}${name}.js`] = bundling.sha384(src)
287246
}
288247
await Promise.all(writePromises);
289248
return sizeInfo;
290249
}
291250

292251
// CDN build uses the exact same highlight.js distributable
293-
module.exports.buildBrowserHighlightJS = buildBrowserHighlightJS;
294-
module.exports.buildBrowserESMHighlightJS = buildBrowserESMHighlightJS;
252+
module.exports.buildCore = buildCore;
295253
module.exports.build = buildBrowser;

tools/build_cdn.js

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
11
const fs = require("fs").promises;
22
const fss = require("fs");
33
const glob = require("glob");
4-
const Terser = require("terser");
54
const zlib = require('zlib');
6-
const { getLanguages } = require("./lib/language");
7-
const { filter } = require("./lib/dependencies");
8-
const config = require("./build_config");
9-
const { install, installCleanCSS, mkdir } = require("./lib/makestuff");
5+
const { getLanguages } = require("./lib/language.js");
6+
const { filter } = require("./lib/dependencies.js");
7+
const config = require("./build_config.js");
8+
const { install, installCleanCSS, mkdir } = require("./lib/makestuff.js");
109
const log = (...args) => console.log(...args);
11-
const { buildBrowserHighlightJS, buildBrowserESMHighlightJS } = require("./build_browser");
12-
const { buildPackageJSON, writePackageJSON } = require("./build_node");
13-
const { rollupCode } = require("./lib/bundling.js");
10+
const { buildCore } = require("./build_browser.js");
11+
const { buildPackageJSON, writePackageJSON } = require("./build_node.js");
1412
const path = require("path");
1513
const bundling = require('./lib/bundling.js');
1614

1715
async function installPackageJSON(options) {
18-
const json = buildPackageJSON(options, {
19-
".": {
20-
import: options.minify ? "./es/index.min.js" : "./es/index.js",
21-
browser: options.minify ? "./highlight.min.js" : "./highlight.js",
22-
},
23-
"./lib/languages/*": {
24-
import: "./es/languages/*.js",
25-
browser: "./languages/*.js"
26-
},
27-
get "./lib/common"(){ return this["."]; },
28-
"./lib/core": { import: "./es/core.js" },
29-
"./styles/*": "./styles/*",
30-
"./package.json": "./package.json",
31-
});
16+
const json = buildPackageJSON(options);
3217
json.name = "@highlightjs/cdn-assets";
3318
json.description = json.description.concat(" (pre-compiled CDN assets)");
19+
// CDN assets do not need an export map, they are just a bunch of files.
20+
// The NPM package mostly only exists to populate CDNs and provide raw files.
21+
delete json.exports;
3422
await writePackageJSON(json);
3523
}
3624

37-
async function buildESMCore(options) {
38-
const input = { ...config.rollup.node.input, input: `src/highlight.js` };
39-
const output = {
40-
...config.rollup.node.output,
41-
format: "es",
42-
file: `${process.env.BUILD_DIR}/es/core.js`,
43-
};
44-
const core = await rollupCode(input, output);
45-
46-
const miniCore = options.minify ? await Terser.minify(core, {...config.terser, module: true}) : { code: core };
47-
await fs.writeFile(output.file, miniCore.code);
48-
return miniCore.code.length;
49-
}
50-
5125
let shas = {};
5226

5327
async function buildCDN(options) {
@@ -59,13 +33,9 @@ async function buildCDN(options) {
5933

6034
// all the languages are built for the CDN and placed into `/languages`
6135
const languages = await getLanguages();
62-
63-
let esmCoreSize, esmIndexSize;
64-
if (options.esm) {
65-
mkdir("es");
66-
await fs.writeFile(`${process.env.BUILD_DIR}/es/package.json`, `{ "type": "module" }`);
67-
esmCoreSize = await buildESMCore(options);
68-
}
36+
37+
let esmCoreSize = {};
38+
let esmCommonSize = {};
6939

7040
await installLanguages(languages, options);
7141

@@ -79,34 +49,43 @@ async function buildCDN(options) {
7949
embedLanguages = [];
8050
}
8151

82-
const size = await buildBrowserHighlightJS(embedLanguages, { minify: options.minify });
83-
if (options.esm) esmIndexSize = await buildBrowserESMHighlightJS("index", embedLanguages, { minify: options.minify });
84-
shas = Object.assign({}, size.shas, shas);
52+
const size = await buildCore("highlight", embedLanguages, { minify: options.minify, format: "cjs" });
53+
if (options.esm) {
54+
mkdir("es");
55+
await fs.writeFile(`${process.env.BUILD_DIR}/es/package.json`, `{ "type": "module" }`);
56+
esmCoreSize = await buildCore("core", [], {minify: options.minify, format: "es"});
57+
esmCommonSize = await buildCore("highlight", embedLanguages, { minify: options.minify, format: "es" });
58+
}
59+
shas = {
60+
...size.shas, ...esmCommonSize.shas, ...esmCoreSize.shas, ...shas
61+
};
8562

8663
await buildSRIDigests(shas);
8764

8865
log("-----");
89-
log("Embedded Lang :",
66+
log("Embedded Lang :",
9067
embedLanguages.map((el) => el.minified.length).reduce((acc, curr) => acc + curr, 0), "bytes");
91-
log("All Lang :",
68+
log("All Lang :",
9269
languages.map((el) => el.minified.length).reduce((acc, curr) => acc + curr, 0), "bytes");
93-
log("highlight.js :",
94-
size.full, "bytes");
70+
log("highlight.js :",
71+
size.fullSize, "bytes");
9572

9673
if (options.minify) {
97-
log("highlight.min.js :", size.minified, "bytes");
98-
log("highlight.min.js.gz :", zlib.gzipSync(size.minifiedSrc).length, "bytes");
74+
log("highlight.min.js :", size.minified, "bytes");
75+
log("highlight.min.js.gz :", zlib.gzipSync(size.minifiedSrc).length, "bytes");
9976
} else {
100-
log("highlight.js.gz :", zlib.gzipSync(size.fullSrc).length, "bytes");
77+
log("highlight.js.gz :", zlib.gzipSync(size.fullSrc).length, "bytes");
10178
}
102-
if(options.esm) {
103-
log("es/core.js :", esmCoreSize, "bytes");
104-
log("es/index.js :", esmIndexSize.fullSize, "bytes");
79+
if (options.esm) {
80+
log("es/core.js :", esmCoreSize.fullSize, "bytes");
81+
log("es/highlight.js :", esmCommonSize.fullSize, "bytes");
10582
if (options.minify) {
106-
log("es/index.min.js :", esmIndexSize.minified, "bytes");
107-
log("es/index.min.js.gz :", zlib.gzipSync(esmIndexSize.minifiedSrc).length, "bytes");
83+
log("es/core.min.js :", esmCoreSize.minified, "bytes");
84+
log("es/core.min.js.gz :", zlib.gzipSync(esmCoreSize.minifiedSrc).length, "bytes");
85+
log("es/highlight.min.js :", esmCommonSize.minified, "bytes");
86+
log("es/highlight.min.js.gz :", zlib.gzipSync(esmCommonSize.minifiedSrc).length, "bytes");
10887
} else {
109-
log("es/index.js.gz :", zlib.gzipSync(esmIndexSize.fullSrc).length, "bytes");
88+
log("es/highlight.js.gz :", zlib.gzipSync(esmCommonSize.fullSrc).length, "bytes");
11089
}
11190
}
11291
log("-----");
@@ -117,7 +96,7 @@ async function buildSRIDigests(shas) {
11796
const temp = await fs.readFile("./tools/templates/DIGESTS.md");
11897
const DIGEST_MD = temp.toString();
11998

120-
const version = require("../package").version;
99+
const version = require("../package.json").version;
121100
const digestList = Object.entries(shas).map(([k, v]) => `${v} ${k}`).join("\n");
122101

123102
const out = DIGEST_MD
@@ -131,7 +110,7 @@ async function buildSRIDigests(shas) {
131110
async function installLanguages(languages, options) {
132111
log("Building language files.");
133112
mkdir("languages");
134-
if(options.esm) mkdir("es/languages");
113+
if (options.esm) mkdir("es/languages");
135114

136115
await Promise.all(
137116
languages.map(async(language) => {
@@ -181,8 +160,10 @@ async function buildCDNLanguage(language, options) {
181160
await language.compile({ terser: config.terser });
182161
shas[name] = bundling.sha384(language.minified);
183162
await fs.writeFile(`${process.env.BUILD_DIR}/${name}`, language.minified);
184-
if (options.esm)
163+
if (options.esm) {
164+
shas[`es/${name}`] = bundling.sha384(language.minifiedESM);
185165
await fs.writeFile(`${process.env.BUILD_DIR}/es/${name}`, language.minifiedESM);
166+
}
186167
}
187168

188169
module.exports.build = buildCDN;

tools/build_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
]
2828
}
2929
},
30-
browser_core: {
30+
browser_iife: {
3131
input: {
3232
plugins: [
3333
jsonPlugin(),

tools/build_node.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ const generatePackageExports = () => ({
106106
"./styles/*": "./styles/*",
107107
"./types/*": "./types/*",
108108
});
109-
function buildPackageJSON(options, exports = generatePackageExports()) {
110-
const packageJson = require("../package");
109+
function buildPackageJSON(options) {
110+
const packageJson = require("../package.json");
111111

112-
if (options.esm) packageJson.exports = exports;
112+
if (options.esm) packageJson.exports = generatePackageExports();
113113

114114
return packageJson;
115115
}

tools/lib/bundling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function sha384(contents) {
2222
const hash = crypto.createHash('sha384');
2323
const data = hash.update(contents, 'utf-8');
2424
const gen_hash = data.digest('base64');
25-
return `sha384-${gen_hash}`
25+
return `sha384-${gen_hash}`;
2626
}
2727

2828
module.exports = { rollupWrite, rollupCode, sha384 };

0 commit comments

Comments
 (0)