Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom postcss.config.js #316

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ This also works with relative paths. If you've installed into your app's directo
TAILWINDCSS_INSTALL_DIR=node_modules/.bin
```


## Developing with Tailwindcss

### Configuration
Expand Down Expand Up @@ -100,6 +99,24 @@ If you want unminified assets, you can pass a `debug` argument to the rake task,
Note that you can combine task options, e.g. `rails tailwindcss:watch[debug,poll]`.


### Using with PostCSS

If you want to use PostCSS as a preprocessor, create a custom `config/postcss.config.js` and it will be loaded automatically.

For example, to enable nesting:

```js
// config/postcss.config.js
module.exports = {
plugins: {
'postcss-import': {},
'tailwindcss/nesting': {},
tailwindcss: {},
autoprefixer: {},
},
}
```

### Custom inputs or outputs

If you need to use a custom input or output file, you can run `bundle exec tailwindcss` to access the platform-specific executable, and give it your own build options.
Expand Down
13 changes: 9 additions & 4 deletions lib/tailwindcss/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ def executable(exe_path: DEFAULT_DIR)
end

def compile_command(debug: false, **kwargs)
[
command = [
executable(**kwargs),
"-i", Rails.root.join("app/assets/stylesheets/application.tailwind.css").to_s,
"-o", Rails.root.join("app/assets/builds/tailwind.css").to_s,
"-c", Rails.root.join("config/tailwind.config.js").to_s,
].tap do |command|
command << "--minify" unless (debug || rails_css_compressor?)
end
]

command << "--minify" unless (debug || rails_css_compressor?)

postcss_path = Rails.root.join("config/postcss.config.js")
command += ["--postcss", postcss_path.to_s] if File.exist?(postcss_path)

command
end

def watch_command(always: false, poll: false, **kwargs)
Expand Down
23 changes: 23 additions & 0 deletions test/lib/tailwindcss/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ def mock_local_tailwindcss_install
end
end

test ".compile_command when postcss.config.js exists" do
mock_exe_directory("sparc-solaris2.8") do |dir, executable|
Dir.mktmpdir do |tmpdir|
Rails.stub(:root, Pathname.new(tmpdir)) do # Rails.root won't work in this test suite
actual = Tailwindcss::Commands.compile_command(exe_path: dir)
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
refute_includes(actual, "--postcss")

config_file = Rails.root.join("config/postcss.config.js")
FileUtils.mkdir_p(Rails.root.join("config"))
FileUtils.touch(config_file)
actual = Tailwindcss::Commands.compile_command(exe_path: dir)
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "--postcss")
postcss_index = actual.index("--postcss")
assert_equal(actual[postcss_index + 1], config_file.to_s)
end
end
end
end

test ".watch_command" do
mock_exe_directory("sparc-solaris2.8") do |dir, executable|
Rails.stub(:root, File) do # Rails.root won't work in this test suite
Expand Down