-
-
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
Conversation
Thanks for the PR. This is an interesting feature. What use cases are you thinking about for this? |
__tests__/purgecssDefault.test.js
Outdated
}], | ||
css: [{ | ||
raw: '.stay {display: inline}; .remove {display: block}' | ||
}] |
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 ;
after inline
__tests__/purgecssDefault.test.js
Outdated
}) | ||
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 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
src/index.js
Outdated
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 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
src/index.js
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
use for...of
instead of forEach
src/index.js
Outdated
content.forEach(option => { | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
use destructuring to get option properties
…variables, test update
Thanks for the feedback, the for..of is new to me :) I am using this in a custom plugin for webpack, the part of it that uses purgecss just gets the chunks and assets and purges the css, then replaces the asset with the purged css, this all happens before any files are written. |
I just noticed that if you accept this merge you could remove |
- merge PR #29 by @jsnanigans: add `raw` for content option to pass a raw string instead of a filepath. - fix incorrect logic that removes attributes selectors - update dependencies - add changelog
Please add an example of this to the usage section, I just wrote all this: const temp = path.resolve(__dirname, "../temp")
fs.mkdirSync(temp)
const fd = fs.openSync(path.join(temp, "temp.css"), "w")
fs.writeSync(fd, css, 0, "utf8")
const purgecss = new PurgeCSS()
css = (await purgecss.purge({
content: config.purge,
css: [path.join(temp, "temp.css")],
}))[0].css
fs.rmdirSync(temp, { recursive: true }) Only to find this PR immediately after :P |
Proposed changes
The Idea is that I can pass HTML or whatever content directly instead of just the file path.
Types of changes
Updated functions that gets CSS content and HTML selectors.
Created extractRawSelector
Checklist
Put an
x
in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.Further comments
Im not sure if the syntax is acceptable, if you pass a array of strings in
content
andcss
it will behave like before, but if its a object instead of a array, forcontent
you passraw
with the eg. HTML andextension
to make the extractors work. Forcss
you just pass a object with the css inraw
like this: