Skip to content
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

vue cli 3 does not load postcss.config.js or .postcssrc.js #852

Closed
twocngdagz opened this issue Feb 17, 2018 · 22 comments
Closed

vue cli 3 does not load postcss.config.js or .postcssrc.js #852

twocngdagz opened this issue Feb 17, 2018 · 22 comments

Comments

@twocngdagz
Copy link

Version

3.0.0-alpha.13

Reproduction link

https://github.com/twocngdagz/vue-tailwind-select

Steps to reproduce

I added a postcss.config.js or postcssrc.js for postcss to load tailwindcss

module.exports = {
plugins: [
require("postcss-import")(),
require("tailwindcss")("./tailwind-config.js"),
require("autoprefixer")(),
]
};

What is expected?

it should process the

@tailwind preflight;

@tailwind utilities;

What is actually happening?

when running vue build src/main.js

style output
#app{font-family:Avenir,Helvetica,Arial,sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-align:center;color:#2c3e50;margin-top:60px}h3[data-v-2ebf6f26]{margin:40px 0 0}ul[data-v-2ebf6f26]{list-style-type:none;padding:0}li[data-v-2ebf6f26]{display:inline-block;margin:0 10px}a[data-v-2ebf6f26]{color:#42b983}@tailwind preflight;@tailwind utilities;

@yyx990803
Copy link
Member

You are placing the directive in the wrong place: https://github.com/twocngdagz/vue-tailwind-select/blob/master/src/App.vue#L23

It should be inside <style> blocks.

@Christoph-Harms
Copy link

Christoph-Harms commented May 9, 2018

To anyone having the same issue despite having placed the directives in the correct place: vue-cli 3 does not pick up on the .postcssrc.js or the postcss.config.js, since it uses the package.json to configure postCSS.

This article helped me solve the problem.

@davidohlin
Copy link

@Christoph-Harms I can't get it to work, following the article. Do you happen to have a working example?

@adrian-fjellberg
Copy link

If anyone else has this problem you might be interested in my solution to the problem.

The postcss.config.js was not loading at all (tried putting error code in there, but no failures/warnings). What was happening was that Webpack was never really loading the config file because I was not using any .postcss files in my project, only .scss files. I was expecting the postcss-loader to be used as part of the .scss rule in the Webpack-config, but it is not set up this way by default in Webpack 2 and 3.

The solution was then to add the postcss-loader to the .scss rule in the Webpack config files. As the loaders are generated by a function called generateLoaders in the utils.js file, I had to change some of that generating-code to make it work. After making the changes I made sure that when generating the scss loaders I added in the postcss-loader.

This is how my build/utils.js file looks like:
https://gist.github.com/adrian-fjellberg/5e2243cf6b99cf47b02051446dd16e6e

@Akryum
Copy link
Member

Akryum commented Jul 26, 2018

@adrian-fjellberg Postcss is supported out-of-the-box in vue-cli 3.

@adrian-fjellberg
Copy link

@Akryum Aha. Of course, it's not correct of me to say:

it is not set up this way by default in Webpack 2 and 3

What I should say is that my problem was related to back when vue-cli used a build directory to configure Webpack. I don't know when vue-cli stopped configuring Webpack that way and added postcss-loader to the .scss rule by default. Sorry for the confusion, I was up all night to get this to work on an older application and wanted to share my solution when I finally got it working.

@Akryum
Copy link
Member

Akryum commented Jul 26, 2018

I don't know when vue-cli stopped configuring Webpack that way and added postcss-loader to the .scss rule by default.

Since version 3.0

@LinusBorg
Copy link
Member

At the core, this has more to do with vue-loader than vue-cli. Since vue-loader 15, postcss is not applied automatically to all <style> tags anymore, since vue-loader now just uses the other webpack rules defined for *.css files, *.scss files etc.

@imcvampire
Copy link

Hi @LinusBorg, can you provide a small example to apply postcss to all style tags?

@LinusBorg
Copy link
Member

Oh your misunderstanding.

Vue-cli 3 is configured to apply postcss to all style tags. I was me rely stating that this is now done explicitly by adding postcss to the rule that is applied to the styles, not implicitly by vue-loader internally.

@lovetingyuan
Copy link

postcss-loader does not load any config file like postcss.config.js or .postcssrc when it's options has some useful fields like plugins.

@equinusocio
Copy link

equinusocio commented Dec 19, 2018

