From 1dfe1f1ec05a677723ee5b333fb45ac594ea412a Mon Sep 17 00:00:00 2001 From: ValeraS Date: Sun, 8 Jul 2018 22:12:38 +0300 Subject: [PATCH] fix: Sass has not been compiled when tests running --- packages/learn/src/head/index.js | 3 +- packages/learn/src/head/sassjs.js | 9 ++++ .../Challenges/rechallenge/transformers.js | 54 ++++++++++--------- 3 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 packages/learn/src/head/sassjs.js diff --git a/packages/learn/src/head/index.js b/packages/learn/src/head/index.js index 51da3baab3..a851780487 100644 --- a/packages/learn/src/head/index.js +++ b/packages/learn/src/head/index.js @@ -2,9 +2,10 @@ import favicons from './favicons'; import meta from './meta'; import styleSheets from './styleSheets'; import mathjax from './mathjax'; +import sassjs from './sassjs'; const metaAndStyleSheets = meta - .concat(favicons, styleSheets, mathjax) + .concat(favicons, styleSheets, mathjax, sassjs) .map((element, i) => ({ ...element, key: `meta-stylesheet-${i}` })); export default metaAndStyleSheets; diff --git a/packages/learn/src/head/sassjs.js b/packages/learn/src/head/sassjs.js new file mode 100644 index 0000000000..9a338babff --- /dev/null +++ b/packages/learn/src/head/sassjs.js @@ -0,0 +1,9 @@ +import React from 'react'; + +const cdnAddr = + 'https://cdnjs.cloudflare.com/ajax/libs/sass.js/' + + '0.10.9/sass.sync.min.js'; + +const sassjs = [ -`; // if shouldProxyConsole then we change instances of console log // to `window.__console.log` // this let's us tap into logging into the console. @@ -107,12 +88,37 @@ export const babelTransformer = cond([ [stubTrue, identity] ]); -export const sassTransformer = cond([ - [testSASS, partial(vinyl.appendToTail, browserSassCompiler)], +const htmlSassTransformCode = file => { + let doc = document.implementation.createHTMLDocument(); + doc.body.innerHTML = file.contents; + let styleTags = [].filter.call( + doc.querySelectorAll('style'), + style => style.type === 'text/sass' + ); + if (styleTags.length === 0 || typeof Sass === 'undefined') { + return vinyl.transformContents(() => doc.body.innerHTML, file); + } + return styleTags.reduce((obs, style) => { + return obs.flatMap(file => new Promise(resolve => { + window.Sass.compile(style.innerHTML, function(result) { + style.type = 'text/css'; + style.innerHTML = result.text; + resolve(vinyl.transformContents(() => doc.body.innerHTML, file)); + }); + })); + }, Observable.of(file)); +}; + +export const htmlSassTransformer = cond([ + [testHTML, htmlSassTransformCode], [stubTrue, identity] ]); -export const _transformers = [replaceNBSP, babelTransformer, sassTransformer]; +export const _transformers = [ + replaceNBSP, + babelTransformer, + htmlSassTransformer +]; export function applyTransformers(file, transformers = _transformers) { return transformers.reduce((obs, transformer) => {