-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binstubs #833
Binstubs #833
Changes from 5 commits
2b098fb
c94ba2e
7d0c515
04b629e
518d1cf
1bf8d1c
2653876
7d181fa
4d8d4f2
10997b8
1a8d0bd
03c8b09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env ruby | ||
|
||
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development" | ||
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"] | ||
|
||
require "webpacker" | ||
require "webpacker/runner" | ||
Webpacker::Runner.run(ARGV) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env ruby | ||
|
||
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development" | ||
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"] | ||
|
||
require "webpacker" | ||
require "webpacker/dev_server_runner" | ||
Webpacker::DevServerRunner.run(ARGV) |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require "shellwords" | ||
require "yaml" | ||
require "socket" | ||
|
||
module Webpacker | ||
class DevServerRunner | ||
def self.run(argv) | ||
$stdout.sync = true | ||
|
||
new(argv).run | ||
end | ||
|
||
def initialize(argv) | ||
@argv = argv | ||
|
||
@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/#{ENV["NODE_ENV"]}.js") | ||
@default_listen_host_addr = ENV["NODE_ENV"] == 'development' ? 'localhost' : '0.0.0.0' | ||
|
||
begin | ||
dev_server = YAML.load_file(@config_file)[ENV["RAILS_ENV"]]["dev_server"] | ||
|
||
@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 | ||
$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 | ||
end | ||
|
||
def run | ||
check_server! | ||
update_argv | ||
execute_cmd | ||
end | ||
|
||
private | ||
def check_server! | ||
server = TCPServer.new(@listen_host_addr, @port) | ||
server.close | ||
|
||
rescue Errno::EADDRINUSE | ||
$stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server" | ||
exit! | ||
end | ||
|
||
def update_argv | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this empty method here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, removing! |
||
end | ||
|
||
def execute_cmd | ||
argv = @argv | ||
|
||
# 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 env, *cmd | ||
end | ||
end | ||
|
||
def args(key) | ||
index = @argv.index(key) | ||
index ? @argv[index + 1] : nil | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "shellwords" | ||
|
||
module Webpacker | ||
class Runner | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like: # lib/webpacker/runners/webpack.rb
class Webpacker::WebpackRunner < Webpacker::Runner
# lib/webpacker/runners/dev_server.rb
class Webpacker::DevServerRunner < Webpacker::Runner |
||
def self.run(argv) | ||
$stdout.sync = true | ||
|
||
new(argv).run | ||
end | ||
|
||
def initialize(argv) | ||
@argv = argv | ||
|
||
@app_path = File.expand_path("../", __dir__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, good catch—error in local testing. I may need to pass the app_path into the runner which may mean keeping the install template with less code instead of using a bundler binstub. |
||
@node_modules_path = File.join(@app_path, "node_modules") | ||
@webpack_config = File.join(@app_path, "config/webpack/#{NODE_ENV}.js") | ||
|
||
unless File.exist?(@webpack_config) | ||
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." | ||
exit! | ||
end | ||
end | ||
|
||
def run | ||
env = { "NODE_PATH" => @node_modules_path.shellescape } | ||
cmd = [ "#{@node_modules_path}/.bin/webpack", "--config", @webpack_config ] + @argv | ||
|
||
Dir.chdir(@app_path) do | ||
exec env, *cmd | ||
end | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be we can load options from
Webpacker.config
ordev_server
itself instead of loading the yml again?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to avoid that duplication. Currently,
Webpacker.dev_server
inherits formWebpacker::Instance
which depends onRails
(forRails.root
here) and I think it would be preferable to avoid depending on Rails to execute these scripts. Perhaps a future improvement?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense 👍