This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
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
9 changed files
with
102 additions
and
78 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
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,7 +1,7 @@ | ||
export { default as Output } from './lib/interfaces/Output' | ||
export { default as Output } from "./lib/interfaces/Output"; | ||
|
||
import StaticStyles from './lib/StaticStyles' | ||
import StaticStyles from "./lib/StaticStyles"; | ||
|
||
export default (html: string, css: string) => { | ||
return new StaticStyles(html, css).get() | ||
} | ||
return new StaticStyles(html, css).get(); | ||
}; |
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,89 +1,89 @@ | ||
import * as Css from 'css' | ||
import * as Css from "css"; | ||
|
||
import Context from './interfaces/Context' | ||
import Output from './interfaces/Output' | ||
import Context from "./interfaces/Context"; | ||
import Output from "./interfaces/Output"; | ||
|
||
import isPseudo from './isPseudo' | ||
import convertToDom from './convertToDom' | ||
import humanSize from './humanSize' | ||
import convertToDom from "./convertToDom"; | ||
import humanSize from "./humanSize"; | ||
import isPseudo from "./isPseudo"; | ||
|
||
export default class StaticStyles { | ||
private startTime: [number, number] | ||
private context: Context | ||
private startTime: [number, number]; | ||
private context: Context; | ||
|
||
constructor( | ||
private html: string, | ||
private css: string | ||
private css: string, | ||
) { | ||
this.startTime = process.hrtime() | ||
this.startTime = process.hrtime(); | ||
|
||
this.context = convertToDom(html) | ||
this.context = convertToDom(html); | ||
} | ||
|
||
public get(): Output { | ||
const ast = Css.parse(this.css); | ||
|
||
ast.stylesheet.rules = this.filterStyles(ast.stylesheet.rules); | ||
|
||
const compressedCss: string = Css.stringify(ast, { | ||
compress: true, | ||
}); | ||
|
||
return { | ||
css: compressedCss, | ||
stats: { | ||
efficiency: 1 - (compressedCss.length / this.css.length), | ||
input: humanSize(this.css.length), | ||
output: humanSize(compressedCss.length), | ||
timeSpent: this.getTimeSpend(), | ||
}, | ||
}; | ||
} | ||
|
||
private filterStyles(rules: any[]): any[] { | ||
return rules.map((rule) => { | ||
|
||
if (rule.selectors) { | ||
const matches = rule.selectors.map((selector: string): boolean => { | ||
const pseudo = isPseudo(selector) | ||
const pseudo = isPseudo(selector); | ||
let matchingElements: NodeList; | ||
let matchingElementsArray: Node[] = []; | ||
|
||
if (pseudo) { | ||
const selectorSet = selector.split(':') | ||
const selectorSet = selector.split(":"); | ||
|
||
if (selectorSet[0] && selectorSet[0].length > 0) { | ||
selector = selectorSet[0]; | ||
} else { | ||
return true | ||
return true; | ||
} | ||
} | ||
|
||
try { | ||
matchingElements = this.context.document.querySelectorAll(selector) | ||
matchingElementsArray = Array.from(matchingElements) | ||
matchingElements = this.context.document.querySelectorAll(selector); | ||
matchingElementsArray = Array.from(matchingElements); | ||
} catch (error) {} | ||
|
||
return matchingElementsArray.length > 0 | ||
}) | ||
return matchingElementsArray.length > 0; | ||
}); | ||
|
||
if (matches.indexOf(true) === -1) { | ||
rule.declarations = [] | ||
rule.declarations = []; | ||
} | ||
} else if (rule.rules) { | ||
rule.rules = this.filterStyles(rule.rules); | ||
} | ||
|
||
return rule | ||
}) | ||
return rule; | ||
}); | ||
} | ||
|
||
private getTimeSpend(): [number, number] { | ||
const currentTime: [number, number] = process.hrtime() | ||
const currentTime: [number, number] = process.hrtime(); | ||
|
||
return [ | ||
currentTime[0] - this.startTime[0], | ||
(currentTime[1] - this.startTime[1]) / 1000000 // get milli sec | ||
] | ||
} | ||
|
||
public get(): Output { | ||
const ast = Css.parse(this.css) | ||
|
||
ast.stylesheet.rules = this.filterStyles(ast.stylesheet.rules) | ||
|
||
const compressedCss: string = Css.stringify(ast, { | ||
compress: true, | ||
}) | ||
|
||
return { | ||
stats: { | ||
efficiency: 1 - (compressedCss.length / this.css.length), | ||
timeSpent: this.getTimeSpend(), | ||
input: humanSize(this.css.length), | ||
output: humanSize(compressedCss.length), | ||
}, | ||
css: compressedCss, | ||
} | ||
(currentTime[1] - this.startTime[1]) / 1000000, // get milli sec | ||
]; | ||
} | ||
} |
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,19 +1,19 @@ | ||
import { JSDOM } from 'jsdom' | ||
import { JSDOM } from "jsdom"; | ||
|
||
import Context from './interfaces/Context' | ||
import Context from "./interfaces/Context"; | ||
|
||
const convertToDom = (html: string): Context => { | ||
if (!html || html.length === 0) { | ||
throw new Error('HTML not set') | ||
throw new Error("HTML not set"); | ||
} | ||
|
||
let jsdom: JSDOM = new JSDOM(html) | ||
const jsdom: JSDOM = new JSDOM(html); | ||
|
||
return { | ||
document: jsdom.window.document, | ||
jsdom, | ||
window: jsdom.window, | ||
document: jsdom.window.document | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export default convertToDom | ||
export default convertToDom; |
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,7 +1,7 @@ | ||
export default (size: number): string => { | ||
const i = Math.floor(Math.log(size) / Math.log(1024)); | ||
const number: string = (size / Math.pow(1024, i)).toFixed(2) | ||
const sizes: string[] = ['B', 'kB', 'MB', 'GB', 'TB'] | ||
const sizeNumber: string = (size / Math.pow(1024, i)).toFixed(2); | ||
const sizes: string[] = ["B", "kB", "MB", "GB", "TB"]; | ||
|
||
return `${number} ${sizes[i]}` | ||
} | ||
return `${sizeNumber} ${sizes[i]}`; | ||
}; |
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,4 +1,4 @@ | ||
export default interface Output { | ||
stats: object, | ||
css: string, | ||
stats: object; | ||
css: 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
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,26 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"extends": [ | ||
"tslint:recommended" | ||
], | ||
"jsRules": {}, | ||
"rules": { | ||
"variable-name": [ | ||
true, | ||
"ban-keywords", | ||
"check-format" | ||
], | ||
"interface-name": { | ||
"options": [ | ||
"never-prefix" | ||
] | ||
} | ||
}, | ||
"rulesDirectory": [], | ||
"linterOptions": { | ||
"exclude": [ | ||
"dist/**/*", | ||
"node_modules/**/*" | ||
] | ||
} | ||
} |
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