Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create a PostCSS Plugin to Remove Vendor Prefixes

    John OtanderJohn Otander
    javascriptJavaScript

    PostCSS is a CSS processor that allows you to modify source CSS with plugins. In this lesson we'll modify the CSS AST by creating a plugin that removes declarations when they contain vendor prefixes.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: In order to write a postCSS plugin, we'll need to initialize a project, then we'll need to install the development dependencies. We install postCSS so we can run our plug in, and Jest so we can run our test.

    When the installation is complete, we can add a test script to our package JSON. We can begin our test file.

    First, we'll instantiate our Fixture CSS, then we can require postCSS, and begin scaffolding out our test.

    You can run postcss and pass it our FIXTURE. Then we can create our assertion. We'll also want to instantiate our expected CSS output. This will be the same as our FIXTURE but without any vendor prefix declarations.

    Now we can run our test. As we can see it fails, because we haven't created our plugin yet. Let's go ahead and do that.

    First, we will require a PostCSS, and then call its plugin method. Give it our plugin's name and then our function.

    Function itself will turn a function that receives the tree. With our tree, we will walk all CSS declarations. For now, we can go ahead and log them. Then in our tests, make sure we use our plugin.

    We will require the plugin and then pass it as part of an array to PostCSS. Now, run our test. As we can see, we have our declarations. Here's a prop and its value, another property, its value.

    Now, in this plugin, we will remove all vendor prefixes. We can install a library to check for us. We will yarn add is-vendor-prefixed.

    We can require is-vendor-prefixed. Then we can check to see the declaration's property is vendor-prefixed.

    When it is, we can remove the declaration. Now, we can run our test.

    As we can see, some of the properties have been removed, but we also need to check the declaration's value. Here, we can copy over our initial prop check and swap out declaration.prop for the CSS declaration's value.

    With that complete, we can run our test again. It should pass.

    Altogether, we have required PostCSS, called the PostCSS plugin, walked all declarations in our route AST structure for the CSS document, and then required is-vendor-prefixed and checked both the declaration property and the declaration value to see if either were vendor prefixed. If they were, we call declaration.remove.

    In our test, we require postCSS and the plugin, and then call postCSS or we pass it the plugin and our FIXTURE, to make sure that we get the expected outcome.