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 1 commit
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
17 changes: 17 additions & 0 deletions __tests__/purgecssDefault.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,21 @@ 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="stay"></span>',
extension: 'html'
}],
css: [{
raw: '.stay {display: inline}; .remove {display: block}'
}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give a valid css for the test, with ; after inline

})
const result = purgecss.purge()[0].css
expect(result.includes('remove')).toBe(false)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add assertion to test that used css are present, like .stay

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

const cssFileClasses = this.extractFileSelector(content.filter(o => typeof o === 'string'), this.options.extractors)
const cssRawClasses = this.extractRawSelector(content.filter(o => typeof o === 'object'), this.options.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(this.options.css, new Set([...cssFileClasses, ...cssRawClasses]))

return sources
}

getCssContents(cssOptions: Array<any>, cssClasses: Set<string>) {
const sources = []
cssOptions.forEach(option => {
let file = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use for...of instead of forEach, it stays consistent with extractFileSelector

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)
})
}
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()

content.forEach(option => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use for...of instead of forEach

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use destructuring to get option properties

})

return selectors
}

/**
Expand Down