Skip to content

Commit 76efccf

Browse files
committed
(fix/chore) safer wrapper for IIFE grammar plugins
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.
1 parent 0ce3979 commit 76efccf

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

tools/lib/language.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,18 @@ class Language {
7979

8080

8181
async function compileLanguage (language, options) {
82-
const IIFE_HEADER_REGEX = /^(var hljsGrammar = )?\(function \(\)/;
83-
8482
// TODO: cant we use the source we already have?
8583
const input = { ...build_config.rollup.browser_iife.input, input: language.path };
8684
const output = { ...build_config.rollup.browser_iife.output, name: `hljsGrammar`, file: "out.js" };
8785
output.footer = null;
88-
86+
8987
const data = await rollupCode(input, output);
90-
const iife = data.replace(IIFE_HEADER_REGEX, `hljs.registerLanguage('${language.name}', function ()`);
88+
const iife = `
89+
(function(){
90+
${data}
91+
hljs.registerLanguage('${language.name}', hljsGrammar);
92+
})();
93+
`.trim();
9194
const esm = `${data};\nexport default hljsGrammar;`;
9295

9396
language.module = iife;

0 commit comments

Comments
 (0)