-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add dev server config class and setup proxy #637
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
Changes from all commits
cbd84c6
e63fe45
3e9ae23
ea18549
2cdb999
b83b2ad
dd4f34a
40a2d04
28ea343
53c48ad
87a9df1
e2dd334
e1d027a
6f3dca4
e1261fd
9ade7b4
13fa899
e5b7def
0cdba74
39a6970
a915c2b
0ee002c
74baa53
40e3094
1230635
9308b61
8ea5a1b
0b5ae8e
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 |
---|---|---|
|
@@ -24,7 +24,8 @@ function formatPublicPath(host = '', path = '') { | |
|
||
const output = { | ||
path: resolve('public', settings.public_output_path), | ||
publicPath: formatPublicPath(env.ASSET_HOST, settings.public_output_path) | ||
publicPath: `/${settings.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1'), | ||
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. @gauravtiwari Any chance that you can provide some examples and maybe comments on what's going on here with the formatting of the publicPath. Since publicPathWithHost uses a function, how about extracting the plain publicPath into a function as well. Maybe it's me, but doing a string interpolation that feeds into a regexp replace could be a bit less terse for more readability. I get that you take: public_output_path = "packs" and get public_path ==> "/packs/" But I'm not following on why you'd be doing something like take "abc//packs/" with a double // in the string, you'd change the double slash to a single slash, and you won't change the protocol. Is not having slashes in the in the public_output_path an error that should just be reported? Maybe there's some information missing from the README.md on what you want for the 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. On line 32 below, can you confirm that you're using the adding of the source_path to make non-JS asset inclusion easier? Or are there other reasons? 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. @justin808 I am going to push some cleanup for this whole webpack part in another PR. Basically, the idea was to not end up with
Yes, that's just adding source path to webpack module resolver. |
||
publicPathWithHost: formatPublicPath(env.ASSET_HOST, settings.public_output_path) | ||
} | ||
|
||
let resolvedModules = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Webpacker::DevServer | ||
delegate :config, to: :@webpacker | ||
|
||
def initialize(webpacker) | ||
@webpacker = webpacker | ||
end | ||
|
||
def running? | ||
Socket.tcp(host, port, connect_timeout: 1).close | ||
true | ||
rescue Errno::ECONNREFUSED, NoMethodError | ||
false | ||
end | ||
|
||
def host | ||
fetch(:host) | ||
end | ||
|
||
def port | ||
fetch(:port) | ||
end | ||
|
||
def host_with_port | ||
"#{host}:#{port}" | ||
end | ||
|
||
private | ||
def fetch(key) | ||
config.dev_server.fetch(key, defaults[key]) | ||
end | ||
|
||
def defaults | ||
config.send(:defaults)[:dev_server] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "rack/proxy" | ||
|
||
class Webpacker::DevServerProxy < Rack::Proxy | ||
def rewrite_response(response) | ||
status, headers, body = response | ||
headers.delete "transfer-encoding" | ||
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. Hi @gauravtiwari do you by any chance remember why you needed to delete the Transfer-Encoding header? Maybe something to do with preventing Gzipping? Doing so breaks chunked streaming as per #2196 (review) Currently my PR conditionally deletes the header under the assumption that in some cases it does need deleting, but as suggested it would be even better to get rid of the line if not needed :) |
||
response | ||
end | ||
|
||
def perform_request(env) | ||
if env["PATH_INFO"] =~ /#{public_output_uri_path}/ && Webpacker.dev_server.running? | ||
env["HTTP_HOST"] = Webpacker.dev_server.host_with_port | ||
super(env) | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
def public_output_uri_path | ||
Webpacker.config.public_output_path.relative_path_from(Webpacker.config.public_path) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "webpacker_test_helper" | ||
|
||
class DevServerTest < Minitest::Test | ||
def test_host | ||
assert_equal "localhost", Webpacker.dev_server.host | ||
end | ||
|
||
def test_port | ||
assert_equal Webpacker.dev_server.port, 3035 | ||
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.
@gauravtiwari can you explain this comment a bit more.