Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
No matter which plugins you use, what's most important is the CSS output you serve the browser. In this video, you'll learn to optimize your CSS for production.
-
0:00
PostCSS plugins offer a wide variety of syntax extensions.
-
0:04
Some even provide Sass like syntax.
-
0:06
With the language extension plugins,
-
0:09
you're able to nest selectors, use variables, create mix ins and
-
0:13
extends, even import partials, just like you would with Sass.
-
0:19
In other words, PostCSS could do enough of what Sass offers that it could be your go
-
0:23
to styling method.
-
0:24
And since the plugins are written in JavaScript,
-
0:28
your CSS is processed faster than Ruby Sass and LibSass even.
-
0:33
So, if you're working with a large CSS file,
-
0:34
that speed can make a big difference in your bill times.
-
0:38
The PreCSS plugin pack combines all the plugins that use Sass like syntax and
-
0:43
features into one plugin, so you don't have to manually install each one.
-
0:49
Now, I'm not gonna cover the language extension plugins in this workshop,
-
0:52
but now that you know how to install and run a plugin, you can try them out for
-
0:56
yourself later.
-
0:58
No matter what plugins you use or which features and syntax, ultimately,
-
1:02
what's most important is the final CSS output you serve the browser.
-
1:07
PostCSS plugins also offer an easy way to optimize your CSS for production.
-
1:13
The output CSS currently displays all the comments and spaces.
-
1:18
And the CSS rules are fully expanded,
-
1:21
which can be helpful when debugging in development.
-
1:24
But it's best not to deploy your CSS to production this way, since every line,
-
1:29
space, and comment can make the file size larger than it needs to be,
-
1:33
especially if this was a big project.
-
1:36
So you can optimize your build file for production by stripping out
-
1:40
all the comments and spaces and compressing the CSS to one line.
-
1:45
This process is called minification.
-
1:47
So the plugin I'm going to install to optimize my CSS is called cssnano.
-
1:54
In the terminal, I'll install the cssnano plugin as a dev dependency
-
2:00
by typing npm install --save-dev, followed by cssnano.
-
2:10
Then I'll require cssnano in my gulp file.
-
2:22
And like the rest of our plugins, I'll add cssnano to the processor's array.
-
2:32
I'll save my gulp file, bring up the terminal, and
-
2:36
run gulp to start the watcher.
-
2:40
And now when you save your source CSS, cssnano minifies your CSS
-
2:47
to make the final CSS as small as possible for your production environment.
-
2:52
So now the entire style sheet is compressed to one line.
-
2:57
cssnano also removes empty rules and declarations.
-
3:01
It converts longhand properties into the shorthand.
-
3:05
And it shortens hex values to three digits, wherever possible.
-
3:11
cssnano will even output the smallest CSS representation of a color value.
-
3:16
For example, if you define the hex value for
-
3:21
the color red, so let's say #ff0000.
-
3:26
It will output the actual keyword value red because
-
3:32
the word red is shorter than ff0000, creating less output.
-
3:43
We've just barely scratched the surface of the PostCSS plugin ecosystem.
-
3:47
Hopefully now you're more curious and excited about its possibilities.
-
3:50
In this workshop, you learned that PostCSS is a different approach
-
3:53
to writing CSS because it's not limited to any one type of functionality.
-
3:58
You can customize and configure PostCSS to align it with the way you like to work.
-
4:03
And it can help solve problems that other tools like CSS preprocessors may
-
4:07
not be able to solve yet.
-
4:09
You can even build your own solutions to problems by building a custom plugin and
-
4:13
sharing it with the developers in the PostCSS community.
-
4:16
So be sure to check the teachers' notes for resources on plugin development and
-
4:20
the latest in PostCSS.
-
4:22
Thanks everyone.
You need to sign up for Treehouse in order to download course files.
Sign up