Skip to content

Getting postcss to process imported css #35

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

Closed
fenomas opened this issue Nov 21, 2015 · 8 comments
Closed

Getting postcss to process imported css #35

fenomas opened this issue Nov 21, 2015 · 8 comments

Comments

@fenomas
Copy link

fenomas commented Nov 21, 2015

Hi,

I'm trying to run autoprefixer on CSS files that use @import. Using basic configs, I find that my root CSS file (that gets required through webpack) gets processed, but styles that are imported from CSS get added to the project but not processed by postcss.

Is this supposed to work out of the box, or am I missing something obvious?

My webpack config is straight from the docs:

    module: {
        loaders: [
            { test: /\.css$/, loader: 'style-loader!css-loader!postcss-loader', },
        ]
    },
    postcss: [
        autoprefixer({
            browsers: ['last 2 versions']
        })
    ],

I run that with a root foo.css that imports bar.css, and the results is that the page gets two style tags, one for each CSS file, and the imported one does not get processed:

    <style type="text/css">
        .bar {
            display: flex;
        }
    </style>
    <style type="text/css">
        .foo {
            display: -webkit-flex;
            display: -ms-flexbox;
            display: flex;
        }
    </style>

Thanks for any help!

@ai
Copy link
Contributor

ai commented Nov 21, 2015

Use postcss-import to import files inside PostCSS and process it with Autoprefixer after import.

@ai ai closed this as completed Nov 21, 2015
@fenomas
Copy link
Author

fenomas commented Nov 21, 2015

@ai I considered that, but in #8 you say using postcss-import is a bad way to use this module. Is that no longer the case?

@ai
Copy link
Contributor

ai commented Nov 21, 2015

@AndyHall the best way is to use one dependency tree for components. Require one component to other, not import one CSS file to other CSS.

So, for example, you have button component. And you will have separated dir with all things for this component:

button/
  index.js
  button.css
  button.js
  image.svg

In index.js you load all stuff (there are few webpack plugins, which will create it automatically):

require('./button.js');
require('./button.css');

So, if you need to use button inside a form, you write in form/form.js:

require('../button');

@fenomas
Copy link
Author

fenomas commented Nov 21, 2015

@ai That's basically what I'm doing, it's just that the css is quite large and separated into modular pieces.

With that said, I just found that passing importLoaders=1 to css-loader seems to resolve my problem, if I chain from autoprefixer instead of postcss. That is:

    {
        test: /\.css$/, 
        loader: 'style-loader!css-loader?importLoaders=1!autoprefixer-loader',
    }

I'm brand new to webpack and I don't really understand the difference between this and using postcss, but for the moment it seems to avoid the problem..?

@ai
Copy link
Contributor

ai commented Nov 21, 2015

Yeap, importLoaders=1 looks like a solution.

BTW, autoprefixer-loader is not a official way to load Autoprefixer :). You will have delay for updates.

@fenomas
Copy link
Author

fenomas commented Nov 21, 2015

Hmm, now that I check, adding importLoaders=1 works with postcss too. So I can do that if it's better.

By not official, do you mean autoprefixer-loader is unmaintained or incorrect or something? I'm just looking for whatever is the standard, default way to autoprefix CSS files that might contain imports.

@ai
Copy link
Contributor

ai commented Nov 21, 2015

Nope, autoprefixer-loader is OK. But it just unnecessary extra wrap for my opinion. So, when new major Autoprefixer will be release, autoprefixer-loader will require a week or more for update.

@fenomas
Copy link
Author

fenomas commented Nov 21, 2015

Ah, I get you. Okay, thanks for your help!

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

No branches or pull requests

2 participants