From 7cdf01276d035acd63d695b3b571fec4990d5abe Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 11 Feb 2020 14:35:55 +0100 Subject: [PATCH] fix: ensure Babel options exist when used Also renamed 'config' to 'options' to be consistent with the calling function. --- .../Challenges/rechallenge/transformers.js | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/client/src/templates/Challenges/rechallenge/transformers.js b/client/src/templates/Challenges/rechallenge/transformers.js index da6a51ab9e..891717e9c6 100644 --- a/client/src/templates/Challenges/rechallenge/transformers.js +++ b/client/src/templates/Challenges/rechallenge/transformers.js @@ -131,24 +131,17 @@ function tryTransform(wrap = identity) { }; } -const babelTransformer = ({ preview = false, protect = true }) => { - let options = babelOptionsJSBase; - // we always protect the preview, since it evaluates as the user types and - // they may briefly have infinite looping code accidentally - if (protect) { - options = preview ? babelOptionsJSPreview : babelOptionsJS; - } else { - options = preview ? babelOptionsJSPreview : options; - } +const babelTransformer = options => { return cond([ [ testJS, async code => { await loadBabel(); await loadPresetEnv(); + const babelOptions = getBabelOptions(options); return partial( vinyl.transformHeadTailAndContents, - tryTransform(babelTransformCode(options)) + tryTransform(babelTransformCode(babelOptions)) )(code); } ], @@ -170,6 +163,18 @@ const babelTransformer = ({ preview = false, protect = true }) => { ]); }; +function getBabelOptions({ preview = false, protect = true }) { + let options = babelOptionsJSBase; + // we always protect the preview, since it evaluates as the user types and + // they may briefly have infinite looping code accidentally + if (protect) { + options = preview ? babelOptionsJSPreview : babelOptionsJS; + } else { + options = preview ? babelOptionsJSPreview : options; + } + return options; +} + const sassWorker = createWorker(sassCompile); async function transformSASS(element) { const styleTags = element.querySelectorAll('style[type="text/sass"]'); @@ -189,6 +194,7 @@ async function transformScript(element) { script.innerHTML = tryTransform(babelTransformCode(babelOptionsJS))( script.innerHTML ); + console.log('transformed:', script.innerHTML); }); } @@ -219,9 +225,9 @@ export const htmlTransformer = cond([ [stubTrue, identity] ]); -export const getTransformers = config => [ +export const getTransformers = options => [ replaceNBSP, - babelTransformer(config ? config : {}), + babelTransformer(options ? options : {}), composeHTML, htmlTransformer ];