fix: ensure Babel options exist when used

Also renamed 'config' to 'options' to be consistent with the calling
function.
pull/38240/head
Oliver Eyton-Williams 2020-02-11 14:35:55 +01:00 committed by mrugesh
parent a27992a8b6
commit 7cdf01276d
1 changed files with 18 additions and 12 deletions

View File

@ -131,24 +131,17 @@ function tryTransform(wrap = identity) {
}; };
} }
const babelTransformer = ({ preview = false, protect = true }) => { const babelTransformer = options => {
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 cond([ return cond([
[ [
testJS, testJS,
async code => { async code => {
await loadBabel(); await loadBabel();
await loadPresetEnv(); await loadPresetEnv();
const babelOptions = getBabelOptions(options);
return partial( return partial(
vinyl.transformHeadTailAndContents, vinyl.transformHeadTailAndContents,
tryTransform(babelTransformCode(options)) tryTransform(babelTransformCode(babelOptions))
)(code); )(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); const sassWorker = createWorker(sassCompile);
async function transformSASS(element) { async function transformSASS(element) {
const styleTags = element.querySelectorAll('style[type="text/sass"]'); const styleTags = element.querySelectorAll('style[type="text/sass"]');
@ -189,6 +194,7 @@ async function transformScript(element) {
script.innerHTML = tryTransform(babelTransformCode(babelOptionsJS))( script.innerHTML = tryTransform(babelTransformCode(babelOptionsJS))(
script.innerHTML script.innerHTML
); );
console.log('transformed:', script.innerHTML);
}); });
} }
@ -219,9 +225,9 @@ export const htmlTransformer = cond([
[stubTrue, identity] [stubTrue, identity]
]); ]);
export const getTransformers = config => [ export const getTransformers = options => [
replaceNBSP, replaceNBSP,
babelTransformer(config ? config : {}), babelTransformer(options ? options : {}),
composeHTML, composeHTML,
htmlTransformer htmlTransformer
]; ];