Skip to content

Commit 8f48616

Browse files
rosstagauravtiwari
authored andcommitted
Binstubs (#833)
Convert webpacker executables to binstubs
1 parent 7459f1f commit 8f48616

9 files changed

+129
-98
lines changed

exe/webpack

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
3+
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4+
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
5+
6+
require "webpacker"
7+
require "webpacker/webpack_runner"
8+
Webpacker::WebpackRunner.run(ARGV)

exe/webpack-dev-server

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
3+
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4+
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
5+
6+
require "webpacker"
7+
require "webpacker/dev_server_runner"
8+
Webpacker::DevServerRunner.run(ARGV)

lib/install/bin/webpack-dev-server.tt

-68
This file was deleted.

lib/install/bin/webpack.tt

-26
This file was deleted.

lib/install/template.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
puts "Creating javascript app source directory"
1414
directory "#{__dir__}/javascript", Webpacker.config.source_path
1515

16-
puts "Copying binstubs"
17-
directory "#{__dir__}/bin", "bin"
18-
19-
chmod "bin", 0755 & ~File.umask, verbose: false
16+
puts "Installing binstubs"
17+
run "bundle binstubs webpacker"
2018

2119
if File.exists?(".gitignore")
2220
append_to_file ".gitignore", <<-EOS

lib/webpacker/dev_server_runner.rb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require "shellwords"
2+
require "yaml"
3+
require "socket"
4+
require "webpacker/runner"
5+
6+
module Webpacker
7+
class DevServerRunner < Webpacker::Runner
8+
def run
9+
load_config
10+
detect_port!
11+
execute_cmd
12+
end
13+
14+
private
15+
16+
def load_config
17+
@config_file = File.join(@app_path, "config/webpacker.yml")
18+
@default_listen_host_addr = ENV["NODE_ENV"] == "development" ? "localhost" : "0.0.0.0"
19+
20+
dev_server = YAML.load_file(@config_file)[ENV["RAILS_ENV"]]["dev_server"]
21+
22+
@hostname = args("--host") || dev_server["host"]
23+
@port = args("--port") || dev_server["port"]
24+
@https = @argv.include?("--https") || dev_server["https"]
25+
@dev_server_addr = "http#{"s" if @https}://#{@hostname}:#{@port}"
26+
@listen_host_addr = args("--listen-host") || @default_listen_host_addr
27+
28+
rescue Errno::ENOENT, NoMethodError
29+
$stdout.puts "Webpack dev_server configuration not found in #{@config_file}."
30+
$stdout.puts "Please run bundle exec rails webpacker:install to install webpacker"
31+
exit!
32+
end
33+
34+
def detect_port!
35+
server = TCPServer.new(@listen_host_addr, @port)
36+
server.close
37+
38+
rescue Errno::EADDRINUSE
39+
$stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server"
40+
exit!
41+
end
42+
43+
def execute_cmd
44+
argv = @argv.dup
45+
46+
# Delete supplied host, port and listen-host CLI arguments
47+
["--host", "--port", "--listen-host"].each do |arg|
48+
argv.delete(args(arg))
49+
argv.delete(arg)
50+
end
51+
52+
env = { "NODE_PATH" => @node_modules_path.shellescape }
53+
54+
cmd = [
55+
"#{@node_modules_path}/.bin/webpack-dev-server", "--progress", "--color",
56+
"--config", @webpack_config,
57+
"--host", @listen_host_addr,
58+
"--public", "#{@hostname}:#{@port}",
59+
"--port", @port.to_s
60+
] + argv
61+
62+
Dir.chdir(@app_path) do
63+
exec env, *cmd
64+
end
65+
end
66+
67+
def args(key)
68+
index = @argv.index(key)
69+
index ? @argv[index + 1] : nil
70+
end
71+
end
72+
end

lib/webpacker/runner.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Webpacker
2+
class Runner
3+
def self.run(argv)
4+
$stdout.sync = true
5+
6+
new(argv).run
7+
end
8+
9+
def initialize(argv)
10+
@argv = argv
11+
12+
@app_path = File.expand_path(".", Dir.pwd)
13+
@node_modules_path = File.join(@app_path, "node_modules")
14+
@webpack_config = File.join(@app_path, "config/webpack/#{ENV["NODE_ENV"]}.js")
15+
16+
unless File.exist?(@webpack_config)
17+
puts "Webpack config #{@webpack_config} not found, please run 'bundle exec rails webpacker:install' to install webpacker with default configs or add the missing config file for your custom environment."
18+
exit!
19+
end
20+
end
21+
end
22+
end

lib/webpacker/webpack_runner.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "shellwords"
2+
require "webpacker/runner"
3+
4+
module Webpacker
5+
class WebpackRunner < Webpacker::Runner
6+
def run
7+
env = { "NODE_PATH" => @node_modules_path.shellescape }
8+
cmd = [ "#{@node_modules_path}/.bin/webpack", "--config", @webpack_config ] + @argv
9+
10+
Dir.chdir(@app_path) do
11+
exec env, *cmd
12+
end
13+
end
14+
end
15+
end

webpacker.gemspec

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Gem::Specification.new do |s|
99
s.summary = "Use Webpack to manage app-like JavaScript modules in Rails"
1010
s.homepage = "https://github.com/rails/webpacker"
1111
s.license = "MIT"
12+
s.bindir = "exe"
13+
s.executables = `git ls-files -- exe/*`.split("\n").map { |f| File.basename(f) }
1214

1315
s.required_ruby_version = ">= 1.9.3"
1416

0 commit comments

Comments
 (0)