From 174dd8e40393fb8c620fc42b2b956810bde8945b Mon Sep 17 00:00:00 2001 From: sgara Date: Fri, 13 Sep 2019 15:05:39 +0200 Subject: [PATCH 1/5] Add simple config flag to switch to webpacker assets --- CHANGELOG.md | 5 + .../layouts/active_admin_logged_out.html.erb | 12 +- gemfiles/rails_60_webpacker/Gemfile | 39 ++ gemfiles/rails_60_webpacker/Gemfile.lock | 359 ++++++++++++++++++ lib/active_admin/namespace_settings.rb | 3 + lib/active_admin/views/pages/base.rb | 6 +- spec/support/rails_template.rb | 11 + 7 files changed, 431 insertions(+), 4 deletions(-) create mode 100644 gemfiles/rails_60_webpacker/Gemfile create mode 100644 gemfiles/rails_60_webpacker/Gemfile.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index 32073f0aff3..f61bd147304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancements + +* Add webpacker compatibility with config switch and installation generator. [#5855] by [@sgara] + ## 2.6.0 [☰](https://github.com/activeadmin/activeadmin/compare/v2.5.0..v2.6.0) ### Enhacements @@ -541,6 +545,7 @@ Please check [0-6-stable] for previous changes. [#5946]: https://github.com/activeadmin/activeadmin/pull/5946 [#5956]: https://github.com/activeadmin/activeadmin/pull/5956 [#5957]: https://github.com/activeadmin/activeadmin/pull/5957 +[#5855]: https://github.com/activeadmin/activeadmin/pull/5855 [@5t111111]: https://github.com/5t111111 [@aarek]: https://github.com/aarek diff --git a/app/views/layouts/active_admin_logged_out.html.erb b/app/views/layouts/active_admin_logged_out.html.erb index b661c67004a..423213d5e66 100644 --- a/app/views/layouts/active_admin_logged_out.html.erb +++ b/app/views/layouts/active_admin_logged_out.html.erb @@ -6,10 +6,18 @@ <%= [@page_title, ActiveAdmin.application.site_title(self)].compact.join(" | ") %> <% ActiveAdmin.application.stylesheets.each do |style, options| %> - <%= stylesheet_link_tag style, options %> + <% if ActiveAdmin.application.use_webpacker %> + <%= stylesheet_pack_tag style, options %> + <% else %> + <%= stylesheet_link_tag style, options %> + <% end %> <% end %> <% ActiveAdmin.application.javascripts.each do |path| %> - <%= javascript_include_tag path %> + <% if ActiveAdmin.application.use_webpacker %> + <%= javascript_pack_tag path %> + <% else %> + <%= javascript_include_tag path %> + <% end %> <% end %> <%= favicon_link_tag ActiveAdmin.application.favicon if ActiveAdmin.application.favicon %> diff --git a/gemfiles/rails_60_webpacker/Gemfile b/gemfiles/rails_60_webpacker/Gemfile new file mode 100644 index 00000000000..ba0cc7a4e1a --- /dev/null +++ b/gemfiles/rails_60_webpacker/Gemfile @@ -0,0 +1,39 @@ +source "https://rubygems.org" + +group :development, :test do + gem 'rake' + gem 'pry' # Easily debug from your console with `binding.pry` + gem 'pry-byebug', platform: :mri # Step-by-step debugging + + gem 'cancancan' + gem 'pundit' + gem 'jruby-openssl', '~> 0.10.1', platform: :jruby + + gem 'draper', '~> 3.1' + gem "devise", "~> 4.7" + + gem "rails", "~> 6.0.0" + gem "activerecord-jdbcsqlite3-adapter", "~> 60.0.rc1", platform: :jruby + + gem "webpacker", "~> 4.0" +end + +group :test do + gem 'apparition' + gem 'capybara', '~> 3.14' + gem 'db-query-matchers', '0.10.0' + + gem 'simplecov', '0.17.1', require: false # Test coverage generator. Go to /coverage/ after running tests + gem 'cucumber-rails', '~> 2.0', require: false + gem 'cucumber' + gem 'database_cleaner' + gem 'jasmine' + gem 'jasmine-core', '2.99.2' # last release with Ruby 2.2 support. + gem 'launchy' + gem 'parallel_tests', '~> 2.26' + gem 'rails-i18n' # Provides default i18n for many languages + gem 'rspec-rails' + gem "sqlite3", "~> 1.4", platform: :mri +end + +gemspec path: "../.." diff --git a/gemfiles/rails_60_webpacker/Gemfile.lock b/gemfiles/rails_60_webpacker/Gemfile.lock new file mode 100644 index 00000000000..3a47f33fb32 --- /dev/null +++ b/gemfiles/rails_60_webpacker/Gemfile.lock @@ -0,0 +1,359 @@ +PATH + remote: ../.. + specs: + activeadmin (2.6.0) + arbre (~> 1.2, >= 1.2.1) + formtastic (~> 3.1) + formtastic_i18n (~> 0.4) + inherited_resources (~> 1.7) + jquery-rails (~> 4.2) + kaminari (~> 1.0, >= 1.0.1) + railties (>= 5.2, < 6.1) + ransack (~> 2.1, >= 2.1.1) + sassc-rails (~> 2.1) + sprockets (>= 3.0, < 4.1) + +GEM + remote: https://rubygems.org/ + specs: + actioncable (6.0.1) + actionpack (= 6.0.1) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailbox (6.0.1) + actionpack (= 6.0.1) + activejob (= 6.0.1) + activerecord (= 6.0.1) + activestorage (= 6.0.1) + activesupport (= 6.0.1) + mail (>= 2.7.1) + actionmailer (6.0.1) + actionpack (= 6.0.1) + actionview (= 6.0.1) + activejob (= 6.0.1) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (6.0.1) + actionview (= 6.0.1) + activesupport (= 6.0.1) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (6.0.1) + actionpack (= 6.0.1) + activerecord (= 6.0.1) + activestorage (= 6.0.1) + activesupport (= 6.0.1) + nokogiri (>= 1.8.5) + actionview (6.0.1) + activesupport (= 6.0.1) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (6.0.1) + activesupport (= 6.0.1) + globalid (>= 0.3.6) + activemodel (6.0.1) + activesupport (= 6.0.1) + activemodel-serializers-xml (1.0.2) + activemodel (> 5.x) + activesupport (> 5.x) + builder (~> 3.1) + activerecord (6.0.1) + activemodel (= 6.0.1) + activesupport (= 6.0.1) + activestorage (6.0.1) + actionpack (= 6.0.1) + activejob (= 6.0.1) + activerecord (= 6.0.1) + marcel (~> 0.3.1) + activesupport (6.0.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + zeitwerk (~> 2.2) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + apparition (0.4.0) + capybara (~> 3.13, < 4) + websocket-driver (>= 0.6.5) + arbre (1.2.1) + activesupport (>= 3.0.0) + backports (3.15.0) + bcrypt (3.1.13) + builder (3.2.3) + byebug (11.0.1) + cancancan (3.0.1) + capybara (3.29.0) + addressable + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (~> 1.5) + xpath (~> 3.2) + coderay (1.1.2) + concurrent-ruby (1.1.5) + crass (1.0.5) + cucumber (3.1.2) + builder (>= 2.1.2) + cucumber-core (~> 3.2.0) + cucumber-expressions (~> 6.0.1) + cucumber-wire (~> 0.0.1) + diff-lcs (~> 1.3) + gherkin (~> 5.1.0) + multi_json (>= 1.7.5, < 2.0) + multi_test (>= 0.1.2) + cucumber-core (3.2.1) + backports (>= 3.8.0) + cucumber-tag_expressions (~> 1.1.0) + gherkin (~> 5.0) + cucumber-expressions (6.0.1) + cucumber-rails (2.0.0) + capybara (>= 2.12, < 4) + cucumber (>= 3.0.2, < 4) + mime-types (>= 2.0, < 4) + nokogiri (~> 1.8) + railties (>= 4.2, < 7) + cucumber-tag_expressions (1.1.1) + cucumber-wire (0.0.1) + database_cleaner (1.7.0) + db-query-matchers (0.10.0) + activesupport (>= 4.0, < 7) + rspec (~> 3.0) + devise (4.7.1) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) + diff-lcs (1.3) + docile (1.3.2) + draper (3.1.0) + actionpack (>= 5.0) + activemodel (>= 5.0) + activemodel-serializers-xml (>= 1.0) + activesupport (>= 5.0) + request_store (>= 1.0) + erubi (1.9.0) + ffi (1.11.3) + formtastic (3.1.5) + actionpack (>= 3.2.13) + formtastic_i18n (0.6.0) + gherkin (5.1.0) + globalid (0.4.2) + activesupport (>= 4.2.0) + has_scope (0.7.2) + actionpack (>= 4.1) + activesupport (>= 4.1) + i18n (1.7.0) + concurrent-ruby (~> 1.0) + inherited_resources (1.11.0) + actionpack (>= 5.0, < 6.1) + has_scope (~> 0.6) + railties (>= 5.0, < 6.1) + responders (>= 2, < 4) + jasmine (2.99.0) + jasmine-core (>= 2.99.0, < 3.0.0) + phantomjs + rack (>= 1.2.1) + rake + jasmine-core (2.99.2) + jquery-rails (4.3.5) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + json (2.2.0) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) + launchy (2.4.3) + addressable (~> 2.3) + loofah (2.3.1) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + marcel (0.3.3) + mimemagic (~> 0.3.2) + method_source (0.9.2) + mime-types (3.3) + mime-types-data (~> 3.2015) + mime-types-data (3.2019.1009) + mimemagic (0.3.3) + mini_mime (1.0.2) + mini_portile2 (2.4.0) + minitest (5.13.0) + multi_json (1.14.1) + multi_test (0.1.2) + nio4r (2.5.2) + nokogiri (1.10.5) + mini_portile2 (~> 2.4.0) + orm_adapter (0.5.0) + parallel (1.19.0) + parallel_tests (2.29.2) + parallel + phantomjs (2.1.1.0) + polyamorous (2.3.0) + activerecord (>= 5.0) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-byebug (3.7.0) + byebug (~> 11.0) + pry (~> 0.10) + public_suffix (4.0.1) + pundit (2.1.0) + activesupport (>= 3.0.0) + rack (2.0.7) + rack-proxy (0.6.5) + rack + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails (6.0.1) + actioncable (= 6.0.1) + actionmailbox (= 6.0.1) + actionmailer (= 6.0.1) + actionpack (= 6.0.1) + actiontext (= 6.0.1) + actionview (= 6.0.1) + activejob (= 6.0.1) + activemodel (= 6.0.1) + activerecord (= 6.0.1) + activestorage (= 6.0.1) + activesupport (= 6.0.1) + bundler (>= 1.3.0) + railties (= 6.0.1) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + rails-i18n (6.0.0) + i18n (>= 0.7, < 2) + railties (>= 6.0.0, < 7) + railties (6.0.1) + actionpack (= 6.0.1) + activesupport (= 6.0.1) + method_source + rake (>= 0.8.7) + thor (>= 0.20.3, < 2.0) + rake (13.0.1) + ransack (2.3.0) + actionpack (>= 5.0) + activerecord (>= 5.0) + activesupport (>= 5.0) + i18n + polyamorous (= 2.3.0) + regexp_parser (1.6.0) + request_store (1.4.1) + rack (>= 1.4) + responders (3.0.0) + actionpack (>= 5.0) + railties (>= 5.0) + rspec (3.9.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-core (3.9.0) + rspec-support (~> 3.9.0) + rspec-expectations (3.9.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-rails (3.9.0) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.0) + sassc (2.2.1) + ffi (~> 1.9) + sassc-rails (2.1.2) + railties (>= 4.0.0) + sassc (>= 2.0) + sprockets (> 3.0) + sprockets-rails + tilt + simplecov (0.17.1) + docile (~> 1.1) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) + sprockets (4.0.0) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + sqlite3 (1.4.1) + thor (0.20.3) + thread_safe (0.3.6) + tilt (2.0.10) + tzinfo (1.2.5) + thread_safe (~> 0.1) + warden (1.2.8) + rack (>= 2.0.6) + webpacker (4.2.0) + activesupport (>= 4.2) + rack-proxy (>= 0.6.1) + railties (>= 4.2) + websocket-driver (0.7.1) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.4) + xpath (3.2.0) + nokogiri (~> 1.8) + zeitwerk (2.2.1) + +PLATFORMS + ruby + +DEPENDENCIES + activeadmin! + activerecord-jdbcsqlite3-adapter (~> 60.0.rc1) + apparition + cancancan + capybara (~> 3.14) + cucumber + cucumber-rails (~> 2.0) + database_cleaner + db-query-matchers (= 0.10.0) + devise (~> 4.7) + draper (~> 3.1) + jasmine + jasmine-core (= 2.99.2) + jruby-openssl (~> 0.10.1) + launchy + parallel_tests (~> 2.26) + pry + pry-byebug + pundit + rails (~> 6.0.0) + rails-i18n + rake + rspec-rails + simplecov (= 0.17.1) + sqlite3 (~> 1.4) + webpacker (~> 4.0) + +BUNDLED WITH + 2.0.2 diff --git a/lib/active_admin/namespace_settings.rb b/lib/active_admin/namespace_settings.rb index 9cabc20b76c..61080d2396f 100644 --- a/lib/active_admin/namespace_settings.rb +++ b/lib/active_admin/namespace_settings.rb @@ -119,5 +119,8 @@ class NamespaceSettings < DynamicSettingsNode :email, ] register :filter_method_for_large_association, '_starts_with' + + # Switch between asset pipeline and webpacker assets + register :use_webpacker, false end end diff --git a/lib/active_admin/views/pages/base.rb b/lib/active_admin/views/pages/base.rb index 822a10a94c0..58ac14962e0 100644 --- a/lib/active_admin/views/pages/base.rb +++ b/lib/active_admin/views/pages/base.rb @@ -29,7 +29,8 @@ def build_active_admin_head text_node(active_admin_namespace.head) active_admin_application.stylesheets.each do |style, options| - text_node stylesheet_link_tag(style, options).html_safe + stylesheet_tag = active_admin_namespace.use_webpacker ? stylesheet_pack_tag(style, options) : stylesheet_link_tag(style, options) + text_node(stylesheet_tag.html_safe) if stylesheet_tag end active_admin_namespace.meta_tags.each do |name, content| @@ -37,7 +38,8 @@ def build_active_admin_head end active_admin_application.javascripts.each do |path| - text_node(javascript_include_tag(path)) + javascript_tag = active_admin_namespace.use_webpacker ? javascript_pack_tag(path) : javascript_include_tag(path) + text_node(javascript_tag) end if active_admin_namespace.favicon diff --git a/spec/support/rails_template.rb b/spec/support/rails_template.rb index fdbb57a11bf..a38045c7c90 100644 --- a/spec/support/rails_template.rb +++ b/spec/support/rails_template.rb @@ -79,6 +79,17 @@ append_file 'app/assets/javascripts/active_admin.js', "//= require turbolinks\n" end +# Setup webpacker if necessary +if ENV["BUNDLE_GEMFILE"] == File.expand_path("../../gemfiles/rails_60_webpacker.gemfile", __dir__) + inject_into_file 'config/initializers/active_admin.rb', "\n config.use_webpacker = true\n", before: /^end/ + rake "webpacker:install" + create_file 'app/javascript/packs/active_admin.scss' + create_file 'app/javascript/packs/active_admin/print.scss' + create_file 'app/javascript/packs/active_admin.js' + append_file 'app/javascript/packs/active_admin.js', "import './active_admin.css';" + gsub_file 'config/webpacker.yml', /^.*extract_css.*$/, "\n extract_css: true" +end + if ENV['RAILS_ENV'] != 'test' inject_into_file 'config/routes.rb', "\n root to: redirect('admin')", after: /.*routes.draw do/ end From 0137beb39d70a8d398e23f922158480939bc6986 Mon Sep 17 00:00:00 2001 From: sgara Date: Wed, 13 Nov 2019 18:52:54 +0100 Subject: [PATCH 2/5] Add generator and templates for webpacker configured app --- .../active_admin/install/install_generator.rb | 8 ++++- .../install/templates/active_admin.rb.erb | 7 +++++ .../webpacker/templates/active_admin.js | 19 ++++++++++++ .../webpacker/templates/active_admin.scss | 17 +++++++++++ .../webpacker/templates/print.scss | 2 ++ .../webpacker/webpacker_generator.rb | 15 ++++++++++ spec/support/rails_template.rb | 29 ++++++++++++++----- 7 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 lib/generators/active_admin/webpacker/templates/active_admin.js create mode 100644 lib/generators/active_admin/webpacker/templates/active_admin.scss create mode 100644 lib/generators/active_admin/webpacker/templates/print.scss create mode 100644 lib/generators/active_admin/webpacker/webpacker_generator.rb diff --git a/lib/generators/active_admin/install/install_generator.rb b/lib/generators/active_admin/install/install_generator.rb index aeed6646f58..32a16c7ea8a 100644 --- a/lib/generators/active_admin/install/install_generator.rb +++ b/lib/generators/active_admin/install/install_generator.rb @@ -8,6 +8,7 @@ class InstallGenerator < ActiveRecord::Generators::Base hook_for :users, default: "devise", desc: "Admin user generator to run. Skip with --skip-users" class_option :skip_comments, type: :boolean, default: false, desc: "Skip installation of comments" + class_option :use_webpacker, type: :boolean, default: false, desc: "User webpacker assets instead of sprocket" source_root File.expand_path('templates', __dir__) @@ -15,6 +16,7 @@ def copy_initializer @underscored_user_name = name.underscore.gsub('/', '_') @use_authentication_method = options[:users].present? @skip_comments = options[:skip_comments] + @use_webpacker = options[:use_webpacker] template 'active_admin.rb.erb', 'config/initializers/active_admin.rb' end @@ -36,7 +38,11 @@ def setup_routes end def create_assets - generate "active_admin:assets" + if options[:use_webpacker] + generate "active_admin:webpacker" + else + generate "active_admin:assets" + end end def create_migrations diff --git a/lib/generators/active_admin/install/templates/active_admin.rb.erb b/lib/generators/active_admin/install/templates/active_admin.rb.erb index c7ba68b79e6..5d5a054f8ef 100644 --- a/lib/generators/active_admin/install/templates/active_admin.rb.erb +++ b/lib/generators/active_admin/install/templates/active_admin.rb.erb @@ -325,4 +325,11 @@ ActiveAdmin.setup do |config| # You can inherit it with own class and inject it for all resources # # config.order_clause = MyOrderClause + + # == Webpacker + # + # By default, Active Admin uses sprocket's asset pipeline. + # You can switch to webpacker here. + # + <% unless @use_webpacker %># <% end %>config.use_webpacker = true end diff --git a/lib/generators/active_admin/webpacker/templates/active_admin.js b/lib/generators/active_admin/webpacker/templates/active_admin.js new file mode 100644 index 00000000000..6bc7c8acacf --- /dev/null +++ b/lib/generators/active_admin/webpacker/templates/active_admin.js @@ -0,0 +1,19 @@ +// import 'active_admin/assets/stylesheets/active_admin' +// SASS variable overrides must be declared before loading up Active Admin's styles. +// +// To view the variables that Active Admin provides, take a look at +// `app/assets/stylesheets/active_admin/mixins/_variables.scss` in the +// Active Admin source. +// +// For example, to change the sidebar width: +// $sidebar-width: 242px; + +// Active Admin's got SASS! +import "../stylesheets/active_admin"; + +// Overriding any non-variable SASS must be done after the fact. +// For example, to change the default status-tag color: +// +// .status_tag { background: #6090DB; } + +import "@activeadmin/activeadmin"; diff --git a/lib/generators/active_admin/webpacker/templates/active_admin.scss b/lib/generators/active_admin/webpacker/templates/active_admin.scss new file mode 100644 index 00000000000..d1c7b3bd7a5 --- /dev/null +++ b/lib/generators/active_admin/webpacker/templates/active_admin.scss @@ -0,0 +1,17 @@ +// SASS variable overrides must be declared before loading up Active Admin's styles. +// +// To view the variables that Active Admin provides, take a look at +// `app/assets/stylesheets/active_admin/mixins/_variables.scss` in the +// Active Admin source. +// +// For example, to change the sidebar width: +// $sidebar-width: 242px; + +// Active Admin's got SASS! +@import "~@activeadmin/activeadmin/src/scss/mixins"; +@import "~@activeadmin/activeadmin/src/scss/base"; + +// Overriding any non-variable SASS must be done after the fact. +// For example, to change the default status-tag color: +// +// .status_tag { background: #6090DB; } diff --git a/lib/generators/active_admin/webpacker/templates/print.scss b/lib/generators/active_admin/webpacker/templates/print.scss new file mode 100644 index 00000000000..79ac0361c95 --- /dev/null +++ b/lib/generators/active_admin/webpacker/templates/print.scss @@ -0,0 +1,2 @@ +/* Active Admin Print Stylesheet */ +@import "~@activeadmin/activeadmin/src/scss/print"; diff --git a/lib/generators/active_admin/webpacker/webpacker_generator.rb b/lib/generators/active_admin/webpacker/webpacker_generator.rb new file mode 100644 index 00000000000..7d9f83acbc7 --- /dev/null +++ b/lib/generators/active_admin/webpacker/webpacker_generator.rb @@ -0,0 +1,15 @@ +module ActiveAdmin + module Generators + class WebpackerGenerator < Rails::Generators::Base + + source_root File.expand_path('templates', __dir__) + + def install_assets + template 'active_admin.js', 'app/javascript/packs/active_admin.js' + template "active_admin.scss", "app/javascript/stylesheets/active_admin.scss" + template 'print.scss', 'app/javascript/packs/active_admin/print.scss' + end + + end + end +end diff --git a/spec/support/rails_template.rb b/spec/support/rails_template.rb index a38045c7c90..defdf6d6066 100644 --- a/spec/support/rails_template.rb +++ b/spec/support/rails_template.rb @@ -58,7 +58,7 @@ RUBY # Setup Active Admin -generate 'active_admin:install' +generate "active_admin:install#{" --use-webpacker" if ENV["BUNDLE_GEMFILE"] == File.expand_path("../../gemfiles/rails_60_webpacker/Gemfile", __dir__)}" # Force strong parameters to raise exceptions inject_into_file 'config/application.rb', after: 'class Application < Rails::Application' do @@ -80,14 +80,23 @@ end # Setup webpacker if necessary -if ENV["BUNDLE_GEMFILE"] == File.expand_path("../../gemfiles/rails_60_webpacker.gemfile", __dir__) - inject_into_file 'config/initializers/active_admin.rb', "\n config.use_webpacker = true\n", before: /^end/ +if ENV["BUNDLE_GEMFILE"] == File.expand_path("../../gemfiles/rails_60_webpacker/Gemfile", __dir__) rake "webpacker:install" - create_file 'app/javascript/packs/active_admin.scss' - create_file 'app/javascript/packs/active_admin/print.scss' - create_file 'app/javascript/packs/active_admin.js' - append_file 'app/javascript/packs/active_admin.js', "import './active_admin.css';" - gsub_file 'config/webpacker.yml', /^.*extract_css.*$/, "\n extract_css: true" + gsub_file 'config/webpacker.yml', /^.*extract_css.*$/, <<-YML + extract_css: true + YML + jquery_env = <<-ENV +\nconst webpack = require('webpack') +environment.plugins.prepend('Provide', + new webpack.ProvidePlugin({ + "$":"jquery", + "jQuery":"jquery", + "window.jQuery":"jquery" + }) +) + ENV + inject_into_file 'config/webpack/environment.js', jquery_env, after: "const { environment } = require('@rails/webpacker')" + run "yarn add @activeadmin/activeadmin" end if ENV['RAILS_ENV'] != 'test' @@ -102,5 +111,9 @@ rake "parallel:drop parallel:create parallel:load_schema", env: ENV['RAILS_ENV'] end +# Add both assets to test as some specs still rely on sprockets +rake "active_admin:assets" +rake "active_admin:webpacker" + git add: "." git commit: "-m 'Bare application'" From 297e90e728eecea72bf8b72dd60c8dfc5787cbc7 Mon Sep 17 00:00:00 2001 From: sgara Date: Wed, 4 Dec 2019 15:50:50 +0100 Subject: [PATCH 3/5] Automate jquery env setup --- .../active_admin/webpacker/plugins/jquery.js | 7 +++++++ .../active_admin/webpacker/webpacker_generator.rb | 13 ++++++++++++- spec/support/rails_template.rb | 15 --------------- 3 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 lib/generators/active_admin/webpacker/plugins/jquery.js diff --git a/lib/generators/active_admin/webpacker/plugins/jquery.js b/lib/generators/active_admin/webpacker/plugins/jquery.js new file mode 100644 index 00000000000..54c61ec8353 --- /dev/null +++ b/lib/generators/active_admin/webpacker/plugins/jquery.js @@ -0,0 +1,7 @@ +const webpack = require('webpack') + +module.exports = new webpack.ProvidePlugin({ + "$":"jquery", + "jQuery":"jquery", + "window.jQuery":"jquery" +}); diff --git a/lib/generators/active_admin/webpacker/webpacker_generator.rb b/lib/generators/active_admin/webpacker/webpacker_generator.rb index 7d9f83acbc7..739b9f861ee 100644 --- a/lib/generators/active_admin/webpacker/webpacker_generator.rb +++ b/lib/generators/active_admin/webpacker/webpacker_generator.rb @@ -8,8 +8,19 @@ def install_assets template 'active_admin.js', 'app/javascript/packs/active_admin.js' template "active_admin.scss", "app/javascript/stylesheets/active_admin.scss" template 'print.scss', 'app/javascript/packs/active_admin/print.scss' - end + copy_file "#{__dir__}/plugins/jquery.js", Rails.root.join("config/webpack/plugins/jquery.js").to_s + + insert_into_file Rails.root.join("config/webpack/environment.js").to_s, + "const jquery = require('./plugins/jquery')\n", + after: /require\(('|")@rails\/webpacker\1\);?\n/ + + insert_into_file Rails.root.join("config/webpack/environment.js").to_s, + "environment.plugins.prepend('jquery', jquery)\n", + before: "module.exports" + + run "yarn add @activeadmin/activeadmin" + end end end end diff --git a/spec/support/rails_template.rb b/spec/support/rails_template.rb index defdf6d6066..22449805102 100644 --- a/spec/support/rails_template.rb +++ b/spec/support/rails_template.rb @@ -82,21 +82,6 @@ # Setup webpacker if necessary if ENV["BUNDLE_GEMFILE"] == File.expand_path("../../gemfiles/rails_60_webpacker/Gemfile", __dir__) rake "webpacker:install" - gsub_file 'config/webpacker.yml', /^.*extract_css.*$/, <<-YML - extract_css: true - YML - jquery_env = <<-ENV -\nconst webpack = require('webpack') -environment.plugins.prepend('Provide', - new webpack.ProvidePlugin({ - "$":"jquery", - "jQuery":"jquery", - "window.jQuery":"jquery" - }) -) - ENV - inject_into_file 'config/webpack/environment.js', jquery_env, after: "const { environment } = require('@rails/webpacker')" - run "yarn add @activeadmin/activeadmin" end if ENV['RAILS_ENV'] != 'test' From 74b3a7ddb4321e1dfcdae5ae71e6134a014f7497 Mon Sep 17 00:00:00 2001 From: sgara Date: Wed, 13 Nov 2019 19:13:00 +0100 Subject: [PATCH 4/5] Add installation instructions --- docs/0-installation.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/0-installation.md b/docs/0-installation.md index 64139343b31..0f468c4b2b3 100644 --- a/docs/0-installation.md +++ b/docs/0-installation.md @@ -112,6 +112,30 @@ Draper::CollectionDecorator.send :delegate, :per_page_kaminari If you're getting the error `wrong number of arguments (6 for 4..5)`, [read #2703]. +## webpacker + +Since rails 6.0 webpacker has become the default asset generator. You can opt in using webpacker for ActiveAdmin assets as well by setting `use_webpacker` at install or manually. + +* at active_admin installation: + + ```sh + rails g active_admin:install --use_webpacker + ``` + +* manually: + + ```ruby + ActiveAdmin.setup do |config| + config.use_webpacker = true + end + ``` + + And run the generator to get default Active Admin assets: + + ```sh + rails g active_admin:webpacker + ``` + [CHANGELOG]: https://github.com/activeadmin/activeadmin/blob/master/CHANGELOG.md [dashboard.rb]: https://github.com/activeadmin/activeadmin/blob/master/lib/generators/active_admin/install/templates/dashboard.rb [active_admin.rb]: https://github.com/activeadmin/activeadmin/blob/master/lib/generators/active_admin/install/templates/active_admin.rb.erb From 3e6bfefca7b912e621b63ead3cb8998532b9d2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20A=2E=20Carro=20Dupl=C3=A1?= Date: Fri, 17 Jan 2020 12:25:35 -0500 Subject: [PATCH 5/5] Fix for webpacker issue 2071 --- app/views/layouts/active_admin_logged_out.html.erb | 7 +++---- lib/active_admin/views/pages/base.rb | 8 +++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/views/layouts/active_admin_logged_out.html.erb b/app/views/layouts/active_admin_logged_out.html.erb index 423213d5e66..1492daacd17 100644 --- a/app/views/layouts/active_admin_logged_out.html.erb +++ b/app/views/layouts/active_admin_logged_out.html.erb @@ -5,13 +5,12 @@ <%= [@page_title, ActiveAdmin.application.site_title(self)].compact.join(" | ") %> - <% ActiveAdmin.application.stylesheets.each do |style, options| %> - <% if ActiveAdmin.application.use_webpacker %> - <%= stylesheet_pack_tag style, options %> - <% else %> + <% unless ActiveAdmin.application.use_webpacker %> + <% ActiveAdmin.application.stylesheets.each do |style, options| %> <%= stylesheet_link_tag style, options %> <% end %> <% end %> + <% ActiveAdmin.application.javascripts.each do |path| %> <% if ActiveAdmin.application.use_webpacker %> <%= javascript_pack_tag path %> diff --git a/lib/active_admin/views/pages/base.rb b/lib/active_admin/views/pages/base.rb index 58ac14962e0..8a0425f3726 100644 --- a/lib/active_admin/views/pages/base.rb +++ b/lib/active_admin/views/pages/base.rb @@ -28,9 +28,11 @@ def build_active_admin_head text_node(active_admin_namespace.head) - active_admin_application.stylesheets.each do |style, options| - stylesheet_tag = active_admin_namespace.use_webpacker ? stylesheet_pack_tag(style, options) : stylesheet_link_tag(style, options) - text_node(stylesheet_tag.html_safe) if stylesheet_tag + unless active_admin_namespace.use_webpacker + active_admin_application.stylesheets.each do |style, options| + stylesheet_tag = stylesheet_link_tag(style, options) + text_node(stylesheet_tag.html_safe) if stylesheet_tag + end end active_admin_namespace.meta_tags.each do |name, content|