railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.88k stars 2.25k forks source link

Problem with importmap and version 3.1.0 #3572

Closed dailson-igo closed 1 year ago

dailson-igo commented 1 year ago

Describe the bug Error: File to import not found or unreadable: rails_admin/src/rails_admin/styles/base with version 3.1 and importmap, without ESbuild. The problem does not occur when I use ESbuild with version 3.1 or without ESbuild with version 3.0.

Reproduction steps

  1. rails _7.0.4_ new defaultjs --skip --database=sqlite3
  2. Add in Gemfile: gem "sassc-rails" and gem "rails_admin", "~>3.1"
  3. bundle install
  4. rails g rails_admin:install
  5. rails db:create
  6. Start server
  7. Erro when open link http://0.0.0.0:3000/admin

Expected behavior I expect the version 3.1.0 work with importmap, without ESbuild.

Additional context

image

Started GET "/admin/" for 127.0.0.1 at 2022-11-24 09:33:16 -0300
Processing by RailsAdmin::MainController#dashboard as HTML
  Rendering layout /home/igo/.rbenv/versions/3.1.2/lib64/ruby/gems/3.1.0/gems/rails_admin-3.1.0/app/views/layouts/rails_admin/application.html.erb
  Rendering /home/igo/.rbenv/versions/3.1.2/lib64/ruby/gems/3.1.0/gems/rails_admin-3.1.0/app/views/rails_admin/main/dashboard.html.erb within layouts/rails_admin/application
  Rendered /home/igo/.rbenv/versions/3.1.2/lib64/ruby/gems/3.1.0/gems/rails_admin-3.1.0/app/views/rails_admin/main/dashboard.html.erb within layouts/rails_admin/application (Duration: 1.1ms | Allocations: 759)
  Rendered /home/igo/.rbenv/versions/3.1.2/lib64/ruby/gems/3.1.0/gems/rails_admin-3.1.0/app/views/layouts/rails_admin/_head.html.erb (Duration: 14.0ms | Allocations: 9416)
  Rendered layout /home/igo/.rbenv/versions/3.1.2/lib64/ruby/gems/3.1.0/gems/rails_admin-3.1.0/app/views/layouts/rails_admin/application.html.erb (Duration: 16.5ms | Allocations: 10696)
Completed 500 Internal Server Error in 170ms (ActiveRecord: 0.8ms | Allocations: 83151)

ActionView::Template::Error (Error: File to import not found or unreadable: rails_admin/src/rails_admin/styles/base.
        on line 2:1 of app/assets/stylesheets/rails_admin.scss
>> @import "rails_admin/src/rails_admin/styles/base";

   ^
):
    16:   <%= stylesheet_link_tag "rails_admin.css", media: :all, data: {'turbo-track': 'reload'} %>
    17:   <%= javascript_include_tag "rails_admin.js", defer: true, data: {'turbo-track': 'reload'} %>
    18: <% when :importmap %>
    19:   <%= stylesheet_link_tag "rails_admin.css", media: :all, data: {'turbo-track': 'reload'} %>
    20:   <%= javascript_inline_importmap_tag(RaidlsAdmin::Engine.importmap.to_json(resolver: self)) %>
    21:   <%= javascript_importmap_module_preload_tags(RailsAdmin::Engine.importmap) %>
    22:   <%= javascript_importmap_shim_nonce_configuration_tag %>

app/assets/stylesheets/rails_admin.scss:2

Files referencing the rails_admin text in version 3.1.0.

image

mshibuya commented 1 year ago

You need to follow this instruction, then run yarn build:css manually before starting the rails server. That's the cssbundling-rails way. sassc-rails is just unnecessary.

maximedelpit commented 1 year ago

Hi @mshibuya, From my understanding, the rationale for importmap is to get rid of bundling assets (i.e. dropping npm / yarn...). Shouldn't css be loaded through the old sprockets way when going for importmap?

thanks

mshibuya commented 1 year ago

cssbundling-rails is not 'the old sprockets way', the build process is totally different.

So what's your recommendation on building CSS, not JS?

maximedelpit commented 1 year ago

Well, I'm definitely not a master of rails asset pipeline, so this should be challenged.

Based on some research (see below), I think the goal would be to avoid adding js bundling if user is trying to go the rails 7 way. I am not convinced our point is to obviously mimick user's gemfile choices (cftailwindcss or dartsass references below).

First option i have in mind: letting the user decide what he wants for js and what he wants for css.

2nd option: inspect the user's app repository and make arbitrary choices based on its config. Pseudo code

if File.dirname("#{destination_root}/node_modules").directory? && File.join(destination_root, 'packages.json')).exists?
 # bundle both js & css through webacker or webpack (since node_modules in use)
elsif File.join(destination_root, 'config/importmap.rb')).exists?
  # import map process for js
 # use sprockets for css (through sassc-rails). (since node_modules not in use)
else
  # use sprockets fo js & css
end

Research sources:

What do you think @mshibuya ?

itsNikolay commented 10 months ago

you need to install cssbundling-rails in addition to import-map:

./bin/bundle add cssbundling-rails
./bin/rails css:install:sass