I can't make postcss work with a vue-cli 3 generated project. I just followed the wizard and chosed to use separated configuration files. After configured the postcss.config.js file it should works. Intead postcss plugins aren't loaded inside my vue single file components using <style lang="postcss" scoped>

@LinusBorg
Copy link
Member

This issue is closed. Open a new one with a reproduction

@kid1412621
Copy link

To anyone having the same issue despite having placed the directives in the correct place: vue-cli 3 does not pick up on the .postcssrc.js or the postcss.config.js, since it uses the package.json to configure postCSS.

This article helped me solve the problem.

aw, why vue cli3 suggests using babel.config.js instead of package.json#babel to config babel.
Why not portcss? Gee... front-end sucks

@lovetingyuan
Copy link

#2985

@matzeso
Copy link

matzeso commented Sep 8, 2020

Came here with the same issue in vue-cli 4.5.4, what worked for me was configuring the postcss-loader via vue.config.js.

Create vue.config.js in root directory (next to package.json) and fill it with:

const tailwindcss = require('tailwindcss'); 
const autoprefixer = require('autoprefixer');


module.exports = {
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    tailwindcss,
                    autoprefixer,
                ],
            },
        },
    },
}

Hope it helps somebody :-)

@JamieGoodson
Copy link

JamieGoodson commented Mar 7, 2021

Despite trying various of the above answers and lots of googling (webpack configs, postcss configs, adding things to my package.json, vue configs), I still don't seem to be generating autoprefixed CSS from my <style lang="scss"> tags.

It's very confusing as to the correct place we need to tell Vue to use PostCSS and autoprefix sass tags in .vue files (which I assume is extremely common).

Have created a vue.config.js file like so (as suggested by @matzeso):

const autoprefixer = require('autoprefixer');

module.exports = {
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    autoprefixer,
                ],
            },
        },
    },
}

Output .css doesn't seem to have any prefixes applied. No errors in console. I know the vue.config.js is being read because if I throw random code in there it will error during the build.

@equinusocio
Copy link

@jamiegdsn They don't care about bugs.

@matzeso
Copy link

matzeso commented Mar 9, 2021

This is not the place to report bugs or expect answers, as this is a closed ticket @equinusocio
If you want to help a project move forward, help creating good tickets and stir people in the right direction.
Nobody is motivated or helped by just complaining.

@jamiegdsn I would advice you to create a new ticket, create a minimum reproduction of the issue on e.g. codesandbox and share it a link to that in your newly created ticket.

Good luck!

@ricky11
Copy link

ricky11 commented Apr 17, 2021

Isn't autoprefixer enabled by default already in vue cli 3?

So finnaly what are the ways to add a postCss module or 'postcss-preset-env' in vue?

  1. package.json

"postcss": {
"plugins": {
"postcss-preset-env": {
"importFrom" : "['./src/assets/styles.css']"
}
}
},

  1. vue.config.js
    css: {
    loaderOptions: {
    plugins: [
    [
    "postcss-preset-env", {}

    ],
    ],
    },
    },

i believe both of the above should work in a standard vue 2.x project, although the above still does not work for me, when i have a global style.css file imported in to main.js I sincerely wish the Vue Cookbook or docs would explain this better for newbs, infact most experienced devs still get confused around post css and vue.

@SteveTj88
Copy link

Isn't autoprefixer enabled by default already in vue cli 3?

So finnaly what are the ways to add a postCss module or 'postcss-preset-env' in vue?

  1. package.json

"postcss": { "plugins": { "postcss-preset-env": { "importFrom" : "['./src/assets/styles.css']" } } },

  1. vue.config.js
    css: {
    loaderOptions: {
    plugins: [
    [
    "postcss-preset-env", {}
    ],
    ],
    },
    },

i believe both of the above should work in a standard vue 2.x project, although the above still does not work for me, when i have a global style.css file imported in to main.js I sincerely wish the Vue Cookbook or docs would explain this better for newbs, infact most experienced devs still get confused around post css and vue.

Did you find the solution to this in the end?

@ricky11
Copy link

ricky11 commented Feb 16, 2022

You can add it in vue.config.js

autoprefixer is enabled by default, but i am unsure if you edit the css object in vue.config.js if any setting will be overwritten.

So all plugin options should be inserted in css.loadOptions.plugins[]

At this moment i have disabled purgecss and postcss presets.

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