From 76efccf3a36fdcf538403ebd498a1bd5c1166f4d Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 17 Oct 2021 16:03:17 -0400 Subject: [PATCH] (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. --- tools/lib/language.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/lib/language.js b/tools/lib/language.js index 0e25f973c0..30dd1367a6 100644 --- a/tools/lib/language.js +++ b/tools/lib/language.js @@ -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;