discuss icon indicating copy to clipboard operation
discuss copied to clipboard

Example app on how to get things running in Rails

Open jvanbaarsen opened this issue 7 years ago • 38 comments

I'm not much of a JS hero, so all the build tools: Webpack, Postcss etc feel like one big magic show to me. Is there anyone around here that has more experience with these tools that could create an example app with the bare configuration that is required for Ruby on Rails?

In return I'll make sure that the steps to set it up are added to the docs :-)

jvanbaarsen avatar Nov 01 '17 19:11 jvanbaarsen

Laravel Mix makes webpack a no-brainer. It is not coupled to PHP or Laravel at all, you can use it in any project.

Repo: https://github.com/JeffreyWay/laravel-mix

Installation: https://github.com/JeffreyWay/laravel-mix/blob/master/docs/installation.md

The only exception to it not being coupled to Laravel this is the versioned paths found in mix-manifest.json. Laravel provides a helper, {{ mix('/css/app.css') }}, to resolve /css/app.css to /css/app.css?id=1234567890.

You may need to write your own helper function to do a lookup in the mix mix-manifest.json. This is Laravel's implementation: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/helpers.php#L537

adrianb93 avatar Nov 03 '17 00:11 adrianb93

@jvanbaarsen depending on which version of Rails (4.2+) you're using, the Webpacker gem makes this pretty straightforward.

Please note: I haven't used this in production, and I only fired up a Rails app to start playing with Tailwind. Also, I'm also not much of a JS hero either. 😎

This is a very basic process I followed and was able to get up and running pretty quick. Someone may have a more efficient way to do this. If I had more time today, I'd work on making this part of the Webpack build process.

  1. Add Webpacker to your Gemfile.

gem 'webpacker', '~> 3.0'

  1. Install the Gem

bundle install

  1. Install (and configure) Webpacker

bundle exec rake webpacker:install

Webpacker will add the required files for Webpack, as well as create the app/javascript directory.

  1. Create a tailwind.js file. I used:

(edit) ALSO NEED TO INSTALL TAILWIND: yarn add tailwindcss --dev 🤦‍♂️

./node_modules/.bin/tailwind init tailwind.js

  1. Create a file inside app/javascript/src named styles.css

app/javascript/src/styles.css

Paste in the contents of this file.

  1. In app/views/layouts/application.html.erb' add this in the ` (or wherever you want)
<%= javascript_pack_tag 'application' %>
<%= stylesheet_pack_tag 'application' %>
  1. Now, hop back in a terminal and run:

./node_modules/.bin/tailwind build app/javascript/src/styles.css -o app/javascript/src/application.css

This builds the CSS for the application.

  1. Inside app/javascript/packs/application.js add:

import "src/application.css"

  1. Fire up your servers:

In one terminal window: ./bin/webpack-dev-server' In another: rails s`

  1. Bask in Webpacker + Tailwind CSS 😎

I hope this helps. As I mentioned, this is just my first attempt at it. I'm glad to share with you (and hope you'll share with me!) as I experiment more with Rails + Tailwind.

jasoncharnes avatar Nov 04 '17 18:11 jasoncharnes

@jasoncharnes Thanks! Yeah I have been trying to do this with Webpacker, but I wasn't sure where to place the code that is in the docs for Webpack (The tailwind docs that is).

I tried placing that in a new file called: postcss.config.js but nothing happend 🙈 .

I'll give this a shot and see if I can get it deployed to Heroku!

jvanbaarsen avatar Nov 06 '17 08:11 jvanbaarsen

@jasoncharnes Great, I got this working locally. Only thing that I had to change was the following:

"yarn add tailwindcss --dev" -> When I ran that command the tailwindcss was not added to package.json. So I manually added it to that file to the dependency group, and ran: yarn install.

It does seem that the config js (The one in the root tailwind.js) is not picked up. I'm looking into ways of getting this to work :)

jvanbaarsen avatar Nov 06 '17 10:11 jvanbaarsen

