Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Rails 7] - Help to configure a newly migrated app to process activeadmin assets (js and scss) using jsbundling-rails with esbuild#7947

Unanswered
guillaumebesse asked this question inHelp
Discussion options

Thanks to the work in progress in PR#7235, activeadmin is starting.

But I don't know how to deal with active_admin.js and my custom scss files.
I am new to esbuild.

Any help is welcome.

You must be logged in to vote

Replies: 9 comments 10 replies

Comment options

But I don't know how to deal with active_admin.js and my custom scss files.
I am new to esbuild.

Hi

⚠️ Disclaimer: I'm not a JavaScript developer

I switched back to webpacker for the moment, because I can see that Active Adminis using jQuery UI and this library does not play well with esbuild by default

I think this resource could be helpful:https://gorails.com/episodes/how-to-use-jquery-with-esbuild

It requires to download custom assets from jquery-ui and I'm not comfortable in doing that

Slightly refers to#7226

You must be logged in to vote
0 replies
Comment options

I did some progress, everything is not tested but It is really promising.

The goal is to compile inapp/assets/builds

  • two CSS files
    • application.css for my web app and build with tailwindcss
    • active_admin.css build with sass compiler
  • two js files
    • application.js for my web app
    • active_admin.js

Files tree

  • app
    • assets
      • builds
      • config
        • manifest.js
      • stylesheets
        • active_admin.scss
        • application.tailwind.css
    • javascript
      • controllers
        • application.js
        • index.js
      • active_admin.js
      • application.js

1/ In package.json, I have

