Skip to content

Commit ff37f87

Browse files
authored
Merge pull request #316 from ahmeij/feature/allow_custom_postcss_config
Allow custom postcss.config.js
2 parents c14c681 + a30ad61 commit ff37f87

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

Diff for: README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ This also works with relative paths. If you've installed into your app's directo
3838
TAILWINDCSS_INSTALL_DIR=node_modules/.bin
3939
```
4040

41-
4241
## Developing with Tailwindcss
4342

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

102101

102+
### Using with PostCSS
103+
104+
If you want to use PostCSS as a preprocessor, create a custom `config/postcss.config.js` and it will be loaded automatically.
105+
106+
For example, to enable nesting:
107+
108+
```js
109+
// config/postcss.config.js
110+
module.exports = {
111+
plugins: {
112+
'postcss-import': {},
113+
'tailwindcss/nesting': {},
114+
tailwindcss: {},
115+
autoprefixer: {},
116+
},
117+
}
118+
```
119+
103120
### Custom inputs or outputs
104121

105122
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.

Diff for: lib/tailwindcss/commands.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,19 @@ def executable(exe_path: DEFAULT_DIR)
7474
end
7575

7676
def compile_command(debug: false, **kwargs)
77-
[
77+
command = [
7878
executable(**kwargs),
7979
"-i", Rails.root.join("app/assets/stylesheets/application.tailwind.css").to_s,
8080
"-o", Rails.root.join("app/assets/builds/tailwind.css").to_s,
8181
"-c", Rails.root.join("config/tailwind.config.js").to_s,
82-
].tap do |command|
83-
command << "--minify" unless (debug || rails_css_compressor?)
84-
end
82+
]
83+
84+
command << "--minify" unless (debug || rails_css_compressor?)
85+
86+
postcss_path = Rails.root.join("config/postcss.config.js")
87+
command += ["--postcss", postcss_path.to_s] if File.exist?(postcss_path)
88+
89+
command
8590
end
8691

8792
def watch_command(always: false, poll: false, **kwargs)

Diff for: test/lib/tailwindcss/commands_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ def mock_local_tailwindcss_install
151151
end
152152
end
153153

154+
test ".compile_command when postcss.config.js exists" do
155+
mock_exe_directory("sparc-solaris2.8") do |dir, executable|
156+
Dir.mktmpdir do |tmpdir|
157+
Rails.stub(:root, Pathname.new(tmpdir)) do # Rails.root won't work in this test suite
158+
actual = Tailwindcss::Commands.compile_command(exe_path: dir)
159+
assert_kind_of(Array, actual)
160+
assert_equal(executable, actual.first)
161+
refute_includes(actual, "--postcss")
162+
163+
config_file = Rails.root.join("config/postcss.config.js")
164+
FileUtils.mkdir_p(Rails.root.join("config"))
165+
FileUtils.touch(config_file)
166+
actual = Tailwindcss::Commands.compile_command(exe_path: dir)
167+
assert_kind_of(Array, actual)
168+
assert_equal(executable, actual.first)
169+
assert_includes(actual, "--postcss")
170+
postcss_index = actual.index("--postcss")
171+
assert_equal(actual[postcss_index + 1], config_file.to_s)
172+
end
173+
end
174+
end
175+
end
176+
154177
test ".watch_command" do
155178
mock_exe_directory("sparc-solaris2.8") do |dir, executable|
156179
Rails.stub(:root, File) do # Rails.root won't work in this test suite

0 commit comments

Comments
 (0)