Skip to content

Commit

Permalink
(fix/chore) safer wrapper for IIFE grammar plugins
Browse files Browse the repository at this point in the history
Resolves #3363.

The `rollup` output format changed out from underneath us which
means that our simple text processing of IIFE output broke and
we were outputting invalid JS for our CDN language builds.

Now we wrap the IIFE inside our own second IIFE and then perform
`hljs.registerLanguage` there. This should be more resilient to
future subtle changes in the output.
  • Loading branch information
joshgoebel committed Oct 17, 2021
1 parent 0ce3979 commit 76efccf
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions tools/lib/language.js
Expand Up @@ -79,15 +79,18 @@ class Language {


async function compileLanguage (language, options) {
const IIFE_HEADER_REGEX = /^(var hljsGrammar = )?\(function \(\)/;

// 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" };
output.footer = null;

const data = await rollupCode(input, output);
const iife = data.replace(IIFE_HEADER_REGEX, `hljs.registerLanguage('${language.name}', function ()`);
const iife = `
(function(){
${data}
hljs.registerLanguage('${language.name}', hljsGrammar);
})();
`.trim();
const esm = `${data};\nexport default hljsGrammar;`;

language.module = iife;
Expand Down

0 comments on commit 76efccf

Please sign in to comment.