Nice glad you guys are figuring this out! Once you feel like you've got it ironed out pretty well, would you mind sharing a simple bare bones Rails repo with everything setup? Would be great for helping out other trying to integrate with Rails 👍

adamwathan avatar Nov 06 '17 13:11 adamwathan

@adamwathan Yes! Collaborative learning 💪

jvanbaarsen avatar Nov 06 '17 14:11 jvanbaarsen

@jasoncharnes / @adamwathan It looks like I have the full app running now! I'll do another test with a brand new app, after that is working I'll post it as an example app.

jvanbaarsen avatar Nov 08 '17 20:11 jvanbaarsen

@jasoncharnes I got it all working. For now I quickly created a Heroku app and an example repo without any readme:

Repo: https://github.com/jvanbaarsen/tailwindcss-rails-example URL: https://tailwind-rails-example.herokuapp.com/

All the configuration has been done in this commit: https://github.com/jvanbaarsen/tailwindcss-rails-example/commit/9e00e2719570bfe06299fcf8499df63f98563c3d

The most import thing is to remove the .postcssrc file, and replace it with the postcss.config.js file.

Tomorrow I'll write some proper documentation with that readme, and if @adamwathan would accept a PR for it a small headline in the official docs on how to set it up and a link to that example repo (Or even better, maybe they can import it under the tailwindcss namespace and collect a bunch of example apps)

jvanbaarsen avatar Nov 08 '17 21:11 jvanbaarsen

@jvanbaarsen Could you add your example to the awesome-tailwind list?

m1guelpf avatar Nov 09 '17 07:11 m1guelpf

@m1guelpf Where do you want me to add it? Under Resources or Open Source?

jvanbaarsen avatar Nov 09 '17 08:11 jvanbaarsen

@jvanbaarsen I'd put it under resources, but creating a quickstart subsection in the resources section would be the best thing.

m1guelpf avatar Nov 09 '17 10:11 m1guelpf

@jvanbaarsen I got Tailwind to work with Rails (5.1.4) and Webpacker gem without replacing the .postcssrc file.

Here is my .postcssrc: https://github.com/jonaskay/tailwind-rails-blog/blob/master/.postcssrc.yml

jonaskay avatar Nov 29 '17 00:11 jonaskay

@jonaskay Thanks for sharing :-) Yeah I got that figured out in the end as well! The syntax feels so weird :P

jvanbaarsen avatar Nov 29 '17 07:11 jvanbaarsen

This is very helpful, and I can't emphasize enough how important it is to build the Tailwind file using: ./node_modules/.bin/tailwind build app/javascript/src/styles.css -o app/javascript/src/application.css.

I find that its easiest to add this to your package.json as a script, so that while working in you development environment, you're beginning with the latest build of your Tailwind config.

See the "scripts" key in my package.json:

{
  "name": "cloudcma",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "[email protected]:wrstudios/cloudcma.git",
  "license": "MIT",
  "dependencies": {
    "@rails/webpacker": "^3.2.0",
    "tailwindcss": "^0.4.0",
    "webpack-dev-server": "^2.9.7"
  },
  "scripts": {
    "dev": "./node_modules/.bin/tailwind build app/javascript/src/styles.css -o app/javascript/src/application.css && ./bin/webpack-dev-server"
  }
}

This way, typing yarn run dev in your terminal will build your Tailwind css file and launch the webpack dev server.

Thanks again for the help in here, it was great!

skywinston avatar Jan 05 '18 01:01 skywinston

@skywinston For me there is no need to use the build script. With the example app I posted earlier you can run bin/webpack-dev-server and it will start a webpack server (which auto compiles on CSS changes)

jvanbaarsen avatar Jan 05 '18 07:01 jvanbaarsen

@jvanbaarsen You're absolutely correct. I couldn't get this to work until I ran ./node_modules/.bin/tailwind build app/javascript/src/styles.css -o app/javascript/src/application.css and assumed that webpack was missing the initial build step. But running bin/webpack-dev-server just now and making a change to my tailwind.js config resulted in a new class being generated, so it looks like webpack is watching for these changes. Thanks for clearing that up; I think its safe to disregard my previous comment.

