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

Pass content and css as raw data #29

merged 3 commits into from
Nov 26, 2017

Conversation

jsnanigans
Copy link
Collaborator

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

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

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.

  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Im not sure if the syntax is acceptable, if you pass a array of strings in content and css it will behave like before, but if its a object instead of a array, for content you pass raw with the eg. HTML and extension to make the extractors work. For css you just pass a object with the css in raw

like this:

const purgecss = new Purgecss({
  content: [{
    raw: '<span class="stay"></span>',
    extension: 'html'
  }],
  css: [{
    raw: '.stay {display: inline}; .remove {display: block}'
    }]
})

@Ffloriel Ffloriel self-requested a review November 25, 2017 10:44
@Ffloriel
Copy link
Member

Thanks for the PR. This is an interesting feature.
I was thinking of a way to use purgecss to remove unused css with individual vue component. Being able to select the raw content + the extensions could help with this task.

What use cases are you thinking about for this?

}],
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

src/index.js Outdated
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

src/index.js Outdated
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

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)])
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

@jsnanigans
Copy link
Collaborator Author

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.

@jsnanigans
Copy link
Collaborator Author

I just noticed that if you accept this merge you could remove options.stdin since it does almost the same thing, but with this PR it would be possible to mix files and standard input for content and css

@Ffloriel Ffloriel merged commit fec778a into FullHuman:master Nov 26, 2017
Ffloriel added a commit that referenced this pull request Dec 7, 2017
- 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
Ffloriel added a commit that referenced this pull request Oct 30, 2019
@giraffekey
Copy link

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
Would be nice if the information were more accessible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants