From 0b33594dfe66472f562368860a906f55826c5f6b Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:01:41 +0100 Subject: [PATCH 01/12] 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. --- lib/tasks/installers.rake | 2 +- lib/tasks/webpacker/binstubs.rake | 2 +- lib/tasks/webpacker/check_binstubs.rake | 2 +- lib/tasks/webpacker/install.rake | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tasks/installers.rake b/lib/tasks/installers.rake index 96b658b27..2bb445d23 100644 --- a/lib/tasks/installers.rake +++ b/lib/tasks/installers.rake @@ -14,7 +14,7 @@ dependencies = { "Angular": [:typescript] } -bin_path = ENV["BUNDLE_BIN"] || "./bin" +bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin") namespace :webpacker do namespace :install do diff --git a/lib/tasks/webpacker/binstubs.rake b/lib/tasks/webpacker/binstubs.rake index 19d71458a..ecc2821c2 100644 --- a/lib/tasks/webpacker/binstubs.rake +++ b/lib/tasks/webpacker/binstubs.rake @@ -1,5 +1,5 @@ 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" diff --git a/lib/tasks/webpacker/check_binstubs.rake b/lib/tasks/webpacker/check_binstubs.rake index 81731940a..17db6b4b2 100644 --- a/lib/tasks/webpacker/check_binstubs.rake +++ b/lib/tasks/webpacker/check_binstubs.rake @@ -1,7 +1,7 @@ namespace :webpacker do desc "Verifies that webpack & webpack-dev-server are present." task :check_binstubs do - unless File.exist?("bin/webpack") + unless File.exist?(Rails.root.join("bin/webpack")) $stderr.puts "webpack binstubs not found.\n"\ "Have you run rails webpacker:install ?\n"\ "Make sure the bin directory or binstubs are not included in .gitignore\n"\ diff --git a/lib/tasks/webpacker/install.rake b/lib/tasks/webpacker/install.rake index a5a78e215..a90a16927 100644 --- a/lib/tasks/webpacker/install.rake +++ b/lib/tasks/webpacker/install.rake @@ -1,5 +1,5 @@ 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" From abb8c8784ee273c805586d41a1eac79e1a5aeec0 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:14:24 +0100 Subject: [PATCH 02/12] 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. --- lib/tasks/installers.rake | 8 +++++--- lib/tasks/webpacker/binstubs.rake | 8 +++++--- lib/tasks/webpacker/clobber.rake | 4 +--- lib/tasks/webpacker/compile.rake | 6 ++++-- lib/tasks/webpacker/install.rake | 8 +++++--- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/tasks/installers.rake b/lib/tasks/installers.rake index 2bb445d23..e88372bfe 100644 --- a/lib/tasks/installers.rake +++ b/lib/tasks/installers.rake @@ -20,13 +20,15 @@ namespace :webpacker do namespace :install do installers.each do |name, task_name| desc "Install everything needed for #{name}" - task task_name => ["webpacker:verify_install"] do + task task_name => ["webpacker:verify_install"] do |task| + prefix = task.name.split(/#|webpacker:install/).first + template = File.expand_path("../install/#{task_name}.rb", __dir__) base_path = if Rails::VERSION::MAJOR >= 5 - "#{RbConfig.ruby} #{bin_path}/rails app:template" + "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template" else - "#{RbConfig.ruby} #{bin_path}/rake rails:template" + "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template" end dependencies[name] ||= [] diff --git a/lib/tasks/webpacker/binstubs.rake b/lib/tasks/webpacker/binstubs.rake index ecc2821c2..645655205 100644 --- a/lib/tasks/webpacker/binstubs.rake +++ b/lib/tasks/webpacker/binstubs.rake @@ -3,11 +3,13 @@ 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 diff --git a/lib/tasks/webpacker/clobber.rake b/lib/tasks/webpacker/clobber.rake index 748ccd178..57f0e12d5 100644 --- a/lib/tasks/webpacker/clobber.rake +++ b/lib/tasks/webpacker/clobber.rake @@ -10,7 +10,5 @@ end # Run clobber if the assets:clobber is run if Rake::Task.task_defined?("assets:clobber") - Rake::Task["assets:clobber"].enhance do - Rake::Task["webpacker:clobber"].invoke - end + Rake::Task["assets:clobber"].enhance ["webpacker:clobber"] end diff --git a/lib/tasks/webpacker/compile.rake b/lib/tasks/webpacker/compile.rake index f2b02cd46..a970f8df2 100644 --- a/lib/tasks/webpacker/compile.rake +++ b/lib/tasks/webpacker/compile.rake @@ -10,8 +10,10 @@ end def enhance_assets_precompile # yarn:install was added in Rails 5.1 deps = yarn_install_available? ? [] : ["webpacker:yarn_install"] - Rake::Task["assets:precompile"].enhance(deps) do - Rake::Task["webpacker:compile"].invoke + Rake::Task["assets:precompile"].enhance(deps) do |task| + prefix = task.name.split(/#|assets:precompile/).first + + Rake::Task["#{prefix}webpacker:compile"].invoke end end diff --git a/lib/tasks/webpacker/install.rake b/lib/tasks/webpacker/install.rake index a90a16927..c47a867cd 100644 --- a/lib/tasks/webpacker/install.rake +++ b/lib/tasks/webpacker/install.rake @@ -3,11 +3,13 @@ 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 From 00071d89f48be574afe95220459600d340b27957 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:22:31 +0100 Subject: [PATCH 03/12] Explicitly require Rails::Engine MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 `': 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 `' 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 `
' ``` Explicitly requiring rails/engine resolves this. --- lib/webpacker/railtie.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/webpacker/railtie.rb b/lib/webpacker/railtie.rb index be4749390..8f8633848 100644 --- a/lib/webpacker/railtie.rb +++ b/lib/webpacker/railtie.rb @@ -1,4 +1,5 @@ require "rails/railtie" +require "rails/engine" require "webpacker/helper" require "webpacker/dev_server_proxy" @@ -29,23 +30,24 @@ class Webpacker::Engine < ::Rails::Engine # - edit config/environments/production.rb # - add `config.webpacker.check_yarn_integrity = true` initializer "webpacker.yarn_check" do |app| - if File.exist?("yarn.lock") && Webpacker.config.config_path.exist? && Webpacker.config.check_yarn_integrity? - output = `yarn check --integrity && yarn check --verify-tree 2>&1` + Dir.chdir(Rails.root) do + if File.exist?("yarn.lock") && Webpacker.config.config_path.exist? && Webpacker.config.check_yarn_integrity? + output = `yarn check --integrity && yarn check --verify-tree 2>&1` - unless $?.success? - $stderr.puts "\n\n" - $stderr.puts "========================================" - $stderr.puts " Your Yarn packages are out of date!" - $stderr.puts " Please run `yarn install --check-files` to update." - $stderr.puts "========================================" - $stderr.puts "\n\n" - $stderr.puts "To disable this check, please change `check_yarn_integrity`" - $stderr.puts "to `false` in your webpacker config file (config/webpacker.yml)." - $stderr.puts "\n\n" - $stderr.puts output - $stderr.puts "\n\n" - - exit(1) + unless $?.success? + $stderr.puts "\n\n" + $stderr.puts "========================================" + $stderr.puts " Your Yarn packages are out of date!" + $stderr.puts " Please run `yarn install --check-files` to update." + $stderr.puts "========================================" + $stderr.puts "\n\n" + $stderr.puts "To disable this check, please change `check_yarn_integrity`" + $stderr.puts "to `false` in your webpacker config file (config/webpacker.yml)." + $stderr.puts "\n\n" + $stderr.puts output + $stderr.puts "\n\n" + exit(1) + end end end end From 5d374ca92f93894497d7189bed8b34f04b05d60f Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:35:06 +0100 Subject: [PATCH 04/12] 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. --- lib/install/angular.rb | 6 ++++-- lib/install/coffee.rb | 6 ++++-- lib/install/elm.rb | 12 +++++++----- lib/install/erb.rb | 6 ++++-- lib/install/react.rb | 6 ++++-- lib/install/stimulus.rb | 6 ++++-- lib/install/template.rb | 20 +++++++++++--------- lib/install/typescript.rb | 6 ++++-- lib/install/vue.rb | 6 ++++-- lib/tasks/webpacker/yarn_install.rake | 4 +++- 10 files changed, 49 insertions(+), 29 deletions(-) diff --git a/lib/install/angular.rb b/lib/install/angular.rb index 2f4de8db0..1535610e8 100644 --- a/lib/install/angular.rb +++ b/lib/install/angular.rb @@ -6,8 +6,10 @@ say "Copying hello_angular app to #{Webpacker.config.source_path}" directory "#{__dir__}/examples/angular/hello_angular", "#{Webpacker.config.source_path}/hello_angular" -say "Installing all angular dependencies" -run "yarn add core-js zone.js rxjs @angular/core @angular/common @angular/compiler @angular/platform-browser @angular/platform-browser-dynamic" +Dir.chdir(Rails.root) do + say "Installing all angular dependencies" + run "yarn add core-js zone.js rxjs @angular/core @angular/common @angular/compiler @angular/platform-browser @angular/platform-browser-dynamic" +end if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1 say "You need to enable unsafe-eval rule.", :yellow diff --git a/lib/install/coffee.rb b/lib/install/coffee.rb index 0a402ec1c..800b743cd 100644 --- a/lib/install/coffee.rb +++ b/lib/install/coffee.rb @@ -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 coffeescript@1.12.7 coffee-loader" +Dir.chdir(Rails.root) do + say "Installing all Coffeescript dependencies" + run "yarn add coffeescript@1.12.7 coffee-loader" +end say "Webpacker now supports Coffeescript πŸŽ‰", :green diff --git a/lib/install/elm.rb b/lib/install/elm.rb index 579a97df4..71399f0b4 100644 --- a/lib/install/elm.rb +++ b/lib/install/elm.rb @@ -20,11 +20,13 @@ copy_file "#{__dir__}/examples/elm/Main.elm", "#{Webpacker.config.source_path}/Main.elm" -say "Installing all Elm dependencies" -run "yarn add elm elm-webpack-loader" -run "yarn add --dev elm-hot-webpack-loader" -run "yarn run elm init" -run "yarn run elm make #{Webpacker.config.source_path}/Main.elm" +Dir.chdir(Rails.root) do + say "Installing all Elm dependencies" + run "yarn add elm elm-webpack-loader" + run "yarn add --dev elm-hot-webpack-loader" + run "yarn run elm init" + run "yarn run elm make #{Webpacker.config.source_path}/Main.elm" +end say "Updating webpack paths to include .elm file extension" insert_into_file Webpacker.config.config_path, "- .elm\n".indent(4), after: /\s+extensions:\n/ diff --git a/lib/install/erb.rb b/lib/install/erb.rb index 6918896f1..c9333190b 100644 --- a/lib/install/erb.rb +++ b/lib/install/erb.rb @@ -19,7 +19,9 @@ copy_file "#{__dir__}/examples/erb/hello_erb.js.erb", "#{Webpacker.config.source_entry_path}/hello_erb.js.erb" -say "Installing all Erb dependencies" -run "yarn add rails-erb-loader" +Dir.chdir(Rails.root) do + say "Installing all Erb dependencies" + run "yarn add rails-erb-loader" +end say "Webpacker now supports Erb in JS πŸŽ‰", :green diff --git a/lib/install/react.rb b/lib/install/react.rb index c08e39717..fe7f1c3b8 100644 --- a/lib/install/react.rb +++ b/lib/install/react.rb @@ -12,7 +12,9 @@ say "Updating webpack paths to include .jsx file extension" insert_into_file Webpacker.config.config_path, "- .jsx\n".indent(4), after: /\s+extensions:\n/ -say "Installing all react dependencies" -run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types" +Dir.chdir(Rails.root) do + say "Installing all react dependencies" + run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types" +end say "Webpacker now supports react.js πŸŽ‰", :green diff --git a/lib/install/stimulus.rb b/lib/install/stimulus.rb index 94ee32a70..6ce522865 100644 --- a/lib/install/stimulus.rb +++ b/lib/install/stimulus.rb @@ -6,7 +6,9 @@ say "Creating controllers directory" directory "#{__dir__}/examples/stimulus/controllers", "#{Webpacker.config.source_path}/controllers" -say "Installing all Stimulus dependencies" -run "yarn add stimulus" +Dir.chdir(Rails.root) do + say "Installing all Stimulus dependencies" + run "yarn add stimulus" +end say "Webpacker now supports Stimulus.js πŸŽ‰", :green diff --git a/lib/install/template.rb b/lib/install/template.rb index 96814fe27..69c883dda 100644 --- a/lib/install/template.rb +++ b/lib/install/template.rb @@ -34,16 +34,18 @@ end end -if Webpacker::VERSION =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ - say "Installing all JavaScript dependencies [#{Webpacker::VERSION}]" - run "yarn add @rails/webpacker@#{Webpacker::VERSION}" -else - say "Installing all JavaScript dependencies [from prerelease rails/webpacker]" - run "yarn add @rails/webpacker@next" -end +Dir.chdir(Rails.root) do + if Webpacker::VERSION =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ + say "Installing all JavaScript dependencies [#{Webpacker::VERSION}]" + run "yarn add @rails/webpacker@#{Webpacker::VERSION}" + else + say "Installing all JavaScript dependencies [from prerelease rails/webpacker]" + run "yarn add @rails/webpacker@next" + end -say "Installing dev server for live reloading" -run "yarn add --dev webpack-dev-server" + say "Installing dev server for live reloading" + run "yarn add --dev webpack-dev-server" +end if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1 say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow diff --git a/lib/install/typescript.rb b/lib/install/typescript.rb index fe6c4c65b..333cc6d83 100644 --- a/lib/install/typescript.rb +++ b/lib/install/typescript.rb @@ -40,7 +40,9 @@ copy_file "#{__dir__}/examples/typescript/hello_typescript.ts", "#{Webpacker.config.source_entry_path}/hello_typescript.ts" -say "Installing all typescript dependencies" -run "yarn add typescript ts-loader #{additional_packages}" +Dir.chdir(Rails.root) do + say "Installing all typescript dependencies" + run "yarn add typescript ts-loader #{additional_packages}" +end say "Webpacker now supports typescript πŸŽ‰", :green diff --git a/lib/install/vue.rb b/lib/install/vue.rb index 8a18c531c..33800e3ed 100644 --- a/lib/install/vue.rb +++ b/lib/install/vue.rb @@ -32,8 +32,10 @@ copy_file "#{__dir__}/examples/vue/app.vue", "#{Webpacker.config.source_path}/app.vue" -say "Installing all Vue dependencies" -run "yarn add vue vue-loader vue-template-compiler" +Dir.chdir(Rails.root) do + say "Installing all Vue dependencies" + run "yarn add vue vue-loader vue-template-compiler" +end if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1 say "You need to enable unsafe-eval rule.", :yellow diff --git a/lib/tasks/webpacker/yarn_install.rake b/lib/tasks/webpacker/yarn_install.rake index af5bea542..fbd2e94d5 100644 --- a/lib/tasks/webpacker/yarn_install.rake +++ b/lib/tasks/webpacker/yarn_install.rake @@ -5,6 +5,8 @@ namespace :webpacker do node_env = ENV.fetch("NODE_ENV") do valid_node_envs.include?(Rails.env) ? Rails.env : "production" end - system({ "NODE_ENV" => node_env }, "yarn install --no-progress --frozen-lockfile") + Dir.chdir(Rails.root) do + system({ "NODE_ENV" => node_env }, "yarn install --no-progress --frozen-lockfile") + end end end From b8e414c6d2389aa4b8a31bf08f14f41c3ceeff04 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:39:30 +0100 Subject: [PATCH 05/12] 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. --- lib/install/template.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/install/template.rb b/lib/install/template.rb index 69c883dda..a140f687a 100644 --- a/lib/install/template.rb +++ b/lib/install/template.rb @@ -22,8 +22,9 @@ apply "#{__dir__}/binstubs.rb" -if File.exists?(".gitignore") - append_to_file ".gitignore" do +git_ignore_path = Rails.root.join(".gitignore") +if File.exists?(git_ignore_path) + append_to_file git_ignore_path do "\n" + "/public/packs\n" + "/public/packs-test\n" + From f65be8f0946b6f016d6c6455dc1aaa92cc968e02 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:44:17 +0100 Subject: [PATCH 06/12] 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. --- lib/tasks/webpacker/info.rake | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/tasks/webpacker/info.rake b/lib/tasks/webpacker/info.rake index 3a44a12d9..df9b9fb94 100644 --- a/lib/tasks/webpacker/info.rake +++ b/lib/tasks/webpacker/info.rake @@ -3,17 +3,19 @@ require "webpacker/version" namespace :webpacker do desc "Provide information on Webpacker's environment" task :info do - $stdout.puts "Ruby: #{`ruby --version`}" - $stdout.puts "Rails: #{Rails.version}" - $stdout.puts "Webpacker: #{Webpacker::VERSION}" - $stdout.puts "Node: #{`node --version`}" - $stdout.puts "Yarn: #{`yarn --version`}" + Dir.chdir(Rails.root) do + $stdout.puts "Ruby: #{`ruby --version`}" + $stdout.puts "Rails: #{Rails.version}" + $stdout.puts "Webpacker: #{Webpacker::VERSION}" + $stdout.puts "Node: #{`node --version`}" + $stdout.puts "Yarn: #{`yarn --version`}" - $stdout.puts "\n" - $stdout.puts "@rails/webpacker: \n#{`npm list @rails/webpacker version`}" + $stdout.puts "\n" + $stdout.puts "@rails/webpacker: \n#{`npm list @rails/webpacker version`}" - $stdout.puts "Is bin/webpack present?: #{File.exist? 'bin/webpack'}" - $stdout.puts "Is bin/webpack-dev-server present?: #{File.exist? 'bin/webpack-dev-server'}" - $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}" + $stdout.puts "Is bin/webpack present?: #{File.exist? 'bin/webpack'}" + $stdout.puts "Is bin/webpack-dev-server present?: #{File.exist? 'bin/webpack-dev-server'}" + $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}" + end end end From 87268138d72b7e23474dd5aec9cd44207d2a237e Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 13:48:02 +0100 Subject: [PATCH 07/12] Show a relative path when describing configuration missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ``` --- lib/tasks/webpacker/verify_install.rake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tasks/webpacker/verify_install.rake b/lib/tasks/webpacker/verify_install.rake index d81090c25..4f4ca6189 100644 --- a/lib/tasks/webpacker/verify_install.rake +++ b/lib/tasks/webpacker/verify_install.rake @@ -4,7 +4,8 @@ namespace :webpacker do desc "Verifies if Webpacker is installed" task verify_install: [:check_node, :check_yarn, :check_binstubs] do unless Webpacker.config.config_path.exist? - $stderr.puts "Configuration config/webpacker.yml file not found. \n"\ + path = Webpacker.config.config_path.relative_path_from(Pathname.new(pwd)).to_s + $stderr.puts "Configuration #{path} file not found. \n"\ "Make sure webpacker:install is run successfully before " \ "running dependent tasks" exit! From 32a7749617896c6aa785c331b13c772e4d37c3f2 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Sat, 8 Sep 2018 14:42:39 +0100 Subject: [PATCH 08/12] 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. --- .gitignore | 1 + test/engine_rake_tasks_test.rb | 39 ++++++++++ test/mounted_app/Rakefile | 4 + test/mounted_app/test/dummy/Rakefile | 3 + test/mounted_app/test/dummy/bin/rails | 3 + test/mounted_app/test/dummy/bin/rake | 3 + test/mounted_app/test/dummy/config.ru | 5 ++ .../test/dummy/config/application.rb | 10 +++ .../test/dummy/config/environment.rb | 3 + .../test/dummy/config/webpacker.yml | 75 +++++++++++++++++++ test/mounted_app/test/dummy/package.json | 7 ++ 11 files changed, 153 insertions(+) create mode 100644 test/engine_rake_tasks_test.rb create mode 100644 test/mounted_app/Rakefile create mode 100644 test/mounted_app/test/dummy/Rakefile create mode 100755 test/mounted_app/test/dummy/bin/rails create mode 100755 test/mounted_app/test/dummy/bin/rake create mode 100644 test/mounted_app/test/dummy/config.ru create mode 100644 test/mounted_app/test/dummy/config/application.rb create mode 100644 test/mounted_app/test/dummy/config/environment.rb create mode 100644 test/mounted_app/test/dummy/config/webpacker.yml create mode 100644 test/mounted_app/test/dummy/package.json diff --git a/.gitignore b/.gitignore index 62fbfaee1..07598ddfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /.bundle /pkg +/test/mounted_app/test/dummy/log /test/test_app/log node_modules .byebug_history diff --git a/test/engine_rake_tasks_test.rb b/test/engine_rake_tasks_test.rb new file mode 100644 index 000000000..a1a288a52 --- /dev/null +++ b/test/engine_rake_tasks_test.rb @@ -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 diff --git a/test/mounted_app/Rakefile b/test/mounted_app/Rakefile new file mode 100644 index 000000000..fe4151add --- /dev/null +++ b/test/mounted_app/Rakefile @@ -0,0 +1,4 @@ +require "bundler/setup" + +APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__) +load "rails/tasks/engine.rake" diff --git a/test/mounted_app/test/dummy/Rakefile b/test/mounted_app/test/dummy/Rakefile new file mode 100644 index 000000000..d1baef069 --- /dev/null +++ b/test/mounted_app/test/dummy/Rakefile @@ -0,0 +1,3 @@ +require_relative "config/application" + +Rails.application.load_tasks diff --git a/test/mounted_app/test/dummy/bin/rails b/test/mounted_app/test/dummy/bin/rails new file mode 100755 index 000000000..fc42e555b --- /dev/null +++ b/test/mounted_app/test/dummy/bin/rails @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require "rails/commands" diff --git a/test/mounted_app/test/dummy/bin/rake b/test/mounted_app/test/dummy/bin/rake new file mode 100755 index 000000000..580915dc8 --- /dev/null +++ b/test/mounted_app/test/dummy/bin/rake @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +require "rake" +Rake.application.run diff --git a/test/mounted_app/test/dummy/config.ru b/test/mounted_app/test/dummy/config.ru new file mode 100644 index 000000000..8987d1c00 --- /dev/null +++ b/test/mounted_app/test/dummy/config.ru @@ -0,0 +1,5 @@ +# This file allows the `Rails.root` to be correctly determined. + +require_relative "config/environment" + +run Rails.application diff --git a/test/mounted_app/test/dummy/config/application.rb b/test/mounted_app/test/dummy/config/application.rb new file mode 100644 index 000000000..24f516874 --- /dev/null +++ b/test/mounted_app/test/dummy/config/application.rb @@ -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 diff --git a/test/mounted_app/test/dummy/config/environment.rb b/test/mounted_app/test/dummy/config/environment.rb new file mode 100644 index 000000000..73a3979b0 --- /dev/null +++ b/test/mounted_app/test/dummy/config/environment.rb @@ -0,0 +1,3 @@ +require_relative "application" + +Rails.application.initialize! diff --git a/test/mounted_app/test/dummy/config/webpacker.yml b/test/mounted_app/test/dummy/config/webpacker.yml new file mode 100644 index 000000000..cd7cd329e --- /dev/null +++ b/test/mounted_app/test/dummy/config/webpacker.yml @@ -0,0 +1,75 @@ +# Note: You must restart bin/webpack-dev-server for changes to take effect + +default: &default + source_path: app/javascript + source_entry_path: packs + public_output_path: packs + cache_path: tmp/cache/webpacker + + # Additional paths webpack should lookup modules + # ['app/assets', 'engine/foo/app/assets'] + resolved_paths: + - app/assets + - /etc/yarn + + # Reload manifest.json on all requests so we reload latest compiled packs + cache_manifest: false + + extensions: + - .js + - .sass + - .scss + - .css + - .module.sass + - .module.scss + - .module.css + - .png + - .svg + - .gif + - .jpeg + - .jpg + +development: + <<: *default + compile: true + + # Reference: https://webpack.js.org/configuration/dev-server/ + dev_server: + https: false + host: localhost + port: 3035 + public: localhost:3035 + hmr: false + # Inline should be set to true if using HMR + inline: true + overlay: true + disable_host_check: true + use_local_ip: false + +test: + <<: *default + compile: true + + # Compile test packs to a separate directory + public_output_path: packs-test + +production: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + +staging: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + + # Compile staging packs to a separate directory + public_output_path: packs-staging diff --git a/test/mounted_app/test/dummy/package.json b/test/mounted_app/test/dummy/package.json new file mode 100644 index 000000000..d32a2dc50 --- /dev/null +++ b/test/mounted_app/test/dummy/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "@rails/webpacker": "file:../../../../" + }, + "license": "MIT" +} From bfa84c5c3e03b2f20b7a4baeb06073b1b790f42d Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Mon, 24 Aug 2020 18:48:42 +0100 Subject: [PATCH 09/12] Update railtie.rb --- lib/webpacker/railtie.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/webpacker/railtie.rb b/lib/webpacker/railtie.rb index b5ef681ff..5c9853894 100644 --- a/lib/webpacker/railtie.rb +++ b/lib/webpacker/railtie.rb @@ -1,5 +1,4 @@ require "rails/railtie" -require "rails/engine" require "webpacker/helper" require "webpacker/dev_server_proxy" @@ -7,7 +6,6 @@ class Webpacker::Engine < ::Rails::Engine # Allows Webpacker config values to be set via Rails env config files config.webpacker = ActiveSupport::OrderedOptions.new - initializer "webpacker.proxy" do |app| insert_middleware = Webpacker.config.dev_server.present? rescue nil if insert_middleware From cf3a17f12c14c4e930ed836864752a9bc08a82e2 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Mon, 24 Aug 2020 18:49:03 +0100 Subject: [PATCH 10/12] Update railtie.rb --- lib/webpacker/railtie.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/webpacker/railtie.rb b/lib/webpacker/railtie.rb index 5c9853894..0bb1635da 100644 --- a/lib/webpacker/railtie.rb +++ b/lib/webpacker/railtie.rb @@ -6,6 +6,7 @@ class Webpacker::Engine < ::Rails::Engine # Allows Webpacker config values to be set via Rails env config files config.webpacker = ActiveSupport::OrderedOptions.new + initializer "webpacker.proxy" do |app| insert_middleware = Webpacker.config.dev_server.present? rescue nil if insert_middleware From 6e9fa63e1b2d5acc157c57a5a857f275ddb19f91 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Mon, 24 Aug 2020 18:50:41 +0100 Subject: [PATCH 11/12] Update railtie.rb --- lib/webpacker/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/webpacker/railtie.rb b/lib/webpacker/railtie.rb index 0bb1635da..f2ff72fba 100644 --- a/lib/webpacker/railtie.rb +++ b/lib/webpacker/railtie.rb @@ -6,7 +6,7 @@ class Webpacker::Engine < ::Rails::Engine # Allows Webpacker config values to be set via Rails env config files config.webpacker = ActiveSupport::OrderedOptions.new - + initializer "webpacker.proxy" do |app| insert_middleware = Webpacker.config.dev_server.present? rescue nil if insert_middleware From 8c2b477a97438466670c6a43dda55fe2440b2c51 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Mon, 24 Aug 2020 18:51:58 +0100 Subject: [PATCH 12/12] Use additional_paths --- test/mounted_app/test/dummy/config/webpacker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mounted_app/test/dummy/config/webpacker.yml b/test/mounted_app/test/dummy/config/webpacker.yml index cd7cd329e..b8ef6d6c7 100644 --- a/test/mounted_app/test/dummy/config/webpacker.yml +++ b/test/mounted_app/test/dummy/config/webpacker.yml @@ -8,7 +8,7 @@ default: &default # Additional paths webpack should lookup modules # ['app/assets', 'engine/foo/app/assets'] - resolved_paths: + additional_paths: - app/assets - /etc/yarn