skywinston avatar Jan 05 '18 17:01 skywinston

@jvanbaarsen Do you know if this is watching changes to the application.scss file or style.css file in @jasoncharnes case. This is where you have @tailwind preflight and @tailwind utilities, plus any component class you'll be customizing as you build the app. I have added some changes there, and webpack does not respond to changes made in that file. This file really should be watched as plenty of additions will be made here as the app is being built. Any thoughts on this?

skywinston avatar Jan 05 '18 17:01 skywinston

@skywinston I believe in his example it is watching the style.css file. But in my example repo (https://github.com/jvanbaarsen/tailwindcss-rails-example) it is watching this file: https://github.com/jvanbaarsen/tailwindcss-rails-example/blob/master/app/javascript/src/application.scss

jvanbaarsen avatar Jan 06 '18 08:01 jvanbaarsen

@skywinston I'm in the process of writing a blogpost on step by step setting up Tailwind in your rails post. It should be up beginning next week (I'll post here when it's up 👍 )

jvanbaarsen avatar Jan 06 '18 08:01 jvanbaarsen

Here's my take on it https://github.com/artmann/rails-tailwind-example

Artmann avatar Jan 11 '18 16:01 Artmann

@jvanbaarsen Can you update the steps for a successful deploy to heroku? I've had no issues running tailwindcss locally but fail to deploy via heroku or aws.

rymartin767 avatar Feb 15 '18 01:02 rymartin767

@DawgOnKing I'd be happy to help. I'm successfully deploying Rails 5.2/Edge apps using Tailwind to Heroku right now. What version of Rails are you using and are you using Webpacker to manage everything?

joeybeninghove avatar Feb 15 '18 01:02 joeybeninghove

@joeybeninghove Greatly appreciated! I'm running "locally" on Cloud9 via Rails 5.2 and webpacker gem.

rymartin767 avatar Feb 15 '18 02:02 rymartin767

@DawgOnKing Oh ok, well I'm only working with Heroku at the moment, not Cloud9 or AWS directly. I'll still try to push up a full repo later if I have a chance, but for now, here is a quick Gist showing the relevant files that I've set up to work: https://gist.github.com/joeybeninghove/882ca5a2ea2917ffca087609a7e4c181

This a pretty vanilla set up honestly, as Heroku takes care of automatically running the webpack compile during deployment.

joeybeninghove avatar Feb 15 '18 02:02 joeybeninghove

@joeybeninghove - Thanks again for your help. At quick glance, my setup is exactly the same as yours. I will create another app and post specific errors I'm getting from heroku during compile (which is where it's failing).

rymartin767 avatar Feb 15 '18 02:02 rymartin767

@DawgOnKing Did you setup the buildpacks for Heroku?

jvanbaarsen avatar Feb 15 '18 15:02 jvanbaarsen

@jvanbaarsen - short answer, no. I attempted to via console but attempts have been unsuccessful. Did you use CLI?

rymartin767 avatar Feb 15 '18 19:02 rymartin767

Here's a breakdown for getting Tailwind up and running on Cloud9 machines. This isn't the AWS setup although I have that running as well. I just need additional steps for deploying via Heroku and AWS CodeDeploy which I will work on tomorrow.

Thanks again for assistance with deployment!

https://github.com/DawgOnKing/cloud9-tailwindcss

rymartin767 avatar Feb 15 '18 21:02 rymartin767

@DawgOnKing I believe I set the Buildpacks in the Heroku interface itself. It is very important that the Nodejs buildpack comes before the ruby buildpack. Otherwise it won't work.

jvanbaarsen avatar Feb 16 '18 13:02 jvanbaarsen

@DawgOnKing @jvanbaarsen That’s true about the buildpacks, but in my recent deployments to Heroku, they just figured that out for me automatically. I didn’t have to do anything special to manually configure the buildpacks. I’m not sure how or what about the Rails apps I’m deploying that makes that the case, but I’m happy it’s working like that for me. :)

Sent with GitHawk

joeybeninghove avatar Feb 16 '18 14:02 joeybeninghove