Skip to content

Commit

Permalink
Merge pull request #29 from jsnanigans/master
Browse files Browse the repository at this point in the history
Pass content and css as raw data
  • Loading branch information
Ffloriel authored Nov 26, 2017
2 parents 09a9786 + b841d52 commit fec778a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
26 changes: 26 additions & 0 deletions __tests__/purgecssDefault.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,30 @@ describe('purge methods with files and default extractor', () => {
expect(result.includes('parent2')).toBe(false)
})
})

})

describe('purge methods with raw content and default extractor', () => {
it('remove .remove - content passed', () => {
const purgecss = new Purgecss({
content: [{
raw: '<span class="double-class"></span>',
extension: 'html'
}],
css: [{
raw: `
.single {
color: black;
}
.double-class {
color: black;
}
`
}]
})
const result = purgecss.purge()[0].css
expect(result.includes('single')).toBe(false)
expect(result.includes('double-class')).toBe(true)
})
})
2 changes: 1 addition & 1 deletion lib/purgecss.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/purgecss.js

Large diffs are not rendered by default.

56 changes: 49 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,59 @@ class Purgecss {
*/
purge() {
// Get selectors from content files
let cssClasses = this.extractFileSelector(this.options.content, this.options.extractors)
const {content, extractors, css} = this.options

const cssFileSelectors = this.extractFileSelector(content.filter(o => typeof o === 'string'), extractors)
const cssRawSelectors = this.extractRawSelector(content.filter(o => typeof o === 'object'), extractors)

// Get css selectors and remove unused ones
let files = []
for (let file of this.options.css) {
const cssContent = this.options.stdin ? file : fs.readFileSync(file, 'utf8')
files.push({
const sources = this.getCssContents(css, new Set([...cssFileSelectors, ...cssRawSelectors]))

return sources
}

/**
* Get the content of the css files, or return the raw content
* @param {array} cssOptions Array of css options, files and raw
* @param {Set} cssSelectors Set of all extracted css selectors
*/
getCssContents(cssOptions: Array<any>, cssSelectors: Set<string>) {
const sources = []

for (let option of cssOptions) {
let file = null
let cssContent = ''
if (typeof option === 'string') {
file = option
cssContent = this.options.stdin ? file : fs.readFileSync(file, 'utf8')
} else {
cssContent = option.raw
}

sources.push({
file,
css: this.getSelectorsCss(cssContent, cssClasses)
css: this.getSelectorsCss(cssContent, cssSelectors)
})
}
return files

return sources
}

/**
* Extract the selectors present in the passed string using a purgecss extractor
* @param {array} content Array of content
* @param {array} extractors Array of extractors
*/
extractRawSelector(content: Array<any>, extractors?: Array<ExtractorsObj>): Set<string> {
let selectors = new Set()

for (let option of content) {
const {raw, extension} = option
const extractor = this.getFileExtractor(`.${extension}`, extractors)
selectors = new Set([...selectors, ...this.extractSelectors(raw, extractor)])
}

return selectors
}

/**
Expand Down

0 comments on commit fec778a

Please sign in to comment.