-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
a { | ||
font-family: Roboto; | ||
color: black; | ||
font-size: 12px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
abc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
}); |