-
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
Merged
Merged
Binstubs #833
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b098fb
Convert webpack-dev-server to binstub
rossta c94ba2e
Remove webpack-dev-server install template
rossta 7d0c515
Convert webpack executable to binstub
rossta 04b629e
Remove bin/webpack install template
rossta 518d1cf
Update install task for binstubs: assumes bundler
rossta 1bf8d1c
Fix dev server runner
rossta 2653876
Fix webpack runner
rossta 7d181fa
Move binstubs to exe directory
rossta 4d8d4f2
Rename runner as webpack_runner
rossta 10997b8
Update dev server runner
rossta 1a8d0bd
Extract superclass for command runners
rossta 03c8b09
Rename method
rossta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/webpack_runner" | ||
Webpacker::WebpackRunner.run(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "shellwords" | ||
require "yaml" | ||
require "socket" | ||
require "webpacker/runner" | ||
|
||
module Webpacker | ||
class DevServerRunner < Webpacker::Runner | ||
def run | ||
load_config | ||
detect_port! | ||
execute_cmd | ||
end | ||
|
||
private | ||
|
||
def load_config | ||
@config_file = File.join(@app_path, "config/webpacker.yml") | ||
@default_listen_host_addr = ENV["NODE_ENV"] == "development" ? "localhost" : "0.0.0.0" | ||
|
||
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 | ||
|
||
def detect_port! | ||
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 execute_cmd | ||
argv = @argv.dup | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Webpacker | ||
class Runner | ||
def self.run(argv) | ||
$stdout.sync = true | ||
|
||
new(argv).run | ||
end | ||
|
||
def initialize(argv) | ||
@argv = argv | ||
|
||
@app_path = File.expand_path(".", Dir.pwd) | ||
@node_modules_path = File.join(@app_path, "node_modules") | ||
@webpack_config = File.join(@app_path, "config/webpack/#{ENV["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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "shellwords" | ||
require "webpacker/runner" | ||
|
||
module Webpacker | ||
class WebpackRunner < Webpacker::Runner | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using
Dir.pwd
has pros and cons: we can install the executables as binstubs with bundler (which won't supply the application dir context) but we wouldn't be able to execute the binstubs from anywhere but the project root. I'm unsure of how this might affect various deployment scenarios.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.
Yeah... maybe you can leverage the root_path accessor set via webpacker itself which defaults to Rails.root? Not sure what's best in this context?
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.
Rails.root
would make it easy but I was hoping to avoid relying on loading rails since these are just thin wrappers around node executables.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.
Some alternatives:
APP_PATH
to override the defaultThere 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.
Yeah, lets tackle that in another PR