-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve errors blocking usage in a mountable Rails engine (#1694)
* Run binstubs based on Rails.root This changes the assumption that these tasks are being run in the rails root to explicitly determine the binstub directory based on the Rails root. This fixes the issue for when the tasks are being run from a mounted dummy app as part of a Rails engine. Given a fresh Rails plugin: `rails plugin new rails-plugin --mountable` that has webpacker gem installed. Before this change running running `app:webpacker:check_binstubs` would check in `./bin` for webpack binstubs whereas they are actually expected in `test/dummy/bin`. After this change the binstubs for the rails app in `test/dummy/bin` are checked as expected. * Allow dependant tasks to run under a prefix If you have a scenario where a Rails apps tasks are placed under a namespace these tasks that invoke other tasks don't work. The common scenario where this happens is when a Rails engine is mounted in a dummy app as per: https://github.com/rails/rails/blob/master/railties/lib/rails/tasks/engine.rake#L4-L17 This change resolves the issues where you receive an error of task not found when running an app command e.g. app:assets:precompile. If we are able to know a task dependency prior to runtime we don't have to do anything complex as per the change for `clobber` since it being defined outside a block does not require a namespace. However for the tasks that we need to perform actions at runtime we have to work out the prefix of the task that was executed to then re-use this on the tasks we want to execute. * Explicitly require Rails::Engine When this gem is running inside a Rails engine to do a task such as `app:assets:precompile` an error is thrown: ``` ➜ rails-plugin git:(master) ✗ rake app:assets:precompile Compiling… Compilation failed: /Users/kevindew/dev/webpacker/lib/webpacker/railtie.rb:6:in `<top (required)>': uninitialized constant Rails::Engine (NameError) from /Users/kevindew/dev/webpacker/lib/webpacker.rb:38:in `require' from /Users/kevindew/dev/webpacker/lib/webpacker.rb:38:in `<top (required)>' from /Users/kevindew/dev/rails-plugin/test/dummy/bin/webpack:13:in `require' from /Users/kevindew/dev/rails-plugin/test/dummy/bin/webpack:13:in `<main>' ``` Explicitly requiring rails/engine resolves this. * Run yarn tasks within the Rails root directory This resolves problems that occur when the expectation is that the Rails.root is the same as the working directory of the rakefile. A common situation for this is when you have a mounted Rails engine in the test/dummy app to run your engine. This changes the yarn commands to run in the directory where you would have the package.json for the rails app that would be used for mounting rather than the root of the engine. And the node_modules are also installed within that directory so they are in the expected place for tasks such as app:assets:precompile. * Update the gitignore within the rails root The contents of the gitignore only really make sense for the root directory of a Rails application. If we are dealing with a Rails engine that mounts the engine onto a test one for usage then it does not make sense to update the root .gitignore and if someone does have one in the `test/dummy` directory then that one should be updated instead. * Run webpacker:info in Rails.root This is resolution to problems that occur with a mounted Rails engine. With one of these you would want to run `rake app:webpacker:info` and have it run against the Rails app you'd be mounting in (normally in test/dummy) however it would incorrectly run in the root directory - where you probably don't want to have webpacker and webpacker-dev-server installed. * Show a relative path when describing configuration missing For a mounted Rails engine we need the config to be in the application mount directory e.g. test/dummy/config/webpacker.yml By running this based on pwd we get this in a Rails engine app: ``` ➜ rails-plugin rake app:webpacker:verify_install RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment Configuration test/dummy/config/webpacker.yml file not found. Make sure webpacker:install is run successfully before running dependent tasks ``` Whereas in a normal Rails installation it outputs unchanged: ``` ➜ full-app git:(master) ✗ rake webpacker:verify_install RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment Configuration config/webpacker.yml file not found. Make sure webpacker:install is run successfully before running dependent tasks ``` * Basic tests for a mounted application This sets up a barebones mounted Rails engine so that tests can be performed against it. This follows a similar approach to the other rake tasks test and thus doesn't intend to be too exhaustive in what is testing and is mostly a cursory test that one of the tasks does perform differently in a mounted Rails engine context. Co-authored-by: Gaurav Tiwari <[email protected]>
- Loading branch information
1 parent
ac93e12
commit 741b314
Showing
28 changed files
with
242 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/.bundle | ||
/pkg | ||
/test/mounted_app/test/dummy/log | ||
/test/test_app/log | ||
node_modules | ||
.byebug_history | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,9 @@ | |
copy_file "#{__dir__}/examples/coffee/hello_coffee.coffee", | ||
"#{Webpacker.config.source_entry_path}/hello_coffee.coffee" | ||
|
||
say "Installing all Coffeescript dependencies" | ||
run "yarn add [email protected] coffee-loader" | ||
Dir.chdir(Rails.root) do | ||
say "Installing all Coffeescript dependencies" | ||
run "yarn add [email protected] coffee-loader" | ||
end | ||
|
||
say "Webpacker now supports Coffeescript 🎉", :green |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
binstubs_template_path = File.expand_path("../../install/binstubs.rb", __dir__).freeze | ||
bin_path = ENV["BUNDLE_BIN"] || "./bin" | ||
bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin") | ||
|
||
namespace :webpacker do | ||
desc "Installs Webpacker binstubs in this application" | ||
task binstubs: [:check_node, :check_yarn] do | ||
task binstubs: [:check_node, :check_yarn] do |task| | ||
prefix = task.name.split(/#|webpacker:binstubs/).first | ||
|
||
if Rails::VERSION::MAJOR >= 5 | ||
exec "#{RbConfig.ruby} #{bin_path}/rails app:template LOCATION=#{binstubs_template_path}" | ||
exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION=#{binstubs_template_path}" | ||
else | ||
exec "#{RbConfig.ruby} #{bin_path}/rake rails:template LOCATION=#{binstubs_template_path}" | ||
exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION=#{binstubs_template_path}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
install_template_path = File.expand_path("../../install/template.rb", __dir__).freeze | ||
bin_path = ENV["BUNDLE_BIN"] || "./bin" | ||
bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin") | ||
|
||
namespace :webpacker do | ||
desc "Install Webpacker in this application" | ||
task install: [:check_node, :check_yarn] do | ||
task install: [:check_node, :check_yarn] do |task| | ||
prefix = task.name.split(/#|webpacker:install/).first | ||
|
||
if Rails::VERSION::MAJOR >= 5 | ||
exec "#{RbConfig.ruby} #{bin_path}/rails app:template LOCATION=#{install_template_path}" | ||
exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION=#{install_template_path}" | ||
else | ||
exec "#{RbConfig.ruby} #{bin_path}/rake rails:template LOCATION=#{install_template_path}" | ||
exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION=#{install_template_path}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "test_helper" | ||
|
||
class EngineRakeTasksTest < Minitest::Test | ||
def setup | ||
remove_webpack_binstubs | ||
end | ||
|
||
def teardown | ||
remove_webpack_binstubs | ||
end | ||
|
||
def test_task_mounted | ||
output = Dir.chdir(mounted_app_path) { `rake -T` } | ||
assert_includes output, "app:webpacker" | ||
end | ||
|
||
def test_binstubs | ||
Dir.chdir(mounted_app_path) { `bundle exec rake app:webpacker:binstubs` } | ||
webpack_binstub_paths.each { |path| assert File.exist?(path) } | ||
end | ||
|
||
private | ||
def mounted_app_path | ||
File.expand_path("mounted_app", __dir__) | ||
end | ||
|
||
def webpack_binstub_paths | ||
[ | ||
"#{mounted_app_path}/test/dummy/bin/webpack", | ||
"#{mounted_app_path}/test/dummy/bin/webpack-dev-server", | ||
] | ||
end | ||
|
||
def remove_webpack_binstubs | ||
webpack_binstub_paths.each do |path| | ||
File.delete(path) if File.exist?(path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require "bundler/setup" | ||
|
||
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__) | ||
load "rails/tasks/engine.rake" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require_relative "config/application" | ||
|
||
Rails.application.load_tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env ruby | ||
APP_PATH = File.expand_path("../config/application", __dir__) | ||
require "rails/commands" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env ruby | ||
require "rake" | ||
Rake.application.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This file allows the `Rails.root` to be correctly determined. | ||
|
||
require_relative "config/environment" | ||
|
||
run Rails.application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "action_controller/railtie" | ||
require "action_view/railtie" | ||
require "webpacker" | ||
|
||
module TestDummyApp | ||
class Application < Rails::Application | ||
config.secret_key_base = "abcdef" | ||
config.eager_load = true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require_relative "application" | ||
|
||
Rails.application.initialize! |
Oops, something went wrong.