Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpacker support #3414

Merged
merged 9 commits into from
Dec 11, 2021
Merged

Webpacker support #3414

merged 9 commits into from
Dec 11, 2021

Conversation

mshibuya
Copy link
Member

Closes #3277

@mshibuya mshibuya force-pushed the webpacker branch 2 times, most recently from ffa7d8a to b383d35 Compare November 14, 2021 13:46
@coveralls
Copy link

Pull Request Test Coverage Report for Build 1459001873

  • 0 of 4 (100.0%) changed or added relevant lines in 2 files are covered.
  • 4 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-1.8%) to 94.081%

Files with Coverage Reduction New Missed Lines %
app/controllers/rails_admin/application_controller.rb 4 88.89%
Totals Coverage Status
Change from base Build 1415561078: -1.8%
Covered Lines: 3354
Relevant Lines: 3565

💛 - Coveralls

@coveralls
Copy link

coveralls commented Nov 14, 2021

Pull Request Test Coverage Report for Build 1538205393

  • 45 of 56 (80.36%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 95.987%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/rails_admin/support/esmodule_preprocessor.rb 15 16 93.75%
app/helpers/rails_admin/application_helper.rb 2 5 40.0%
lib/generators/rails_admin/install_generator.rb 21 28 75.0%
Totals Coverage Status
Change from base Build 1533376790: 0.1%
Covered Lines: 3636
Relevant Lines: 3788

💛 - Coveralls

@mshibuya mshibuya force-pushed the webpacker branch 3 times, most recently from 612cd6f to ee102ce Compare November 20, 2021 10:23
@TheBrockEllis
Copy link

I realize that this is a draft PR but I want to share my experience trying to install rails_admin from this branch. I ran into issues with the location of my javascript. In my project, I renamed the location of my scripts folders to app/frontend/packs/ based on some popular Rails 6 tutorials. I got the following error when trying to access /admin:

 Field 'browser' doesn't contain a valid alias configuration
              /path/to/rails/project/app/frontend/js/rails_admin/rails_admin.jpg doesn't exist
            as directory
              /path/to/rails/project/app/frontend/js/rails_admin/rails_admin doesn't exist

@mshibuya
Copy link
Member Author

@TheBrockEllis Thanks for trying this out, looks like a configuration issue. If you could provide me the source code with relevant configuration I can take a look.

@mshibuya mshibuya marked this pull request as ready for review November 28, 2021 09:44
@mshibuya
Copy link
Member Author

@codealchemy @jdufresne
This is going to be a big change, do you guys have time to take a look?
Any suggestions and questions will be appreciated.

Copy link
Member

@jdufresne jdufresne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! This is an exciting change.

- name: Run check
run: npx prettier --check .
run: ./node_modules/.bin/prettier --check .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe yarn will execute scripts from node_modules/.bin

Suggested change
run: ./node_modules/.bin/prettier --check .
run: yarn prettier --check .

@@ -12,35 +12,48 @@ jobs:
gemfile: [gemfiles/rails_6.1.gemfile]
orm: [active_record]
adapter: [sqlite3]
asset: [sprockets]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why aren't other asset pipelines defined here? Don't we want to verify webpack/webpacker in all scenarios?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go this way,

ruby: [2.6, 2.7, 3.0, jruby]
asset: [sprockets, webpacker]

we'll get every combination of 2.6/sprockets, 2.6/webpacker, 2.7/sprockets, ... and so on. That's kind of redundant, since in general Ruby version will not affect how JavaScript assets are executed.

So I didn't add webpacker in the global matrix, and added it individually like this to save CI execution time and computer resource.

        include:
          ...
          - ruby: 3.0
            gemfile: gemfiles/rails_6.1.gemfile
            orm: active_record
            adapter: postgresql
            asset: webpacker
          - ruby: 3.0
            gemfile: gemfiles/rails_7.0.gemfile
            orm: active_record
            adapter: sqlite3
            asset: webpacker

@@ -195,5 +195,17 @@ def flash_alert_class(flash_key)
else "alert-#{flash_key}"
end
end

def handle_asset_dependency_error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the existing exception insufficient or confusing? I'm thinking maybe we don't need this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gives an exception like this:

LoadError in RailsAdmin::MainController#dashboard
cannot load such file -- sassc

not so confusing, but we're dropping existing sassc dependency from our gem so it would be better to be friendly for existing users to instruct what's happening after their gem upgrade.

<% when :webpack, :sprockets %>
<% handle_asset_dependency_error do %>
<%= stylesheet_link_tag "rails_admin.css", media: :all %>
<%= javascript_include_tag "rails_admin.js", defer: true %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use async rather than defer.

Can we also apply the aync attribute to the webpacker file?

gem "database_cleaner-active_record", ">= 2.0", require: false
gem "database_cleaner-mongoid", ">= 2.0", require: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these unrelated ordering changes? Can they be reverted or moved to a separete PR so this remains focused on just webpack and assets?

],
"main": "src/rails_admin/base.js",
"scripts": {
"link": "yarn link && cd spec/dummy_app && yarn link rails_admin",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this link script get used? I don't see it and am not familiar with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is useful for working with webpack-built assets. After your run yarn run link a symlink is created under dummy_app's node_modules so the changes made to the files under src dir will be immediately picked up by webpack-dev-server.
https://classic.yarnpkg.com/en/docs/cli/link/

@@ -0,0 +1,10872 @@
/*!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought here, could import from node_modules to avoid vendoring?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #3414 (comment).

- "spec/dummy_app/bin/**/*"
- "spec/dummy_app/db/schema.rb"
- "spec/dummy_app/tmp/**/*"
- "vendor/bundle/**/*"
NewCops: disable
SuggestExtensions: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this silencing? Maybe it would be better to install the suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Showing the suggestion everytime Rubocop is run is pointless. I run it so many times during development.
You can install those extensions if you want to, regardless of the suggestion.

app/assets/stylesheets/rails_admin/aristo
app/assets/stylesheets/rails_admin/themes/cerulean
coverage
lib/generators/rails_admin/templates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignore this directory? Sems like this should be formatted the same as any other file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's reason for it...

Some of files contained here (specifically speaking, they're environment.js and webpack.config.js) are need to be merged with existing ones which are created by Webpacker or Rails. And if a user already has some configurations, they need to be merged manually with the changes RailsAdmin need.

If we apply prettier to our templates and run RailsAdmin installer, what users get is like this:

% rails g rails_admin:install
...
    conflict  webpack.config.js
Overwrite /path/to/webpack.config.js? (enter "h" for help) [Ynaqdhm] d
--- /path/to/webpack.config.js 2021-12-04 13:53:59.000000000 +0900
+++ /path/to/webpack.config.js20211204-51180-i5ghjr    2021-12-04 13:54:22.000000000 +0900
@@ -1,18 +1,32 @@
-const path    = require("path")
-const webpack = require('webpack')
+const path = require("path");
+const webpack = require("webpack");
+const MiniCssExtractPlugin = require("mini-css-extract-plugin");
...

those diffs are unnecessary if we don't apply prettier. Then it looks like:

    conflict  webpack.config.js
Overwrite /path/to/webpack.config.js? (enter "h" for help) [Ynaqdhm] d
--- /path/to/webpack.config.js 2021-12-04 13:53:59.000000000 +0900
+++ /path/to/webpack.config.js20211204-51508-jf8k2a    2021-12-04 13:58:12.000000000 +0900
@@ -1,18 +1,32 @@
 const path    = require("path")
 const webpack = require('webpack')
+const MiniCssExtractPlugin = require("mini-css-extract-plugin")
...

much cleaner.
My intention is enabling users to install RailsAdmin with less work, instead of applying prettier to templates to get feeling of tidiness. Users can choose to apply prettier to their source code anyway, without hustle.

@@ -86,6 +86,9 @@ class << self
attr_accessor :navigation_static_links
attr_accessor :navigation_static_label

# Set where RailsAdmin fetches JS/CSS from, defaults to :sprockets
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is targeting a new major release, any thought on defaulting to webpacker? Upstream Rails recommends webpack for new projects and describes sprockets as "legacy": https://edgeguides.rubyonrails.org/webpacker.html

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to ensure compatibility with existing applications. Since using :webpacker as default value for asset_source doesn't automatically make those applications Webpacker-ready, we need to provide migration path for users.

In this way, users can proceed like this:

  • Upgrade RailsAdmin, keeping the app functional since RailsAdmin delivers assets using Sprockets by default
  • Make their app Webpacker-ready
  • When things are ready, they can configure RailsAdmin to use Webpacker-sourced assets

- Use `yarn run`, instead of `./node_modules/.bin/`
- Remove unnecessary .prettierignore entries
- Address Rubocop Performance/RegexpMatch offense in the PR build
- Apply async: true to javascript tags
- Restore order changes in gemfiles/*
- Change scripts task `prettier` to `format`, since it conflicts with the prettier command itself
- Apply prettier to src/rails_admin/styles/themes/cerulean
@mshibuya
Copy link
Member Author

mshibuya commented Dec 4, 2021

@jdufresne Addressed all review suggestions, please take a look again 🙏

@mshibuya
Copy link
Member Author

Let me merge this for now, since it's blocking other works.
If you have any comments please leave them here, I will work on them later 🙏

mshibuya added a commit that referenced this pull request Feb 27, 2022
It was introduced by #3414, but is not fully functional yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Webpacker only applications
4 participants