Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass content and css as raw data #29

Merged
merged 3 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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