mirror of
https://github.com/wassname/talk.git
synced 2026-07-04 06:44:54 +08:00
b9a8fdb77b
Merge pull request #1731 from coralproject/next-watcher-flags Watcher --only Use JSDocs comments (#1727) Merge branch 'next' into prevent-compile-loop-relay Merge pull request #1726 from coralproject/prevent-compile-loop-relay [next] Adapt relay watch config [next] Remove nodemon (#1725) * Remove old nodemon configs * Remove nodemon [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 Adapt files.exclude (#1736) Permalink ui Adding Copy to clipboard functionality WIP clean request wip progress progress work in progress wip ui functionality Translations :/ wip Merge branch 'permalink' of github.com:coralproject/talk into permalink * 'permalink' of github.com:coralproject/talk: (42 commits) [next] Support server side jest testing (#1747) Update snapshots Add comments Remove precss Move react-responsive to dev deps Remove comment Mobile first approach Support standard css variables, dynamically set spacing-unit Add docs Fully implement Flex and MatchMedia Responsive Components <3 fix: linting fix: adjusted pageInfo Remove obsoloe snapshot Move jsdom to dev deps Mark comments as always returning a value Add comment Fix unit tests Translate, concept for translation and id strings Add aria props ... Adding Attachment and Popover Component merge conflicts progress Any Working Support for refs Ready Merge branch 'permalink' of github.com:coralproject/talk into permalink * 'permalink' of github.com:coralproject/talk: (101 commits) Ready Support for refs Working Any progress merge conflicts Make timeagoFormatter optional More colors Colors Short circuit endless respawn fix: new mongo parser Remove jest from watcher config, as it doesnt run well inside Add jest set Apply suggestions Move react-timeago to dev Upgrade docz Cleanup docz scripts Support watcher sets [next] Support server side jest testing (#1747) Make filter only a pure function ...
106 lines
2.6 KiB
JavaScript
106 lines
2.6 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, basename, extname } = 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 selectors = getCSSSelectors(textCSS, path);
|
|
const component = basename(path, extname(path));
|
|
|
|
// Prefix class names with component name.
|
|
Object.keys(selectors).forEach(k => {
|
|
selectors[k] = selectors[k]
|
|
.split(/\s+/)
|
|
.map(v => `${component}-${v}`)
|
|
.join(" ");
|
|
});
|
|
|
|
const exprt = JSON.stringify(selectors);
|
|
return `module.exports = ${exprt}`;
|
|
},
|
|
};
|