From c1a902bd9311c84775258215951b0d99746a6143 Mon Sep 17 00:00:00 2001 From: Stefan Wrobel Date: Fri, 26 Jan 2018 15:48:46 -0800 Subject: [PATCH] Updates for Rails 5.2 / Webpacker 3.2 --- .gitignore | 9 ++++-- bin/bundle | 2 +- bin/rails | 2 +- bin/setup | 8 +++-- bin/update | 6 ++-- bin/webpack | 37 +++++++++------------ bin/webpack-dev-server | 52 +++++++++--------------------- bin/yarn | 6 ++-- config/environments/development.rb | 2 +- config/environments/production.rb | 2 +- config/webpacker.yml | 11 ++++--- invoker.ini | 3 ++ package.json | 4 +-- yarn.lock | 4 +-- 14 files changed, 66 insertions(+), 82 deletions(-) diff --git a/.gitignore b/.gitignore index 4386abf..413b758 100644 --- a/.gitignore +++ b/.gitignore @@ -19,10 +19,13 @@ .env* !invoker.ini +# Ignore master key for decrypting credentials and more. +/config/master.key + # Webpacker /public/packs /public/packs-test /node_modules - -# Ignore master key for decrypting credentials and more. -/config/master.key +yarn-debug.log* +yarn-error.log* +.yarn-integrity diff --git a/bin/bundle b/bin/bundle index 66e9889..f19acf5 100755 --- a/bin/bundle +++ b/bin/bundle @@ -1,3 +1,3 @@ #!/usr/bin/env ruby -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) load Gem.bin_path('bundler', 'bundle') diff --git a/bin/rails b/bin/rails index 0138d79..5badb2f 100755 --- a/bin/rails +++ b/bin/rails @@ -4,6 +4,6 @@ begin rescue LoadError => e raise unless e.message.include?('spring') end -APP_PATH = File.expand_path('../../config/application', __FILE__) +APP_PATH = File.expand_path('../config/application', __dir__) require_relative '../config/boot' require 'rails/commands' diff --git a/bin/setup b/bin/setup index 589bee8..94fd4d7 100755 --- a/bin/setup +++ b/bin/setup @@ -1,10 +1,9 @@ #!/usr/bin/env ruby -require 'pathname' require 'fileutils' include FileUtils # path to your application root. -APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) +APP_ROOT = File.expand_path('..', __dir__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") @@ -16,7 +15,10 @@ chdir APP_ROOT do puts '== Installing dependencies ==' system! 'gem install bundler --conservative' - system('bundle check') or system!('bundle install') + system('bundle check') || system!('bundle install') + + # Install JavaScript dependencies if using Yarn + # system('bin/yarn') # puts "\n== Copying sample files ==" # unless File.exist?('config/database.yml') diff --git a/bin/update b/bin/update index a8e4462..58bfaed 100755 --- a/bin/update +++ b/bin/update @@ -1,10 +1,9 @@ #!/usr/bin/env ruby -require 'pathname' require 'fileutils' include FileUtils # path to your application root. -APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) +APP_ROOT = File.expand_path('..', __dir__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") @@ -18,6 +17,9 @@ chdir APP_ROOT do system! 'gem install bundler --conservative' system('bundle check') || system!('bundle install') + # Install JavaScript dependencies if using Yarn + # system('bin/yarn') + puts "\n== Updating database ==" system! 'bin/rails db:migrate' diff --git a/bin/webpack b/bin/webpack index 867550e..52adc07 100755 --- a/bin/webpack +++ b/bin/webpack @@ -1,28 +1,21 @@ #!/usr/bin/env ruby -$stdout.sync = true +# frozen_string_literal: true -require "shellwords" -require "yaml" +# +# This file was generated by Bundler. +# +# The application 'webpack' is installed as part of a gem, and +# this file is here to facilitate running it. +# -ENV["RAILS_ENV"] ||= "development" -RAILS_ENV = ENV["RAILS_ENV"] +bundle_binstub = File.expand_path("../bundle", __FILE__) +load(bundle_binstub) if File.file?(bundle_binstub) -ENV["NODE_ENV"] ||= RAILS_ENV -NODE_ENV = ENV["NODE_ENV"] +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) -APP_PATH = File.expand_path("../", __dir__) -NODE_MODULES_PATH = File.join(APP_PATH, "node_modules") -WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js") +require "rubygems" +require "bundler/setup" -unless File.exist?(WEBPACK_CONFIG) - puts "Webpack configuration not found." - puts "Please run bundle exec rails webpacker:install to install webpacker" - exit! -end - -newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape } -cmdline = ["yarn", "run", "webpack", "--", "--config", WEBPACK_CONFIG] + ARGV - -Dir.chdir(APP_PATH) do - exec newenv, *cmdline -end +load Gem.bin_path("webpacker", "webpack") diff --git a/bin/webpack-dev-server b/bin/webpack-dev-server index 3e10082..03911f2 100755 --- a/bin/webpack-dev-server +++ b/bin/webpack-dev-server @@ -1,43 +1,21 @@ #!/usr/bin/env ruby -$stdout.sync = true +# frozen_string_literal: true -require "shellwords" -require "yaml" +# +# This file was generated by Bundler. +# +# The application 'webpack-dev-server' is installed as part of a gem, and +# this file is here to facilitate running it. +# -ENV["RAILS_ENV"] ||= "development" -RAILS_ENV = ENV["RAILS_ENV"] +bundle_binstub = File.expand_path("../bundle", __FILE__) +load(bundle_binstub) if File.file?(bundle_binstub) -ENV["NODE_ENV"] ||= RAILS_ENV -NODE_ENV = ENV["NODE_ENV"] +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) -APP_PATH = File.expand_path("../", __dir__) -CONFIG_FILE = File.join(APP_PATH, "config/webpacker.yml") -NODE_MODULES_PATH = File.join(APP_PATH, "node_modules") -WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/development.js") +require "rubygems" +require "bundler/setup" -def args(key) - index = ARGV.index(key) - index ? ARGV[index + 1] : nil -end - -begin - dev_server = YAML.load_file(CONFIG_FILE)["development"]["dev_server"] - - DEV_SERVER_HOST = "http#{"s" if args('--https') || dev_server["https"]}://#{args('--host') || dev_server["host"]}:#{args('--port') || dev_server["port"]}" - -rescue Errno::ENOENT, NoMethodError - puts "Webpack dev_server configuration not found in #{CONFIG_FILE}." - puts "Please run bundle exec rails webpacker:install to install webpacker" - exit! -end - -newenv = { - "NODE_PATH" => NODE_MODULES_PATH.shellescape, - "ASSET_HOST" => DEV_SERVER_HOST.shellescape -}.freeze - -cmdline = ["yarn", "run", "webpack-dev-server", "--progress", "--color", "--config", WEBPACK_CONFIG] + ARGV - -Dir.chdir(APP_PATH) do - exec newenv, *cmdline -end +load Gem.bin_path("webpacker", "webpack-dev-server") diff --git a/bin/yarn b/bin/yarn index c2bacef..ec3db7b 100755 --- a/bin/yarn +++ b/bin/yarn @@ -1,8 +1,8 @@ #!/usr/bin/env ruby -VENDOR_PATH = File.expand_path('..', __dir__) -Dir.chdir(VENDOR_PATH) do +APP_ROOT = File.expand_path('..', __dir__) +Dir.chdir(APP_ROOT) do begin - exec "yarnpkg #{ARGV.join(" ")}" + exec "yarnpkg #{ARGV.join(' ')}" rescue Errno::ENOENT $stderr.puts "Yarn executable was not detected in the system." $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install" diff --git a/config/environments/development.rb b/config/environments/development.rb index 91337a0..968e174 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. + config.webpacker.check_yarn_integrity = true # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development diff --git a/config/environments/production.rb b/config/environments/production.rb index 258a41c..21a16ba 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. + config.webpacker.check_yarn_integrity = false # Code is not reloaded between requests. config.cache_classes = true diff --git a/config/webpacker.yml b/config/webpacker.yml index adb2901..8621e53 100644 --- a/config/webpacker.yml +++ b/config/webpacker.yml @@ -14,12 +14,8 @@ default: &default cache_manifest: false extensions: - - .coffee - - .erb - .js - .jsx - - .ts - - .vue - .sass - .scss - .css @@ -43,8 +39,15 @@ development: # Inline should be set to true if using HMR inline: true overlay: true + compress: true disable_host_check: true use_local_ip: false + quiet: false + headers: + 'Access-Control-Allow-Origin': '*' + watch_options: + ignored: /node_modules/ + test: <<: *default diff --git a/invoker.ini b/invoker.ini index daef252..6ee3b7b 100644 --- a/invoker.ini +++ b/invoker.ini @@ -1,6 +1,9 @@ [surf] command = RUBY_DEBUG_PORT=4444 bin/rails s -p $PORT -b 127.0.0.1 +[surf-packs] +command = bin/webpack-dev-server + [guard] command = bin/guard -i diff --git a/package.json b/package.json index 0137578..fd7c353 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "dependencies": { - "@rails/webpacker": "^3.0.2", + "@rails/webpacker": "^3.2.1", "babel-preset-react": "^6.24.1", "headroom.js": "^0.9.4", "prop-types": "^15.6.0", @@ -11,6 +11,6 @@ }, "devDependencies": { "browser-sync": "^2.18.12", - "webpack-dev-server": "^2.9.3" + "webpack-dev-server": "^2.11.1" } } diff --git a/yarn.lock b/yarn.lock index b3e1d3b..978cc7d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,7 +2,7 @@ # yarn lockfile v1 -"@rails/webpacker@^3.0.2": +"@rails/webpacker@^3.2.1": version "3.2.1" resolved "https://registry.yarnpkg.com/@rails/webpacker/-/webpacker-3.2.1.tgz#da7331106b93b2818c3595627c88c5c037fff3c8" dependencies: @@ -6438,7 +6438,7 @@ webpack-dev-middleware@1.12.2: range-parser "^1.0.3" time-stamp "^2.0.0" -webpack-dev-server@^2.9.3: +webpack-dev-server@^2.11.1: version "2.11.1" resolved "https://registry.yarnpkg.com/webpack-dev-server/-/webpack-dev-server-2.11.1.tgz#6f9358a002db8403f016e336816f4485384e5ec0" dependencies: