Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing CSS with webpacker in Rails 6

I use webpacker with Rails and am installing taildwindcss right now. Their installation guide says to use an @import method if I'm using postcss-import. I must say I get confused whenever I have to import CSS to Rails with webpacker, so I have a few questions:

1) The @import method from the docs - is it a JavaScript or CSS import method?

2) If it's a CSS method, why do I have to paste it inside javascript folder (e.g. javascript/stylesheets? I tried to put it inside the application.css file and it doesn't work. I assume it is somehow related to the fact that it's using PostCSS and package was installed via yarn?

3) If the above is true, does it mean that I have to import every CSS package that way if it's installed via yarn?

like image 301
sloneorzeszki Avatar asked Sep 01 '19 19:09

sloneorzeszki


People also ask

How do I add a Webpacker in Rails 6?

To include Webpacker in a new project, add --webpack to the rails new command. To add Webpacker to an existing project, add the webpacker gem to the project's Gemfile , run bundle install , and then run bin/rails webpacker:install . The installation also calls the yarn package manager, creates a package.

Can you use @import in CSS?

Definition and UsageThe @import rule allows you to import a style sheet into another style sheet. The @import rule must be at the top of the document (but after any @charset declaration). The @import rule also supports media queries, so you can allow the import to be media-dependent.

How do I import a CSS class?

Note: There are two different ways to import a CSS file into another using @import url(“style2. css”); or @import “style2. css”; or directly import any CSS file or multiple CSS file in the HTML file directly within <style>@import “style1.


1 Answers

You will likely want to actually want to import into the CSS and javascript!

The typically setup will have app/javascript/styles/application.css for example which will bootstrap your global css:

@import "tailwindcss/base";

@import "tailwindcss/components";
@import "components/buttons"; // custom components that map to path app/javascript/styles/components/buttons.css for example

@import "tailwindcss/utilities";

In your app/javascript/packs/application.js you will import this:

// other js

import('styles/application.css');

// other js

In your layout you will add <%= stylesheet_pack_tag 'application' %> to add the css from application.js and <%= javascript_pack_tag 'application' %> to add the javascript from application.js.

The reason for this setup is that webpack is going to process application.js and it will handle the CSS and JS separately. Think of javascript/pack/application.js more of a bootstrap/dependencies file than a running javascript file. It's saying here's a list of stuff I need to work. In this case, one of the things is app/javascript/styles/xyz.css, and by the way, use post-css to manage how it is processed.

like image 137
Jonathan Bennett Avatar answered Oct 06 '22 20:10

Jonathan Bennett