Files
talk/config/jest/cssTransform.js
T
Kiwi 37cb23dd59 [next] Jest implementation for React Components (#1733)
* Make jest testing work with custom path and css modules

* Add first test

* feat: added unit tests to ci

* fix: updated package-lock.json

* Update cssTransform.js

* Update cssTransform.js

* Fix test in ci
2018-07-05 09:04:38 -06:00

95 lines
2.3 KiB
JavaScript

"use strict";
// Adapted version of https://github.com/Connormiha/jest-css-modules-transform
// Copyright https://github.com/Connormiha/jest-css-modules-transform/graphs/contributors
// This oututs `module.exports = cssObject` instead of `module.exports = { default: cssObject }`;
const { sep: pathSep, resolve } = require("path");
const postcss = require("postcss");
const postcssNested = postcss([require("postcss-nested")]);
const REG_EXP_NAME_BREAK_CHAR = /[\s,.{/#[:]/;
const getCSSSelectors = (css, path) => {
const end = css.length;
let i = 0;
let char;
let bracketsCount = 0;
const result = {};
while (i < end) {
if (i === -1) {
throw Error(`Parse error ${path}`);
}
if (css.indexOf("/*", i) === i) {
i = css.indexOf("*/", i + 2);
// Unclosed comment. Break to avoid infinity loop
if (i === -1) {
// Don't parse, but save collected result
return result;
}
continue;
}
char = css[i];
if (char === "{") {
bracketsCount++;
i++;
continue;
}
if (char === "}") {
bracketsCount--;
i++;
continue;
}
if (char === '"' || char === "'") {
do {
i = css.indexOf(char, i + 1);
// Syntax error since this line. Don't parse, but save collected result
if (i === -1) {
return result;
}
} while (css[i - 1] === "\\");
i++;
continue;
}
if (bracketsCount > 0) {
i++;
continue;
}
if (char === "." || char === "#") {
i++;
const startWord = i;
while (!REG_EXP_NAME_BREAK_CHAR.test(css[i])) {
i++;
}
const word = css.slice(startWord, i);
result[word] = word;
continue;
}
if (css.indexOf("@keyframes", i) === i) {
i += 10;
while (REG_EXP_NAME_BREAK_CHAR.test(css[i])) {
i++;
}
const startWord = i;
while (!REG_EXP_NAME_BREAK_CHAR.test(css[i])) {
i++;
}
const word = css.slice(startWord, i);
result[word] = word;
continue;
}
i++;
}
return result;
};
module.exports = {
process(src, path, config) {
const filename = path.slice(path.lastIndexOf(pathSep) + 1);
const textCSS = postcssNested.process(src).css;
const exprt = JSON.stringify(getCSSSelectors(textCSS, path));
return `module.exports = ${exprt}`;
},
};