How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers for imported sources. However, I find myself feeling a bit overwhelmed by how this integrates with other configurations set for the main PostCSS runner.

For example, if my goal is to inline URLs, should I be including the postcss-url plugin in postcss-import, in the PostCSS runner configuration, or in both places (especially if my main stylesheet contains URL references)?

Answer №1

It is advisable to prioritize postcss-import as the first plugin when configuring postcss plugins in webpack. By doing so, postcss-import will inline the @import statements at the beginning of the file before any other postcss plugins are applied.

For instance:

(For this example, let's assume you are using a postcss.config.js file. The same principle applies if you use an array for the plugins in webpack 1 format)

// Header.css
@import 'button.css';

.foo {
    font-size: 3rem;
    transform: translateY(-10px);
}

// Button.css

.bar {
   transform: translateX(20px);
}

If the import plugin follows autoprefixer in the list of plugins, autoprefixer will apply prefixes to the file first, and then the @import file will be imported. As a result:

// postcss.config.js
module.exports = {
  plugins: {
    'autoprefixer': {},
    'postcss-import': {}
  },
};

// output.css
.bar {
    transform: translateX(20px); // Prefixes not applied to the imported file
}
.foo {
    font-size: 3rem;
    transform:translateY(-10px);
    -webkit-transform: translateY(-10px); // Prefixes applied only to original file
}

However, if you place the import plugin first, it will include the imported file and then perform autoprefixing, resulting in both files being autoprefixed:

// postcss.config.js
    module.exports = {
      plugins: {
        'postcss-import': {},
        'autoprefixer': {}
      },
    };

// output.css
.bar {
    transform: translateX(20px);
    -webkit-transform: translateX(20px); // Both files now prefixed

}
.foo {
    font-size: 3rem;
    transform: translateY(-10px);
    -webkit-transform: translateY(-10px); 
}

This means there is no need to re-add plugins within the options of the postcss-import plugin.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Button that floats on the left or right side within a div container

Is there a way to ensure that a button floats only within a specific div area? Below is the CSS and HTML example for reference. .test { width: 60%; display: inline; overflow: auto; white-space: nowrap; margin: 0px auto; } <div class=' ...

Is there a way to have a span update its color upon clicking a link?

I have 4 Spans and I'm looking to create an interactive feature where clicking a link in a span changes the color of that span. Additionally, when another link in a different span is clicked, the color of that span changes while reverting the previous ...

Is there a way to adjust the scrolling speed of a background image to be slower than the rest of

I'm searching for an example similar to this: Are there any efficient javascript solutions available? I've had difficulty implementing the code I've come across so far. ...

How can I evenly distribute three list items on a PhoneGap webpage using CSS?

My goal is to create a PhoneGap app with a main menu featuring a title and three buttons. Here is the layout: <body> <div class="app"> <div class="headerOne"> <h1></h1> </d ...

The issue with ng-bind-html causing the disappearance of paragraph spaces

Is there a way to preserve paragraph breaks when using ng-bind-html to display text on the screen? I am having an issue where all the text appears as one big chunk if there are paragraph breaks in the data pulled from the database. Not sure what is causing ...

Personalized dropdown appearance

During my work on a select box, I discovered that custom styling is not possible. I attempted to use some plugins but did not find one that met my needs. I am seeking a dropdown menu with options in both black and gray, similar to this template but using ...

The flexbox container is not displaying properly

I have rows containing two flex divs each. One div displays an image, while the other has text. I'm puzzled as to why some divs display correctly while others have overflowing text. Refreshing the page resolves this issue, but it resurfaces if I clea ...

OpenTok Angular 6 encountered an error with code TS2314 stating that the generic type 'Promise<T>' needs to have 1 type argument specified

Issue in opentok.d.ts File: Error TS2314 npm version: 6.2.0 node: v8.10.0 Angular CLI: 6.2.3 Operating System: Linux x64 Angular Version: 7.0.0-beta.5 @opentok/client": "^2.14.8 ...

Tips for creating a CSS selector for a button

<div class="test" data-credit-card-index="1"> <button class="test1 test">Delete</button> <button class="test1 test">Edit</button> I'm trying to create a CSS locator specifically for the Edit button within the [d ...

When you hover over nested HTML-lists in a webpage, make the tree's long text lines expand gracefully using a combination of HTML, CSS

In my Angular 4 application, I have a left div that displays a tree of items as recursive HTML lists. When there is a long text, it gets cut off by the border of the div and a scrollbar appears. I want to have the text expand beyond the border and show in ...

Struggling to access the height attribute from a CSS file

Hey there. I'm pretty confident that the solution to my query is quite simple. So, I have a .css file with this particular code snippet: div.site { text-align:center; border:2px solid #4b6c9e; padding:1px; margin-top:10px; font-size:medi ...

Eliminating the appearance of horizontal scroll bar by employing transform scale to zoom out

When you run the code provided in the link below and scale down the HTML to 50% while increasing the width to fit the window, a scrollbar becomes visible. This only occurs in Chrome and not in Firefox. Click here The content of the DOM can be modified and ...

Is there a way to arrange the navigation items to appear on the right side upon toggler click?

I've been trying to get the nav-items to align on the right side, but so far using the ms-auto class from Bootstrap isn't giving me the desired result. <html lang="en"> <head> <!-- Required meta tags --> < ...

Is it possible to apply two ngClass directives to a single div element in Angular 4?

When I click to start editing, a component is called for the editing process. In this component, I am unable to click on anything and the background turns black, which is okay. However, I would like for each ID that I select to edit to become active with a ...

Top method for converting scrolling into an element

When trying to create a scrollable element, I attempted the following: .scroll-content { overflow: hidden; } .scrollabe { overflow-y: scroll; } <ion-content> <ion-grid style="height:100%" *ngIf="plat && ticket"& ...

Styling Angular Material Forms: Customizing Input Backgrounds and Button Heights

I'm currently working on a simple email input form with a submit button. Here are the adjustments I want to make: Set the background of the email input to white Ensure that the submit button matches the height of the input field However, my attempts ...

Ways to modify the cursor of a disabled ImageButton on an ASP.Net webpage

Within a ListView, I have dynamically generated ImageButtons. If an image does not have a link, I want to make it disabled. In the ItemDataBound event, I attempted the following approach: img.Enabled = False img.DescriptionUrl = "javascript:void(0);" img. ...

Creating a multi-column layout with HTML and CSS

I'm currently exploring the most effective method to design this concept: https://i.stack.imgur.com/SOQp4.png The black square symbolizes small icons, accompanied by a heading and paragraph for each specific section. The icons need to align with the ...

Maintain the previous droppable positioning after refreshing the page

I am encountering an issue with the .droppable event. I have set up two sections where I can move elements, but every time the page is refreshed, the positioning of the elements reverts to the initial position. How can I maintain the last positioning of th ...

The application of document.body.style.backgroundColor is not compatible with an external CSS style sheet

Why does document.body.style.backgroundColor not work with an external CSS style sheet? I have the following CSS: body { background-color: red; } In my css style sheet, when I use JavaScript alert alert(document.body.style.backgroundColor);, it sh ...