Upgrade Webpacker to version 3.0.1 (#5122)
This commit is contained in:
		
				
					committed by
					
						
						Eugen Rochko
					
				
			
			
				
	
			
			
			
						parent
						
							3caf0ba923
						
					
				
				
					commit
					901fc48aae
				
			@@ -3,6 +3,7 @@ $stdout.sync = true
 | 
			
		||||
 | 
			
		||||
require "shellwords"
 | 
			
		||||
require "yaml"
 | 
			
		||||
require "socket"
 | 
			
		||||
 | 
			
		||||
ENV["RAILS_ENV"] ||= "development"
 | 
			
		||||
RAILS_ENV = ENV["RAILS_ENV"]
 | 
			
		||||
@@ -13,7 +14,9 @@ NODE_ENV = ENV["NODE_ENV"]
 | 
			
		||||
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")
 | 
			
		||||
WEBPACK_CONFIG    = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")
 | 
			
		||||
 | 
			
		||||
DEFAULT_LISTEN_HOST_ADDR = NODE_ENV == 'development' ? 'localhost' : '0.0.0.0'
 | 
			
		||||
 | 
			
		||||
def args(key)
 | 
			
		||||
  index = ARGV.index(key)
 | 
			
		||||
@@ -21,23 +24,45 @@ def args(key)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
begin
 | 
			
		||||
  dev_server = YAML.load_file(CONFIG_FILE)["development"]["dev_server"]
 | 
			
		||||
  dev_server = YAML.load_file(CONFIG_FILE)[RAILS_ENV]["dev_server"]
 | 
			
		||||
 | 
			
		||||
  DEV_SERVER_HOST = "http#{"s" if args('--https') || dev_server["https"]}://#{dev_server["host"]}:#{args('--port') || dev_server["port"]}"
 | 
			
		||||
  HOSTNAME          = args('--host') || dev_server["host"]
 | 
			
		||||
  PORT              = args('--port') || dev_server["port"]
 | 
			
		||||
  HTTPS             = ARGV.include?('--https') || dev_server["https"]
 | 
			
		||||
  DEV_SERVER_ADDR   = "http#{"s" if HTTPS}://#{HOSTNAME}:#{PORT}"
 | 
			
		||||
  LISTEN_HOST_ADDR  = args('--listen-host') || DEFAULT_LISTEN_HOST_ADDR
 | 
			
		||||
 | 
			
		||||
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"
 | 
			
		||||
  $stdout.puts "Webpack dev_server configuration not found in #{CONFIG_FILE}."
 | 
			
		||||
  $stdout.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
 | 
			
		||||
begin
 | 
			
		||||
  server = TCPServer.new(LISTEN_HOST_ADDR, PORT)
 | 
			
		||||
  server.close
 | 
			
		||||
 | 
			
		||||
cmdline = ["yarn", "run", "webpack-dev-server", "--", "--progress", "--color", "--config", WEBPACK_CONFIG] + ARGV
 | 
			
		||||
rescue Errno::EADDRINUSE
 | 
			
		||||
  $stdout.puts "Another program is running on port #{PORT}. Set a new port in #{CONFIG_FILE} for dev_server"
 | 
			
		||||
  exit!
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# Delete supplied host, port and listen-host CLI arguments
 | 
			
		||||
["--host", "--port", "--listen-host"].each do |arg|
 | 
			
		||||
  ARGV.delete(args(arg))
 | 
			
		||||
  ARGV.delete(arg)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
env = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
 | 
			
		||||
 | 
			
		||||
cmd = [
 | 
			
		||||
  "#{NODE_MODULES_PATH}/.bin/webpack-dev-server", "--progress", "--color",
 | 
			
		||||
  "--config", WEBPACK_CONFIG,
 | 
			
		||||
  "--host", LISTEN_HOST_ADDR,
 | 
			
		||||
  "--public", "#{HOSTNAME}:#{PORT}",
 | 
			
		||||
  "--port", PORT.to_s
 | 
			
		||||
] + ARGV
 | 
			
		||||
 | 
			
		||||
Dir.chdir(APP_PATH) do
 | 
			
		||||
  exec newenv, *cmdline
 | 
			
		||||
  exec env, *cmd
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user