Skip to content

Commit

Permalink
Merge pull request #12 from J-Kallunki/bugfix/remove-clean-css
Browse files Browse the repository at this point in the history
[Bugfix] remove clean css
  • Loading branch information
J-Kallunki committed Feb 4, 2019
2 parents 6e58356 + b8e48fb commit 09a7eb8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"homepage": "https://github.com/J-Kallunki/css-in-to-js#readme",
"dependencies": {
"clean-css": "4.2.1",
"lodash": "4.17.11",
"postcss": "7.0.5"
},
Expand Down
29 changes: 18 additions & 11 deletions src/cssintojs.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import postcss from "postcss";
import CleanCSS from "clean-css";
import camelCase from "lodash/camelCase";
import upperFirst from "lodash/upperFirst";
import postcss from 'postcss';
import camelCase from 'lodash/camelCase';
import upperFirst from 'lodash/upperFirst';
const _ = { camelCase, upperFirst };

export const minifyCss = cssString =>
new CleanCSS({ compatibility: "*" }).minify(cssString).styles;
cssString
.replace(/\r?\n|\r/g, '') // Remove line breaks
.replace(/(\|\\r|\ |\\f|\\v)+/g, '') // Remove tabs etc.
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1') // Remove comments.
.replace(/ +/g, ' ') // Remove redundant whitespace.
.replace(/ *(:|;|{|}) */g, '$1'); // Remove spaces around those characters

export const cssToJs = (cssString, { cssToString = false } = {}) => {
const root = postcss.parse(cssString, {});
const rootRules = !!root && !!root.nodes && root.nodes.filter(node => node.type === "rule");
const rootRules =
!!root && !!root.nodes && root.nodes.filter(node => node.type === 'rule');

if (!rootRules || rootRules.length < 1) {
if (console) console.error("No CSS selectors found");
if (console) console.error('No CSS selectors found');
return undefined;
}

return Object.entries(rootRules).reduce((acc, [key, { selector, nodes }]) => {
const formatToObject = prop => {
const camelProp = _.camelCase(prop);
return prop.startsWith("-") ? _.upperFirst(camelProp) : camelProp;
return prop.startsWith('-') ? _.upperFirst(camelProp) : camelProp;
};
const selectors = !!selector ? selector.split(",").map(s => s.trim()): [];
const cssDeclarations = !!nodes ? nodes.filter(node => node.type === "decl") : [];
const selectors = !!selector ? selector.split(',').map(s => s.trim()) : [];
const cssDeclarations = !!nodes
? nodes.filter(node => node.type === 'decl')
: [];
const cssDeclarationsFormatted = !!cssToString
? cssDeclarations.map(({ prop, value }) => `${prop}: ${value};`).join(" ")
? cssDeclarations.map(({ prop, value }) => `${prop}: ${value};`).join(' ')
: cssDeclarations.reduce(
(accDecl, { prop, value }) => ({
...accDecl,
Expand Down
20 changes: 12 additions & 8 deletions src/cssintojs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('csstocssinjs', () => {
it('Should be converted to an object', () => {
expect(resets).to.be.a('object');
expect(resets2).to.be.a('object');
})
});

it('Should contain selectors', () => {
const keys = Object.keys(resets);
Expand All @@ -37,7 +37,7 @@ describe('csstocssinjs', () => {

expect(keys[3]).to.equal('[type="reset"]::-moz-focus-inner');
expect(keys2[3]).to.equal('[type="reset"]::-moz-focus-inner');
})
});

it('Should contain declarations', () => {
const values = resets['html'];
Expand All @@ -47,22 +47,26 @@ describe('csstocssinjs', () => {
expect(values['lineHeight']).to.equal('1.15');

expect(values2).to.be.a('string');
expect(values2).to.equal('line-height: 1.15; -webkit-text-size-adjust: 100%;');

})
expect(values2).to.equal(
'line-height: 1.15; -webkit-text-size-adjust: 100%;'
);
});

it('Should need selector', () => {
const noSelector = cssToJs(`line-height: 1.15; /* 1 */`);

expect(noSelector).to.equal(undefined);
})
});
});

describe('minifyCss', () => {
const minify = minifyCss(css);

it('Should still be a string', () => {
expect(minify).to.be.a('string');
})
});

});
it('Should be certain length of a string', () => {
expect(minify).to.have.lengthOf(208);
});
});
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -987,13 +987,6 @@ class-utils@^0.3.5:
isobject "^3.0.0"
static-extend "^0.1.1"

clean-css@4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/clean-css/-/clean-css-4.2.1.tgz#2d411ef76b8569b6d0c84068dabe85b0aa5e5c17"
integrity sha512-4ZxI6dy4lrY6FHzfiy1aEOXgu4LIsW2MhwG0VBKdcoGoH/XLFgaHSdLTGr4O8Be6A8r3MOphEiI8Gc1n0ecf3g==
dependencies:
source-map "~0.6.0"

cli-cursor@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-1.0.2.tgz#64da3f7d56a54412e59794bd62dc35295e8f2987"
Expand Down Expand Up @@ -3274,7 +3267,7 @@ source-map@^0.5.0, source-map@^0.5.6:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=

source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1:
source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
Expand Down

0 comments on commit 09a7eb8

Please sign in to comment.