From b27be4a4226368f13be515013c8120b1b920d072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Dubois?= Date: Thu, 28 Dec 2023 01:52:04 +0100 Subject: [PATCH 1/8] Suggest defining config.assets.css_compressor = nil (#129) --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index ee8e906..a6c1db4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,15 @@ Some CSS packages use new CSS features that are not supported by the default Sas A common issue is that your repository does not contain the output directory used by the build commands. You must have `app/assets/builds` available. Add the directory with a `.gitkeep` file, and you'll ensure it's available in production. +### How do I avoid `ActionView::Template::Error: Error: Function rgb is missing argument $green`? + +This might happen if your Gemfile.lock contains the legacy `sassc-rails`, which might be need while progressively migrating your project, or which might be a transitive dependency of a gem your project depends on and over which you have no control. In this case, prevent Sprockets from bundling the CSS on top of the bundling already performed by this gem. Make sure do this for all environments, not only production, otherwise your test suite may fail. + +``` +# config/initializers/assets.rb +Rails.application.config.assets.css_compressor = nil +``` + ### Why isn't Rails using my updated css files? Watch out - if you precompile your files locally, those will be served over the dynamically created ones you expect. The solution: From 02caad4e0613404d535f804ebffe70ee3fc496da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Mari=C3=A9?= Date: Fri, 19 Jan 2024 15:38:54 +0100 Subject: [PATCH 2/8] Synchronize bin/dev from jsbundling-rails to cssbundling-rails (#151) There are different bin/dev being generated by cssbundling-rails and jsbundling-rails See https://github.com/rails/jsbundling-rails/pull/174 > dev: Use an affirmative conditional > > ...and avoid regex in match. > > (Try a gem list ails vs a gem list --exact ails to see what I mean.) --- lib/install/dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/install/dev b/lib/install/dev index a4e05fa..eda330c 100755 --- a/lib/install/dev +++ b/lib/install/dev @@ -1,6 +1,6 @@ #!/usr/bin/env sh -if ! gem list foreman -i --silent; then +if gem list --no-installed --exact --silent foreman; then echo "Installing foreman..." gem install foreman fi From 7638f924d7376f8fa1fcd1af65f7d673e94b9321 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Sun, 21 Jan 2024 13:23:32 -0600 Subject: [PATCH 3/8] Use Thor's `apply` instead of a prerequisite task (#150) The `css:install:shared` task serves only as a prerequisite for the other installer tasks; it should not be run on its own (nor listed with `rake --tasks`). By replacing this task with corresponding calls to Thor's `apply` method, we avoid the overhead of running `bin/rails app:template` (and `bundle install`) multiple times. --- lib/install/bootstrap/install.rb | 2 ++ lib/install/bulma/install.rb | 2 ++ lib/install/postcss/install.rb | 2 ++ lib/install/sass/install.rb | 2 ++ lib/install/tailwind/install.rb | 2 ++ lib/tasks/cssbundling/install.rake | 15 +++++---------- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/install/bootstrap/install.rb b/lib/install/bootstrap/install.rb index bfea9b0..f00991e 100644 --- a/lib/install/bootstrap/install.rb +++ b/lib/install/bootstrap/install.rb @@ -1,6 +1,8 @@ require_relative "../helpers" self.extend Helpers +apply "#{__dir__}/../install.rb" + say "Install Bootstrap with Bootstrap Icons, Popperjs/core and Autoprefixer" copy_file "#{__dir__}/application.bootstrap.scss", "app/assets/stylesheets/application.bootstrap.scss" diff --git a/lib/install/bulma/install.rb b/lib/install/bulma/install.rb index c5276d0..a6de35a 100644 --- a/lib/install/bulma/install.rb +++ b/lib/install/bulma/install.rb @@ -1,6 +1,8 @@ require_relative "../helpers" self.extend Helpers +apply "#{__dir__}/../install.rb" + say "Install Bulma" copy_file "#{__dir__}/application.bulma.scss", "app/assets/stylesheets/application.bulma.scss" diff --git a/lib/install/postcss/install.rb b/lib/install/postcss/install.rb index d9eb5bd..e80280b 100644 --- a/lib/install/postcss/install.rb +++ b/lib/install/postcss/install.rb @@ -1,6 +1,8 @@ require_relative "../helpers" self.extend Helpers +apply "#{__dir__}/../install.rb" + say "Install PostCSS w/ nesting and autoprefixer" copy_file "#{__dir__}/postcss.config.js", "postcss.config.js" copy_file "#{__dir__}/application.postcss.css", "app/assets/stylesheets/application.postcss.css" diff --git a/lib/install/sass/install.rb b/lib/install/sass/install.rb index 692d60e..b05a8e5 100644 --- a/lib/install/sass/install.rb +++ b/lib/install/sass/install.rb @@ -1,6 +1,8 @@ require_relative "../helpers" self.extend Helpers +apply "#{__dir__}/../install.rb" + say "Install Sass" copy_file "#{__dir__}/application.sass.scss", "app/assets/stylesheets/application.sass.scss" run "#{bundler_cmd} add sass" diff --git a/lib/install/tailwind/install.rb b/lib/install/tailwind/install.rb index 7374ff4..f8e530d 100644 --- a/lib/install/tailwind/install.rb +++ b/lib/install/tailwind/install.rb @@ -1,6 +1,8 @@ require_relative "../helpers" self.extend Helpers +apply "#{__dir__}/../install.rb" + say "Install Tailwind (+PostCSS w/ autoprefixer)" copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js" copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css" diff --git a/lib/tasks/cssbundling/install.rake b/lib/tasks/cssbundling/install.rake index d093d8d..ba1df4c 100644 --- a/lib/tasks/cssbundling/install.rake +++ b/lib/tasks/cssbundling/install.rake @@ -1,32 +1,27 @@ namespace :css do namespace :install do - desc "Install shared elements for all bundlers" - task :shared do - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install.rb", __dir__)}" - end - desc "Install Tailwind" - task tailwind: "css:install:shared" do + task :tailwind do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/tailwind/install.rb", __dir__)}" end desc "Install PostCSS" - task postcss: "css:install:shared" do + task :postcss do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/postcss/install.rb", __dir__)}" end desc "Install Sass" - task sass: "css:install:shared" do + task :sass do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/sass/install.rb", __dir__)}" end desc "Install Bootstrap" - task bootstrap: "css:install:shared" do + task :bootstrap do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/bootstrap/install.rb", __dir__)}" end desc "Install Bulma" - task bulma: "css:install:shared" do + task :bulma do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/bulma/install.rb", __dir__)}" end end From 9075819d32c7e1103b1ee7c237e79001547066af Mon Sep 17 00:00:00 2001 From: Tyler Paige Date: Sun, 21 Jan 2024 14:24:58 -0500 Subject: [PATCH 4/8] Add instructions for including 3rd party packages in bundle (#148) See discussion in issue #92 https://github.com/rails/cssbundling-rails/issues/92 --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index a6c1db4..9e7c24f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,15 @@ Watch out - if you precompile your files locally, those will be served over the rails assets:clobber ``` +### How do I include 3rd party stylesheets from `node_modules` in my bundle? + +Use an `@import` statement and path to a specific stylesheet, omitting the `node_modules/` segment and the file's extension. For example: + +```scss +/* Desired file is at at node_modules/select2/dist/css/select2.css */ +@import "https://github.com/select2/dist/css/select2"; +``` + ## License CSS Bundling for Rails is released under the [MIT License](https://opensource.org/licenses/MIT). From 3e768cdabf51bb5955388804a6a56ad26febfb5c Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Sun, 21 Jan 2024 14:26:16 -0500 Subject: [PATCH 5/8] Fix running "rails new --css bootstrap" on 7.1 (#147) Rails 7.1 included a [change][1] to allow using importmaps along with all cssbundling options. However, the Bootstrap installer was never updated to take this new default into effect (and is currently broken because of this). This commit adds the additional configuration required to use the Bootstrap npm package with importmaps so that "rails new" generates an application that runs without errors. [1]: rails/rails@84458a8577e09621205f2ea86f570745033105f5 --- lib/install/bootstrap/install.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/install/bootstrap/install.rb b/lib/install/bootstrap/install.rb index f00991e..abf8e1d 100644 --- a/lib/install/bootstrap/install.rb +++ b/lib/install/bootstrap/install.rb @@ -21,6 +21,19 @@ say %(Add import * as bootstrap from "bootstrap" to your entry point JavaScript file), :red end +if Rails.root.join("config/importmap.rb").exist? + say "Pin Bootstrap" + append_to_file "config/importmap.rb", %(pin "bootstrap", to: "bootstrap.min.js"\n) + + inject_into_file "config/initializers/assets.rb", after: /.*Rails.application.config.assets.paths.*\n/ do + <<~RUBY + Rails.application.config.assets.paths << Rails.root.join("node_modules/bootstrap/dist/js") + RUBY + end + + append_to_file "config/initializers/assets.rb", %(Rails.application.config.assets.precompile << "bootstrap.min.js") +end + add_package_json_script("build:css:compile", "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules") add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css") add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix") From e650c9ab135504cc2f8b8405c41cdebad5ede66f Mon Sep 17 00:00:00 2001 From: Kevin Sylvestre Date: Sun, 21 Jan 2024 11:27:25 -0800 Subject: [PATCH 6/8] PNPM Support (#145) * Simplify build tool selection for bun / yarn Simplifying ahead of introducing NPM as a build tool. * Support for NPM * Support PNPM --- lib/tasks/cssbundling/build.rake | 33 ++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/tasks/cssbundling/build.rake b/lib/tasks/cssbundling/build.rake index dcdd429..8410147 100644 --- a/lib/tasks/cssbundling/build.rake +++ b/lib/tasks/cssbundling/build.rake @@ -22,15 +22,36 @@ module Cssbundling extend self def install_command - return "bun install" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock')) - return "yarn install" if File.exist?('yarn.lock') || tool_exists?('yarn') - raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies" + case tool + when :bun then "bun install" + when :yarn then "yarn install" + when :pnpm then "pnpm install" + when :npm then "npm install" + else raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies" + end end def build_command - return "bun run build:css" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock')) - return "yarn build:css" if File.exist?('yarn.lock') || tool_exists?('yarn') - raise "cssbundling-rails: No suitable tool found for building CSS" + case tool + when :bun then "bun run build:css" + when :yarn then "yarn build:css" + when :pnpm then "pnpm build:css" + when :npm then "npm run build:css" + else raise "cssbundling-rails: No suitable tool found for building CSS" + end + end + + def tool + case + when File.exist?('bun.lockb') then :bun + when File.exist?('yarn.lock') then :yarn + when File.exist?('pnpm-lock.yaml') then :pnpm + when File.exist?('package-lock.json') then :npm + when tool_exists?('bun') then :bun + when tool_exists?('yarn') then :yarn + when tool_exists?('pnpm') then :pnpm + when tool_exists?('npm') then :npm + end end def tool_exists?(tool) From f9e6dc47b44bca53174e03f9ba504fde5648576d Mon Sep 17 00:00:00 2001 From: dhh Date: Sun, 21 Jan 2024 11:37:24 -0800 Subject: [PATCH 7/8] Bump version for 1.4.0 --- Gemfile.lock | 2 +- lib/cssbundling/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f44fffc..63a04a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - cssbundling-rails (1.3.3) + cssbundling-rails (1.4.0) railties (>= 6.0.0) GEM diff --git a/lib/cssbundling/version.rb b/lib/cssbundling/version.rb index 5b6d7ae..e7ff022 100644 --- a/lib/cssbundling/version.rb +++ b/lib/cssbundling/version.rb @@ -1,3 +1,3 @@ module Cssbundling - VERSION = "1.3.3" + VERSION = "1.4.0" end From 09d81cb0accf00abb77d8af5b24f5aad0b71a57a Mon Sep 17 00:00:00 2001 From: dhh Date: Sun, 21 Jan 2024 12:10:24 -0800 Subject: [PATCH 8/8] Fix escaping issue with bootstrap command Fixed #137 --- lib/install/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/install/helpers.rb b/lib/install/helpers.rb index b8837f8..dd4e974 100644 --- a/lib/install/helpers.rb +++ b/lib/install/helpers.rb @@ -21,7 +21,7 @@ def add_package_json_script(name, script, run_script=true) if using_bun? package_json = JSON.parse(File.read("package.json")) package_json["scripts"] ||= {} - package_json["scripts"][name] = script + package_json["scripts"][name] = script.gsub('\\"', '"') File.write("package.json", JSON.pretty_generate(package_json)) run %(bun run #{name}) if run_script else