Skip to content

Commit

Permalink
first prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
billouboq committed Aug 2, 2018
1 parent 6ee0ded commit 8cedf0d
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 6 deletions.
5 changes: 5 additions & 0 deletions __tests__/assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a {
font-family: Roboto;
color: black;
font-size: 12px;
}
1 change: 1 addition & 0 deletions __tests__/assets/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc
20 changes: 20 additions & 0 deletions __tests__/ast/ast.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import postcss from 'postcss';
import * as AST from "../../src/ast/ast";

describe("Test ast functions", () => {
it("Should format postcss ast to new ast format", async () => {
const input = postcss.parse("a{font-size: 12px; color: red}");
const result = AST.format(input);
const expected = [
{
selector: "a",
props: [
{prop: "font-size", value: "12px"},
{prop: "color", value: "red"}
]
}
];

expect(result).toEqual(expected);
})
})
11 changes: 11 additions & 0 deletions __tests__/files/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as files from "../../src/files/files";

describe("Test files functions", () => {
it("Should get content of file", async () => {
const filePath = "./__tests__/assets/test";
const result = await files.getContent(filePath);
const expected = "abc"

expect(result).toEqual(expected);
})
})
8 changes: 6 additions & 2 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ function run(input: string, output: string, opts: object) {
return postcss([ removeDuplicateCSS(opts) ])
.process(input)
.then(result => {
expect(result.css).toEqual(output);
expect(result.css.replace(/\s/g,'')).toEqual(output);
expect(result.warnings().length).toBe(0);
});
}

describe("Test main functions", () => {
it("Should remove duplicate css properties", () => {
return run('a{}', 'a{}', {});
return run(
'a{font-size: 12px; color: blue; font-family: Roboto}',
'a{color:blue}',
{target: "./__tests__/assets/style.css"}
);
})
})
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "RemoveDuplicateCSS",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "./dist/index.js",
"scripts": {
"build": "tsc",
"test": "jest tests",
Expand All @@ -13,6 +13,7 @@
"license": "ISC",
"devDependencies": {
"@types/jest": "^23.3.1",
"@types/node": "^10.5.5",
"jest": "^23.4.2",
"ts-jest": "^23.0.1",
"typescript": "^3.0.1"
Expand Down
11 changes: 11 additions & 0 deletions src/ast/ast.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare namespace AST {
interface Node {
selector: string,
props: NodeProps[]
}

interface NodeProps {
prop: string,
value: string
}
}
10 changes: 10 additions & 0 deletions src/ast/ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="./ast.d.ts" />

export function format(ast) : AST.Node[] {
return ast.nodes.map(({selector, nodes}) => {
return {
selector,
props: nodes.map(({prop, value}) => ({prop, value}))
}
});
}
13 changes: 13 additions & 0 deletions src/files/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from "fs";

export function getContent(path: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, file) => {
if (err) {
reject(err);
} else {
resolve(file);
}
});
})
}
27 changes: 24 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
"use strict";

import postcss from "postcss";
import * as AST from "./ast/ast";
import * as files from "./files/files";

export default postcss.plugin('RemoveDuplicateCSS', (opts) => {
return async (root, result) => {
try {
const cssContent = await files.getContent(opts.target);
const cssAst = postcss.parse(cssContent);
const newAst = AST.format(cssAst);

root.walkRules((rule) => {
const findedAst = newAst.find(ast => ast.selector === rule.selector);

return function (root, result) {
if (findedAst) {
rule.walkDecls(function(decl) {
if (findedAst.props.some(prop => prop.prop === decl.prop && prop.value === decl.value)) {
decl.remove();
}
});

if (!rule.nodes || !rule.nodes.length) {
rule.remove();
}
}
});
} catch (err) {
console.log("err", err);
console.error(err);
}
};
});

0 comments on commit 8cedf0d

Please sign in to comment.