Skip to content

Commit c68a7e1

Browse files
author
David Heinemeier Hansson
authored
Merge pull request rails#16 from BakiVernes/webpack_setup
Install script for Webpack
2 parents c61f1f1 + 1c513c1 commit c68a7e1

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
/node_modules
1212
.byebug_history
1313
*.gem
14+
.idea/

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Tailwind CSS for Rails makes it easy to use this CSS framework with the asset pipeline. In development mode, the full 3mb+ Tailwind stylsheet is loaded, but in production, only the css classes used by files in `app/views` and `app/helpers` are included.
66

7-
This gem just gives access to the standard Tailwind CSS framework. If you need to customize Tailwind, you will need to install it the traditional way using [Webpacker](https://github.com/rails/webpacker) instead. This gem is purely intended for those who wish to use Tailwind CSS with the asset pipeline.
7+
This gem just gives access to the standard Tailwind CSS framework. If you need to customize Tailwind, you will need to have [Webpacker](https://github.com/rails/webpacker) installed. Tailwind will be installed automatically if Webpacker is installed. After this you need to refer to the [TailwindCSS documentation](https://tailwindcss.com/docs/installation#customizing-your-configuration) to customize your tailwind.config.js file.
88

99
The version of Tailwind included in this gem has been configured for dark mode, forms, aspect-ratio, typography, and the Inter font.
1010

@@ -52,4 +52,4 @@ By default, the CSS purger will only operate on the tailwind css file included w
5252

5353
Tailwind for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
5454
Tailwind CSS is released under the [MIT License](https://opensource.org/licenses/MIT).
55-
The Inter font is relased under the [SIL Open Font License, Version 1.1](https://github.com/rsms/inter/blob/master/LICENSE.txt).
55+
The Inter font is relased under the [SIL Open Font License, Version 1.1](https://github.com/rsms/inter/blob/master/LICENSE.txt).
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
LATEST_WEBPACKER = "\"@rails\/webpacker\": \"rails\/webpacker#b6c2180\",".freeze
2+
TAILWIND_IMPORTS = "@import \"tailwindcss/base\";\n@import \"tailwindcss/components\";\n@import \"tailwindcss/utilities\";\n".freeze
3+
4+
say "Installing Tailwind CSS"
5+
6+
# Current webpacker version relies on an older version of PostCSS
7+
# which the latest TailwindCSS version is not compatible with
8+
gsub_file('package.json', /\"@rails\/webpacker\".*/) { |matched_line| matched_line = LATEST_WEBPACKER }
9+
10+
say "Adding latest tailwind and postcss"
11+
run "yarn add tailwindcss@latest postcss@latest autoprefixer@latest"
12+
insert_into_file "#{Webpacker.config.source_entry_path}/application.js",
13+
"\nrequire(\"stylesheets/application.scss\")\n"
14+
15+
say "Adding minimal configuration for TailwindCSS to work properly"
16+
stylesheets_directory = "#{Webpacker.config.source_path}/stylesheets"
17+
empty_directory stylesheets_directory
18+
add_file "#{stylesheets_directory}/application.scss"
19+
insert_into_file "#{stylesheets_directory}/application.scss", TAILWIND_IMPORTS
20+
21+
insert_into_file "postcss.config.js",
22+
"require('tailwindcss'),\n\t", before: "require('postcss-import')"
23+
24+
say "Tailwind CSS successfully installed️", :green

lib/tasks/tailwindcss_tasks.rake

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
def run_install_template(path)
2+
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}"
3+
end
4+
15
namespace :tailwindcss do
26
desc "Install Tailwind CSS into the app"
37
task :install do
4-
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/tailwindcss.rb", __dir__)}"
8+
if defined?(Webpacker::Engine)
9+
Rake::Task["tailwindcss:install:webpacker"].invoke
10+
else
11+
Rake::Task["tailwindcss:install:asset_pipeline"].invoke
12+
end
513
end
614

715
desc "Show the list of class names being kept in Tailwind CSS"
@@ -15,4 +23,16 @@ namespace :tailwindcss do
1523
Pathname.new(__FILE__).join("../../../app/assets/stylesheets/tailwind.css").read,
1624
keeping_class_names_from_files: Rails.root.glob("app/views/**/*.*") + Rails.root.glob("app/helpers/**/*.rb")
1725
end
26+
27+
namespace :install do
28+
desc "Install Tailwind CSS with the asset pipeline"
29+
task :asset_pipeline do
30+
run_install_template "tailwindcss_with_asset_pipeline"
31+
end
32+
33+
desc "Install Tailwind CSS with webpacker"
34+
task :webpacker do
35+
run_install_template "tailwindcss_with_webpacker"
36+
end
37+
end
1838
end

0 commit comments

Comments
 (0)