fix(client): transform html scripts

This commit is contained in:
Valeriy S
2018-12-12 17:12:30 +03:00
committed by Stuart Taylor
parent 003492cd7c
commit 4bf89a873e
2 changed files with 43 additions and 17 deletions

View File

@ -95,10 +95,7 @@ A required file can not have both a src and a link: src = ${src}, link = ${link}
const body = Promise.all(files).then(files =>
files
.reduce(
(body, file) => [...body, file.contents + file.tail + htmlCatch],
[]
)
.reduce((body, file) => [...body, file.contents + htmlCatch], [])
.map(source => createBody({ source }))
);

View File

@ -92,29 +92,58 @@ export const babelTransformer = cond([
]);
const sassWorker = new WorkerExecutor('sass-compile');
async function transformSASS(element) {
const styleTags = element.querySelectorAll('style[type="text/sass"]');
await Promise.all(
[].map.call(styleTags, async style => {
style.type = 'text/css';
style.innerHTML = await sassWorker.execute(style.innerHTML, 2000);
})
);
}
const htmlSassTransformCode = file => {
function transformScript(element) {
const scriptTags = element.querySelectorAll('script');
scriptTags.forEach(script => {
script.innerHTML = tryTransform(babelTransformCode(babelOptionsJSX))(
script.innerHTML
);
});
}
const transformHtml = async function(file) {
const div = document.createElement('div');
div.innerHTML = file.contents;
const styleTags = div.querySelectorAll('style[type="text/sass"]');
if (styleTags.length > 0) {
return Promise.all(
[].map.call(styleTags, async style => {
style.type = 'text/css';
style.innerHTML = await sassWorker.execute(style.innerHTML, 2000);
})
).then(() => vinyl.transformContents(() => div.innerHTML, file));
}
await Promise.all([transformSASS(div), transformScript(div)]);
return vinyl.transformContents(() => div.innerHTML, file);
};
export const htmlSassTransformer = cond([
[testHTML, htmlSassTransformCode],
export const composeHTML = cond([
[
testHTML,
flow(
partial(
vinyl.transformHeadTailAndContents,
source => {
const div = document.createElement('div');
div.innerHTML = source;
return div.innerHTML;
}
),
partial(vinyl.compileHeadTail, '')
)
],
[stubTrue, identity]
]);
export const htmlTransformer = cond([
[testHTML, transformHtml],
[stubTrue, identity]
]);
export const transformers = [
replaceNBSP,
babelTransformer,
htmlSassTransformer
composeHTML,
htmlTransformer
];