Giter Site home page Giter Site logo

sedubois / cssbundling-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rails/cssbundling-rails

0.0 0.0 0.0 81 KB

Bundle and process CSS in Rails with Tailwind, PostCSS, and Sass via Node.js.

License: MIT License

Shell 4.95% JavaScript 2.37% Ruby 82.06% CSS 0.78% SCSS 9.84%

cssbundling-rails's Introduction

CSS Bundling for Rails

Use Tailwind CSS, Bootstrap, Bulma, PostCSS, or Dart Sass to bundle and process your CSS, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to use app/assets/builds to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to .gitignore by default).

You develop using this approach by running the bundler in watch mode in a terminal with yarn build:css --watch (and your Rails server in another, if you're not using something like puma-dev). You can also use ./bin/dev, which will start both the Rails server and the CSS build watcher (along with a JS build watcher, if you're also using jsbundling-rails).

Whenever the bundler detects changes to any of the stylesheet files in your project, it'll bundle app/assets/stylesheets/application.[bundler].css into app/assets/builds/application.css. This build output takes over from the regular asset pipeline default file. So you continue to refer to the build output in your layout using the standard asset pipeline approach with <%= stylesheet_link_tag "application" %>.

When you deploy your application to production, the css:build task attaches to the assets:precompile task to ensure that all your package dependencies from package.json have been installed via yarn, and then runs yarn build:css to process your stylesheet entrypoint, as it would in development. This output is then picked up by the asset pipeline, digested, and copied into public/assets, as any other asset pipeline file.

This also happens in testing where the bundler attaches to the test:prepare task to ensure the stylesheets have been bundled before testing commences. If your test framework does not call the test:prepare Rake task, ensure that your test framework runs css:build to bundle stylesheets before testing commences. If your setup uses jsbundling-rails (ie, esbuild + tailwind), you will also need to run javascript:build.

That's it!

You can configure your bundler options in the build:css script in package.json or via the installer-generated tailwind.config.js for Tailwind or postcss.config.js for PostCSS.

Installation

You must already have node and yarn installed on your system. You will also need npx version 7.1.0 or later. Then:

  1. Run ./bin/bundle add cssbundling-rails
  2. Run ./bin/rails css:install:[tailwind|bootstrap|bulma|postcss|sass]

Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with rails new myapp --css [tailwind|bootstrap|bulma|postcss|sass].

FAQ

How does this compare to tailwindcss-rails and dartsass-rails?

If you're already relying on Node to process your JavaScript, this gem is the way to go. But if you're using the default import map setup in Rails 7+, you can avoid having to deal with Node at all by using the standalone versions of Tailwind CSS and Dart Sass that are available through tailwindcss-rails and dartsass-rails. It's simpler, fewer moving parts, and still has all the functionality.

How do I import relative CSS files with Tailwind?

If you want to use @import statements as part of your Tailwind application.js file, you need to configure Tailwind to use postcss and then postcss-import. But you should also consider simply referring to your other CSS files directly, instead of bundling them all into one big file. It's better for caching, and it's simpler to setup. You can refer to other CSS files by expanding the stylesheet_link_tag in application.html.erb like so: <%= stylesheet_link_tag "application", "other", "styles", "data-turbo-track": "reload" %>.

How do I avoid SassC::SyntaxError exceptions on existing projects?

Some CSS packages use new CSS features that are not supported by the default SassC rails integration that previous versions of Rails used. If you hit such an incompatibility, it'll likely be in the form of a SassC::SyntaxError when running assets:precompile. The fix is to bundle remove sass-rails (or sassc-rails, if you were using that).

Why do I get application.css not in asset pipeline in production?

A common issue is that your repository does not contain the output directory used by the build commands. You must have app/assets/builds available. Add the directory with a .gitkeep file, and you'll ensure it's available in production.

Why isn't Rails using my updated css files?

Watch out - if you precompile your files locally, those will be served over the dynamically created ones you expect. The solution:

rails assets:clobber 

License

CSS Bundling for Rails is released under the MIT License.

cssbundling-rails's People

Contributors

abevoelker avatar acallaghan avatar adamlogic avatar ajaynomics avatar benkoshy avatar davidcolby avatar dhh avatar domchristie avatar dorianmariecom avatar elalemanyo avatar elia avatar ghiculescu avatar glaucocustodio avatar guilleiguaran avatar hlfh avatar intrepidd avatar javierav avatar kirillplatonov avatar mibradev avatar mrjoy avatar navidemad avatar nickjj avatar phawk avatar pinzonjulian avatar printfinn avatar rafaelfranca avatar richardtowers avatar thebravoman avatar thomasvanholder avatar wakematta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.