angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Specify custom PostCSS plugins

Open ghost opened this issue 9 years ago • 51 comments

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [x] feature request

Versions.

@angular/cli: 1.4.9 node: 6.11.4 os: linux x64 @angular/animations: 4.4.6 @angular/common: 4.4.6 @angular/compiler: 4.4.6 @angular/core: 4.4.6 @angular/forms: 4.4.6 @angular/http: 4.4.6 @angular/platform-browser: 4.4.6 @angular/platform-browser-dynamic: 4.4.6 @angular/router: 4.4.6 @angular/cli: 1.4.9 @angular/compiler-cli: 4.4.6 @angular/language-service: 4.4.6 typescript: 2.3.4

Desired functionality.

I would like the ability to specify custom postCss plugins, without having to eject the angular webpack.config.js file.

Mention any other details that might be useful.

This would enable frameworks like TailwindCSS (https://tailwindcss.com) and other useful PostCSS plugins through the CLI without having to deal with the ejected webpack config file.

ghost avatar Nov 09 '17 20:11 ghost

Hi, just wondering if there was a workaround for using TailwindCSS via PostCSS in Angular while we wait for this item. I've googled and couldn't find any good documentation on how to. Thanks.

tridattran avatar Nov 19 '17 10:11 tridattran

Found a hackable workaround.

As there is no "official" way to provide Angular CLI a postcss-custom-properties config - yet.

Open index.js from your projects node_modules\postcss-custom-properties\dist folder Find this line: warnings: options.warnings === undefined ? true : options.warnings,

We're going to change warnings property from true to false. warnings: options.warnings === undefined ? false : options.warnings,

Save and run ng serve image

trimox avatar Nov 21 '17 14:11 trimox

Thanks @trimox!

But change files in node_modules folder is a big problem for me 😢

I am waiting the official solution...

felipefialho avatar Nov 21 '17 15:11 felipefialho

100% agreed. Hope the issue gets more attention!

trimox avatar Nov 21 '17 15:11 trimox

The typical workaround for this sort of thing is to write a NPM postinstall script in your application which uses sed or similar to monkey-patch (via regex) the relevant line of code inside node_modules. This is as awful a hack as it sounds, but I've used it several times to get past the need for a temporary feature "right now" awaiting a real solution down the road.

Overall I think a monkey patch like this is much less bad than ejecting, or rolling your own build solution, to get access to a needed setting.

kylecordes avatar Dec 20 '17 03:12 kylecordes

Vote up! This could be very useful option.

NazarkinRoman avatar Feb 06 '18 10:02 NazarkinRoman

@filipesilva could you please tell us what the "comp: cli/build" tag you just added means? any chance of this feature landing in cli anytime soon-ish? :)

thanks

tihomir-kit avatar Feb 15 '18 23:02 tihomir-kit

Just wanted to see if I could get a definite answer- does this mean if I wanted to use a third-party postcss plugin with angular-cli (eg. postcss-media-variables), I have to eject the webpack config? In other words, there's currently no native way to add additional postcss plugins to angular-cli?

Thanks for any help!

byronsanchez avatar Mar 23 '18 21:03 byronsanchez

With Angular CLI 6 no longer allowing for ng eject, this has become much more important. We are currently relying on an ejected webpack config to add Tailwind CSS, but with all the changes from Angular 5 to 6 (not to mention going from Webpack 3 to 4), it would be awesome if there was a way to specify PostCSS plugins in the config. Since we already have autoprefixer, I'd imagine we could perhaps move autoprefixer into a postCSS section of the configuration, keeping it all exposed (but preconfigured) to the user.

phroggyy avatar May 20 '18 13:05 phroggyy

Are there any updates about using postcss plugins? We are using cssnext rather than sass, and are relying on postcss-cssnext to do that. We would really like to be able to use Angular CLI - but we aren't able to do any hacking of the node_modules due to constraints of our (overly) constrained build system :/

@filipesilva - it's been almost 4 months since you were unassigned, any idea if someone else will be assigned soon, or should we just give up on CLI for now? I'd have a shot at convincing our team to hang tight if we knew it would be worked on soon-ish...but the natives are becoming restless!!

d4nc3r avatar May 22 '18 20:05 d4nc3r

any updates?

jerguslejko avatar May 30 '18 20:05 jerguslejko

I think that having postcss run the auto prefixer and allowing other plugins is an awesome idea - @phroggyy

@d4nc3r postcss-cssnext is dead, now have to use postcss-preset-env going forward.

babeal avatar Jun 05 '18 20:06 babeal

So looking at the generation of the webpack config, postcss is run for all scss|less|stylus|css right before the raw-loader with the following plugins -> postcss-url, postcss-import, and autoprefixer, plus another cli specific plugin. I really think most of us are wanting to run plugins right before the autoprefixer, but if a plugin like cssinject needs to run before the url plugin then the ordering would need to be managed. The plugin removeRoot is another one that requires ordering. It also looks like plugin configs need to be substantially modified to be usable in this environment. In addition, plugins like postcss-preset-env, which handles cssnext behavior, also configures autoprefixer driven by browserslist which could conflict with the execution of the autoprefixer in the cli.

https://github.com/angular/devkit/blob/master/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts

babeal avatar Jun 05 '18 22:06 babeal

I guess one alternative is to fork https://github.com/angular/devkit and add the webpack config lines directly in to that file and just try to keep them up to date... but a supported option would be nice

babeal avatar Jun 05 '18 22:06 babeal

We patch https://github.com/angular/devkit/blob/master/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts without ejection using patch-package.

hunhavoc avatar Jul 09 '18 09:07 hunhavoc

hi everyone, are there any promising developments on this ?

ahnpnl avatar Aug 22 '18 06:08 ahnpnl

Devkit has been moved to the angular-cli project, new link to styles config is this: https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts

hLittle avatar Oct 05 '18 14:10 hLittle

Hey guys! What about custom builders for Angular 6+? I haven't tried, however package author says it is good alternative for deprecated ng eject and it allows to extend webpack config (without overriding built-in config). See for details: https://github.com/meltedspark/angular-builders/tree/master/packages/custom-webpack

IStranger avatar Nov 09 '18 06:11 IStranger

Isnt this the most requested feature since Angular 2?

Personally i need to add flexbugs to PostCSS.

MickL avatar Nov 19 '18 14:11 MickL

Also looking for this feature to add the postcss-momentum-scrolling plugin. I have tried using the custom builders for Angular 7, but haven't had success adding getting it to add a plugin. Instead it seems to replace the entire ruleset for /\.scss$|\.sass$/.

Started a question here to see if anyone knows how and can point me in the right direction. https://stackoverflow.com/questions/53955119/how-do-i-add-an-additional-postcss-plugin-via-the-new-angular-cli-v7-angular-js

michaelcm avatar Jan 02 '19 21:01 michaelcm