Errors trying to configure Tailwind 2.1 with Rails 6
What version of Tailwind CSS are you using?
v2.2.2
What build tool (or framework if it abstracts the build tool) are you using?
Webpack 5.11, Rails 6.1.3
What version of Node.js are you using?
v12.16.3
What browser are you using?
Chrome
What operating system are you using?
macOS
Reproduction repository
https://github.com/renatodex/rails6-tailwind21-webpacker6-webpack-5
Describe your issue
In my configuration, I have a Rails 6 Project, configured with Webpacker 6 (Beta), Webpack 5, React and Sass.
I've tried to follow Tailwind guide to configure Postcss but it didn't worked.
To make this communication easier, I've hosted a simple repository with my project. https://github.com/renatodex/rails6-tailwind21-webpacker6-webpack-5
When I run webpack-dev-server, I keep getting this error:
ERROR in ./app/packs/entrypoints/hello_react.pcss 1:0
Module parse failed: Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> @import "tailwindcss/base";
|
| @import "tailwindcss/components";
webpack 5.39.1 compiled with 1 error in 1929 ms
ℹ 「wdm」: Failed to compile.
However, as you can see, postconfig is already configured properly with the tailwind postcss plugin:
module.exports = {
plugins: [
require('postcss-import'),
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009'
},
stage: 3
}),
require('tailwindcss'),
require('autoprefixer')
]
}
It should be working, right? I have no idea what I am doing wrong. :(
My goal is simple:
Setup a fully functional Rails 6 project, using Tailwind 2.1 with JIT (Just in Time) and React, using Rails Webpacker. I appreciate your patience very much for reading and trying to help me with this!
My steps to reproduce:
- Install Rails 6.1.3
- Create new rails project with
rails new myproject - Add Webpacker Gem to Gemfile
gem 'webpacker', '6.0.0.beta.7' - Install Webpacker
bundle exec rails webpacker:install - Install React
bundle exec rails webpacker:install:react - Remove
.browserslistrcfile from root. - Add
ENV["WEBPACK_DEV_SERVER"] = "true"tobin/webpack-dev-server - Install a couple Packages
yarn add react react-dom tailwindcss autoprefixer @babel/preset-react postcss - Add
'@babel/preset-react'tobabel.config.js - Add
tailwindcssandautoprefixertopostcss.config.js - Add example.jsx file to
app/packs/entrypoints - Run
npx tailwindcss initto create the config file - Create a simple controller with one index action
rails g controller Home index
When you run bin/webpack-dev-server, you should see the error.
Oh. this is the content of the example.jsx file:
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import 'tailwindcss/tailwind.css'
const Hello = props => (
<div>Hello {props.name}!</div>
)
Hello.defaultProps = {
name: 'David'
}
Hello.propTypes = {
name: PropTypes.string
}
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<Hello name="React" />,
document.body.appendChild(document.createElement('div')),
)
})
I have this in config/webpack/base.js :
const { merge, webpackConfig } = require("@rails/webpacker");
const customConfig = {
resolve: {
extensions: [".css"],
},
};
module.exports = merge(webpackConfig, customConfig);
I'm pretty sure this is not related toto tailwindcss. Postcss does not now how to process the @import ... command.
Your postcss config attempts to require postcss-import, but it's missing from package.json. Try adding postcss-import to your package.json and see if that fixes your error.
yarn add postcss-import