Skip to content

Commit fe9de56

Browse files
committed
Update compileLanguage to produce smaller dist files
1 parent 8d838de commit fe9de56

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

tools/build_config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ module.exports = {
4444
footer: "if (typeof exports === 'object' && typeof module !== 'undefined') { module.exports = hljs; }",
4545
interop: false
4646
}
47+
},
48+
browser: {
49+
input: {
50+
plugins: [
51+
jsonPlugin(),
52+
cjsPlugin(),
53+
nodeResolve()
54+
]
55+
},
56+
output: {
57+
name: "hljs",
58+
format: "es",
59+
interop: false
60+
}
4761
}
4862
},
4963
terser: {

tools/lib/language.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,15 @@ class Language {
8585

8686

8787
async function compileLanguage (language, options) {
88-
const IIFE_HEADER_REGEX = /^(var hljsGrammar = )?\(function \(\)/;
88+
const EXPORT_REGEX = /export default (.*);/;
8989

90-
// TODO: cant we use the source we already have?
91-
const input = { ...build_config.rollup.browser_iife.input, input: language.path };
92-
const output = { ...build_config.rollup.browser_iife.output, name: `hljsGrammar`, file: "out.js" };
90+
// We can't use the source as extra language may be written in CJS
91+
const input = { ...build_config.rollup.browser.input, input: language.path };
92+
const output = { ...build_config.rollup.browser.output, file: "out.js" };
9393
output.footer = null;
9494

95-
const data = await rollupCode(input, output);
96-
const iife = data.replace(IIFE_HEADER_REGEX, `hljs.registerLanguage('${language.name}', function ()`);
97-
const esm = `${data};\nexport default hljsGrammar;`;
95+
const esm = await rollupCode(input, output);
96+
const iife = `hljs.registerLanguage('${language.name}', (function (){"use strict";` + esm.replace(EXPORT_REGEX, 'return $1') + '})())';
9897

9998
language.module = iife;
10099
const miniESM = await Terser.minify(esm, options.terser);

0 commit comments

Comments
 (0)