-
Notifications
You must be signed in to change notification settings - Fork 188
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
Basic "plugin" options #316
Conversation
When usign the api, it is sometime necessary to act on the ast object between csso operations. Instead of having to deal with options and calls to parse and compress, this change makes it easy to edit the ast object after csso's parse (afterParse) and compress (afterCompress) operation. This is especially good for task runner, which can accept those parameters to be able to speed task execution.
1 similar comment
Thank you for your contribution! |
@@ -60,6 +60,18 @@ function buildCompressOptions(options) { | |||
return options; | |||
} | |||
|
|||
function runPlugins(ast, options, plugin) { | |||
var plugins = []; | |||
if (typeof options[plugin] === 'object' && options[plugin].length) { |
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.
if (Array.isArray(options[plugin])) {
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.
Oh. Yeah, old habits are tough to lose..
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.
Fixed in 69256e2
// after parse plugins | ||
if (options.afterParse) { | ||
debugOutput('afterParse', options, Date.now(), | ||
runPlugins(ast, options, 'afterParse') |
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.
Better to pass options.afterParse
instead of property name.
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.
Hum indeed we could. I'll rewrite it.
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.
Fixed in 1174a7e
@@ -75,11 +87,25 @@ function minify(context, source, options) { | |||
}) | |||
); | |||
|
|||
// after parse plugins | |||
if (options.afterParse) { |
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.
I think beforeCompress
is much better name
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.
Agreed.
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.
Fixed in 87f1413
Understood. Maybe we could add a warning in the docs ? Is there a way to validate the AST after each plugin call ?
Which other case do you have in mind ? |
1 similar comment
I've thought about it and I can't see how to simple if clause can slow things down. Adding plugins into a system always has a tradeoff, but current usage would not notice it. But I am available to talk about other options, like doing this change directly in grunt-csso, where it should only stop using minify and issue manual parse and compress operation. Thanks for this amazing little piece of code btw. ❤️ the List object. |
And I see this feature primarily used by runners... anything more complex would directly call parse and compress. |
I thought about it. But I checked source and looks like adding
Well some things may needs to be done after
Not directly but via adding more checking before or during step performing.
It was desinged to avoid using arrays and better performance (see details). Parser and related things including |
This is great thanks! Any ETA about publishing a new version on npm ? |
I'm not sure this changes enough for release. I'll see what we can add and probably publish new version this week. |
Ok. Let's wait until I've tested a complete integration then. Working on the |
CSSO now supports callbacks to be able to poke with the AST before and afer compression. This allows developers to add more optimization on the AST before the file is written while avoiding to parse the css from disk. See css/csso#316 for details
Keep in mind AST format will change soon. When format will be stabilized in |
Perfect, thanks. Should I send a new PR on the csstree branch ? |
Nope, I've already merge master to this branch. |
👍 |
Well, I would finally need a release in order to be able to ship a passing test suite on the grunt plugin. If you could do it when you have a chance, that would be appreciated. Thanks :) |
Sorry for the wait. Published as |
No problems. Thanks! |
When usign the api, it is sometime necessary to act on the ast object
between csso operations. Instead of having to deal with options and calls to
parse and compress, this change makes it easy to edit the ast object
after csso's parse (afterParse) and compress (afterCompress) operation.
This is especially good for task runner, which can accept those
parameters to be able to speed task execution.
The new for a plugin came in view when I was copy/pasting code from csso or grunt-csso into my grunt task.
This is the experiment I've made: https://gist.github.com/nitriques/9d3b05c530a54c3a885c6bf41b46dbae
It's a re-implementation of purifycss but using async streams and csso instead of sync reads and rework. As per my tests (I've tested it on 4 productions sites) it's 45 times faster. Purify css yields saving of ~5%, while my grunt task saves more that 45% using the same config. Those are big wins.
I can live with the current experiment I've made, but feel like it would live better as a csso plugin, hence the PR.
If you want, I can create the release for you. All tests (both old and new) are passing. I would also happy to send a PR to the grunt-csso project too.