-
-
Notifications
You must be signed in to change notification settings - Fork 248
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}' | ||
}] | ||
}) | ||
const result = purgecss.purge()[0].css | ||
expect(result.includes('remove')).toBe(false) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add assertion to test that used css are present, like |
||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
const content = option.raw | ||
const extractor = this.getFileExtractor(`.${option.extension}`, extractors) | ||
selectors = new Set([...selectors, ...this.extractSelectors(content, extractor)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use destructuring to get option properties |
||
}) | ||
|
||
return selectors | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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
;
afterinline