Skip to content

Commit

Permalink
Update compileLanguage to produce smaller dist files
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jun 30, 2021
1 parent 8d838de commit fe9de56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 14 additions & 0 deletions tools/build_config.js
Expand Up @@ -44,6 +44,20 @@ module.exports = {
footer: "if (typeof exports === 'object' && typeof module !== 'undefined') { module.exports = hljs; }",
interop: false
}
},
browser: {
input: {
plugins: [
jsonPlugin(),
cjsPlugin(),
nodeResolve()
]
},
output: {
name: "hljs",
format: "es",
interop: false
}
}
},
terser: {
Expand Down
13 changes: 6 additions & 7 deletions tools/lib/language.js
Expand Up @@ -85,16 +85,15 @@ class Language {


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

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

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

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

0 comments on commit fe9de56

Please sign in to comment.