..."scripts": {    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css && sass ./app/assets/stylesheets/active_admin.scss ./app/assets/builds/active_admin.css --no-source-map --load-path=node_modules",}...

application.tailwind.css: I regroup all my CSS this file

@tailwind base;@tailwind components;@tailwind utilities;// overriding default @layer base {    h1 { @apply ...}    h2 { @apply ...}}@layer components {    .red_button { @apply whitespace-nowrap ... }}

active_admin.scss

@import "@activeadmin/activeadmin/src/scss/mixins";@import "@activeadmin/activeadmin/src/scss/base";// my custom css.yes {  background-color: #c3d4ab;}

active_admin.js

import "@activeadmin/activeadmin";
You must be logged in to vote
0 replies
Comment options

I wanted to get this working with rails 7 and esbuild and took the nuclear option 😅

  1. Generate the final active_admin.js and active_admin.css in a new app.
  2. Copy the generated files to the original app under app/assets/activeadmin
  3. Fork activeadmin and remove dependency on jquery-rails and remove app/assets, app/javascript and vendor/assets
  4. Use the fork in the app

And it works!

It might have been possible to make it work withhttps://github.com/excid3/esbuild-rails but jquery-ui needs a custom build.

If activeadmin can publish a gem with assets precompiled, it will be much easier to use.

You must be logged in to vote
0 replies
Comment options

I have a similar issue, although it is entangled with my limited understanding of the new Rails 7 css and js options. I created a new Rails 7 app with--tailwind, and thusly havetailwindcss-rails in myGemfile. Now, thanks to the works of the developers on the recent Rails 7 PR, I have addedactiveadmin using themaster branch to myGemfile.

After installing and visiting/admin, I am greeted withThe asset 'active_admin.css' was not found in the load path. fromraise Propshaft::MissingAssetError.new(logical_path). Not sure where to go from here, or if I need to replace my tailwind install with one of the other css bundling options.

You must be logged in to vote
1 reply
@JuzerShakir
Comment options

I also face the same issue usingpropshaft as an asset pipeline. However, when creating the app, I didn't specify thejsbundling &cssbundling options.

Did you find any solution to make theactiveadmin work withpropshaft?

Comment options

I successfully manage to installactive_admin withvite_ruby (ViteJS):

To handle theundefined method assets while runningrake activeadmin:install, I temporary installedgem 'propshaft'. Later the gem is not needed.

I've added the following snippet to mimic Webpacker methods theconfig/initializers/active_admin.rb:

module ActiveAdminViteJS  def stylesheet_pack_tag(style, **options)    style = 'active_admin.scss' if style == 'active_admin.css'    vite_stylesheet_tag(style, **options)  end  def javascript_pack_tag(script, **options)    vite_javascript_tag(script, **options)  endendActiveAdmin::Views::Pages::Base.include ActiveAdminViteJS

Ensure you haveconfig.use_webpacker = true in the config.

app/frontend/entrypoints/active_admin.js:

import '../jquery'import 'jquery-ui'import 'jquery-ui/ui/widgets/mouse'import '@activeadmin/activeadmin'

app/frontend/entrypoints/active_admin.scss:

@import "@activeadmin/activeadmin/src/scss/mixins";@import "@activeadmin/activeadmin/src/scss/base";

app/frontend/jquery.js:

import $ from 'jquery'window.jQuery = window.$ = $

No issues so far, but I guess jquery-ui components would require explicit importing

You must be logged in to vote
2 replies
@jhirn
Comment options

This was super helpful! I also had to add this for favicons:

# in ActiveAdmin.configconfig.favicon='images/favicon.png'# In ActiveAdminViteJSdeffavicon_pack_tag(favicon)favicon_link_tag(vite_asset_path(favicon))end
@danieldiekmeier
Comment options

Thank you, this really helped! However, I got an error:

NoMethodError:  undefined method `stylesheet_pack_tag' for an instance of #<Class:0x00007f414a177770>  /home/runner/work/flipvinyls/flipvinyls/vendor/bundle/ruby/3.3.0/gems/activeadmin-3.2.1/app/views/layouts/active_admin_logged_out.html.erb:10

I think this is running in another context because it is used for Devise. I was able to fix it by patching theActiveAdmin::ViewHelpers instead. With this, all my pages work.

- ActiveAdmin::Views::Pages::Base.include ActiveAdminViteJS+ ActiveAdmin::ViewHelpers.include ActiveAdminViteJS
Comment options

Hi guys, sorry to resurrect this discussion but while this solution works like a charminside Active Admin, I can't seem to get the sign in page working. I keep getting the following error:

ActionView::Template::Error (Shakapacker can't find active_admin.css in /Users/.../public/packs/manifest.json. Possible causes:...Your manifest contains:{}

I already removed most of the webpacker files and configs, but I can't seem to find the place to make this particular page working, which means I can't log in to Active Admin at all.

Any help would be greatly appreciated!

You must be logged in to vote
0 replies
Comment options

Hi all, I've been back to this url several times in the last week, I wanted to report on my progress and what i think is a tenable solution for my needs. I started with the snippets from@guillaumebesseup there☝️ . This was perfect to get me css / scss compiling and get me on the right track but as soon as i added inline forms that required javascript (has_many, nested attributes) is when I noticed the javascript was not getting built.

active_admin.js

import"@activeadmin/activeadmin";

issue: web conole reports

Uncaught SyntaxError: Cannot use import statement outside a module

solution (I think): declare the build to be a module inpackage.json

"module":"true","dependencies": {"@activeadmin/activeadmin":"^3.0.0",...

Issue: jquery is not defined:
solution:import statements hoist variables so you have to force jquery to be defined and added to the window in a separate file

app/javascripts/add_jquery.js

importjqueryfrom'jquery';window.jquery=jquery;// i mean why not?!window.jQuery=jquery;// jquery ui is looking for this onewindow.$=jquery;// activeadmin wants this one

application.js

import"./add_jquery"// forces hoisting to behaveimport'./jquery-ui'// jQuery is now definedimport"@activeadmin/activeadmin";

Final issue, jQuery UI when loaded from node_modules has a ton of load dependencies outside the initial widget.js, as you can see from the above import, I vendored jquery-ui instead of using it from the node modules. you can download the full version from the website. I finally came acrossthis video which ended up being really useful to get that final step.

% tree app/javascript app/javascript├── active_admin.js├── add_jquery.js├── application.js├── controllers│   ├── application.js│   ├── hello_controller.js│   └── index.js└── jquery-ui.js

Like often, javascript has taken hours off of my life, but I'm super grateful to not be in the business of building forms, so thanks ActiveAdmin folks. Hopefully this helps streamline the process for others on Rails 7 and esbuild.

You must be logged in to vote
7 replies
@mario-amazing
Comment options

I've successfully configured ActiveAdmin version 3.2 in my Rails project with jsbundling-rails, and propshaft, all managed via node_modules. Here's the setup that worked for me:

//app/javascript/active_admin.jsimport'./add_jquery'import'jquery-ui'import"jquery-ujs"import'jquery-ui/ui/unique-id.js'import'jquery-ui/ui/widgets/mouse'import"jquery-ui/ui/widgets/sortable"import"jquery-ui/ui/widgets/tabs"import"jquery-ui/ui/widgets/dialog"import'jquery-ui/ui/widgets/datepicker'import'jquery-ui/ui/widget'import'@activeadmin/activeadmin'
@jhirn
Comment options

Dang I didn't follow the discussion from the original issue and wasted so much time with vite inject and other configuration trying to get jquery_ui to load properly. I ended up doing exactly this yesterday after losing a day on it.

I believe the reason for this is the order on how Vite transforms CJS imports whentransformMixedEsModules: true is set meaningimport jQuery from 'jquery' will not actually set jQuery before transformingimport 'jQuery-ui which depends on it being set. Moving the jQuery import to another file will load/set jQuery and also do your global assignments towindow.$ andwindow.jQuery if you have them.

@javierjulio
Comment options

This should work with jsbundling-rails and our NPM package as is. I think the issue may be that we aren't specifying the ESM file in a"module": "..." item in thepackage.json file. Can someone please confirm if removing the manual imports and just usingimport '@activeadmin/activeadmin/src/base' works for you? I'm pretty certain we just need to add that like other packages do,for example, the ActionText gem package.json file as it should work without having to manually specify dependency imports.

@javierjulio
Comment options

It doesn't seem we are building a proper ESM version for v3. I'll try to see if we canimplement what ActionView did for rails-ujs for bundling an additional ESM file with our rollup config.

@javierjulio
Comment options

I have a PR that adds a newapp/assets/javascripts/active_admin/base-esm.js file that should help resolve this so you won't need to import each dependency and file manually. I would appreciateany help in testing this new bundle file.

Comment options

I'm not sure if this is necessarily still an issue for people but i was able to get this resolved with two steps. This seems to be acceptable considering the new propshaft asset model handling:

Roughly: transpile with dartsass-rails. That means running the set up for dartsass-rails and then i added a build step inProcfile.dev foradmin_css

admin_css: bin/rails dartsass:watch

My initializer fordartsass-rails looks like the following:

# config/initializers/dartsass.rbRails.application.config.dartsass.builds = {  "#{Rails.root}/vendor/assets/stylesheets/active_admin.scss" => "#{Rails.root}/vendor/assets/builds/active_admin.css"}

You should do the above to make sure that your application css won't get clobbered by active_admin css. nothing else is really needed, propshaft will deliver the correct asset once it's transpiled for active admin css to use because active admin is using stylesheet_link_tag i believe

Then in the active_admin initializer you need to add this configuration:

 # clear any previous assets  config.clear_javascripts!  # We need jQuery and jQuery UI and jQuery-ujs which are dependencies  config.register_javascript "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.js"  config.register_javascript "https://cdn.jsdelivr.net/npm/jquery-ujs@1.2.3/src/rails.min.js"  config.register_javascript "https://cdn.jsdelivr.net/npm/jquery-ui@1.13.2/dist/jquery-ui.min.js"  # The actual active admin javascript file  config.register_javascript "https://cdn.jsdelivr.net/npm/@activeadmin/activeadmin@2.13.1/app/assets/javascripts/active_admin/base.min.js"

I had trouble actually figuring out the javascript. I tried esbuild to bundle the smaller files but that was leading to some other issues with imports and what not and seemed like a rabbit hole. Credit for the javascript goes to Henrik on medium:https://henrikbjorn.medium.com/til-using-activeadmin-with-importmap-rails-289cde6f76b8

He also has a section on importmaps but i don't fully understand importmaps yet so ...

anyways hope the above is helpful especially for people integrating activeadmin with rails 8. This seems to fully work, at least I tested one delete action and was able to successfully delete a resource on the admin backend.

You must be logged in to vote
0 replies
Comment options

tldr: To use Active Admin v3 with Propshaft, puthttps://gist.github.com/losnikitos/46b4fb8c63bfd14577299611ec90f61b intoapp/assets/javascripts/active_admin.js (quick and dirty solution)


I had a problem with missing js bundle after migrating from Sprockets to Propshaft (Rails 7, Active Admin v3). Myactive_admin.js file was basically this one line

//= require active_admin/base

Which Sprockets would have picked up and assembled a bundle, but Propshaft wouldn't.

What I did was 'quick and dirty' solution: I assembled a bundle with sprockets (rails assets:precompile), grabbedpublic/assets/active_admin-{digest}.js and put the contents intoapp/assets/javascripts/active_admin.js. This would work if you don't plan to update active admin very often. Here's what the bundle looks for me (build for active admin v3.2.2)https://gist.github.com/losnikitos/46b4fb8c63bfd14577299611ec90f61b

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Help
Labels
None yet
15 participants
@guillaumebesse@j-manu@jedschneider@jhirn@javierjulio@Kukunin@tagliala@Aesthetikx@danieldiekmeier@losnikitos@mario-amazing@JuzerShakir@reinvanimschoot@aaoafk@alexplatteeuw
Converted from issue

This discussion was converted from issue #7264 on May 11, 2023 02:39.


[8]ページ先頭

©2009-2025 Movatter.jp