Skip to content
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

Add dev server config class and setup proxy #637

Merged
merged 28 commits into from
Aug 12, 2017
Merged

Conversation

gauravtiwari
Copy link
Member

@gauravtiwari gauravtiwari commented Aug 12, 2017

This PR adds a few things related to webpack-dev-server:

  1. A dev server config class that exposes config options
Webpacker.dev_server.running?
  1. A rack middleware that proxies webpacker requests to dev server so we can always serve from same-origin and the lookup works out of the box - no more paths prefixing
   if Rails::VERSION::MAJOR >= 5
      app.middleware.insert_before 0, Webpacker::DevServerProxy, ssl_verify_none: true
    else
      config.middleware.insert_before 0, "Webpacker::DevServerProxy", ssl_verify_none: true
    end

screen shot 2017-08-12 at 12 16 06

  1. HTTPS ssl settings for webpack-dev-server
# Absolute paths to ssl key and certificate
    ssl_key_path:
    ssl_cert_path:
  1. Move dev-server-settings to defaults so it's transparently available in all environments

  2. The host info has been removed from manifest.json, now looks like this:

{
  "hello_react.js": "/packs/hello_react.js"
}

Add options for ssl and set publicPath to only pack output path
Add dev_server config
Move dev-server to defaults
Move to private

Fix paths

Update path names
@gauravtiwari gauravtiwari requested a review from dhh August 12, 2017 11:13
Fix typos

Delete if path exists
@gauravtiwari
Copy link
Member Author

gauravtiwari commented Aug 12, 2017

There is one extra bit of change here i.e. renaming packs to packs-test inside tests - apparently CI is setting RAILS_ENV when running tests and due to output folder being different in webpacker.yml the tests were failing however when running same tests locally the app was booted in development environment.

So I have setup the RAILS.env to be test to avoid any discrepancy:

module TestApp
  class Application < ::Rails::Application
    config.root = File.join(File.dirname(__FILE__), "test_app")
    config.eager_load = true
    ::Rails.env = ENV["RAILS_ENV"] = "test"
  end
end

and renamed all test_app folders from packs to packs-test.

key: readFileSync(devServer.ssl_key_path),
cert: readFileSync(devServer.ssl_cert_path)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do we ever need to bother with HTTPS in development?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhh There were some issues filed where people are using HTTPS for development, mainly for cases where they are interacting with API's that is hosted on HTTPS

@dhh
Copy link
Member

dhh commented Aug 12, 2017 via email

@gauravtiwari
Copy link
Member Author

#612 and #319

@gauravtiwari
Copy link
Member Author

@dhh Should we move the proxy middleware to middlewares folder, guess we gonna have one more middleware for cache-control.

@dhh
Copy link
Member

dhh commented Aug 12, 2017

What do we need for cache-control?

@gauravtiwari
Copy link
Member Author

For expiring cached assets: #267 and #550

@dhh
Copy link
Member

dhh commented Aug 12, 2017

Ah yes. Let's do a separate PR for that.

@gauravtiwari
Copy link
Member Author

Yepp 👍

@dhh dhh merged commit 68b9f08 into master Aug 12, 2017
@dhh dhh deleted the remove-host-from-manifest branch August 12, 2017 19:42
@dhh
Copy link
Member

dhh commented Aug 12, 2017

Actually, if we just use -hash in dev as well, we shouldn't need to explicitly set any headers? But not sure how that'll combine with the dev server.

@gauravtiwari
Copy link
Member Author

Yeah that will work 👍 I doubt this is a problem when using dev-server because it takes care of settings cache-control. I guess this is happening mainly with watcher, where assets are being served by Rails server and getting cached but -hash can totally fix it

@dhh
Copy link
Member

dhh commented Aug 12, 2017

For SSL, we should double down on the reproxy setup. The dev server should not need to know about this. Just like people use nginx to terminate SSL before talking to a puma server.

Copy link
Contributor

@justin808 justin808 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gauravtiwari I have a few questions for you.

@@ -5,7 +5,8 @@ module.exports = {
use: [{
loader: 'file-loader',
options: {
publicPath: output.publicPath,
// Set publicPath with ASSET_HOST env if available so internal assets can use CDN
Copy link
Contributor

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.

@@ -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'),
Copy link
Contributor

Choose a reason for hiding this comment

The 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 public_output_path?

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 // if public_output_path is set to '' i.e. the assets will be outputted at root. Since we are now using proxy, there is no host for publicPath except for internal assets like fonts or images, that's what publicPathWithHost is used for.

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?

Yes, that's just adding source path to webpack module resolver.

class Webpacker::DevServerProxy < Rack::Proxy
def rewrite_response(response)
status, headers, body = response
headers.delete "transfer-encoding"
Copy link
Contributor

Choose a reason for hiding this comment

The 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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants