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

devServer v4: options has an unknown property 'before' #88

Merged
merged 5 commits into from
Feb 12, 2022

Conversation

milosivanovic
Copy link
Contributor

@milosivanovic milosivanovic commented Oct 2, 2021

Fixes #83

As per https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md#-breaking-changes, the locations of the websocket parameters for host, port and path have moved. It's important to note that there are TWO types of configurations both specified in the webpack config: the server config, and the client config.

The server config tells webpack-dev-server where to listen, and the client config tells the client how to connect to webpack-dev-server. Under normal circumstances these wouldn't differ, but in the event a proxy is required/preferred for a particular development environment, then the client might need to connect to a different host/port/path in order to reach the server.

These differences are respected in this PR:

  1. If no client configuration is specified, use the server config. (The only hardcoded value here is for the path set to /ws as I'm not sure how to get this default value programmatically from the webpack server config -- the default in the webpacker config is /ws.)
  2. If there is a client configuration specified for host, port or path, then use those values instead.

Tested and working with the following versions:

"webpack": "^5.55.1",
"webpack-cli": "^4.8.0",
"webpack-dev-server": "^4.3.0"

I am seeing this 404 in the developer console though (in my case I'm running webpack-dev-server on port 8081):
Screen Shot 2021-10-01 at 6 45 30 PM
I'm not sure what that's referring to or why it's trying to use sockjs when there is already an established websocket at /ws.

src/index.js Outdated
Comment on lines 15 to 32
sockOptions.sockHost = compiler.options.devServer.client &&
compiler.options.devServer.client.webSocketURL &&
compiler.options.devServer.client.webSocketURL.hostname || compiler.options.devServer.host
sockOptions.sockPath = (
compiler.options.devServer.client &&
compiler.options.devServer.client.webSocketURL &&
compiler.options.devServer.client.webSocketURL.pathname
)
||
(
compiler.options.devServer.webSocketServer &&
compiler.options.devServer.webSocketServer.options &&
compiler.options.devServer.webSocketServer.options.path
)
|| '/ws'
sockOptions.sockPort = compiler.options.devServer.client &&
compiler.options.devServer.client.webSocketURL &&
compiler.options.devServer.client.webSocketURL.port || compiler.options.devServer.port

Choose a reason for hiding this comment

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

Looks like yarn format needs to be run on this file

Choose a reason for hiding this comment

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

looks good!

src/index.js Outdated
Comment on lines 15 to 17
sockOptions.sockHost = compiler.options.devServer.client &&
compiler.options.devServer.client.webSocketURL &&
compiler.options.devServer.client.webSocketURL.hostname || compiler.options.devServer.host

Choose a reason for hiding this comment

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

Could you use compiler.options.devServer.client?.webSocketURL?.hostname here? Would be more readable if so.

Also, I suggest adding a comment per the description about why this is necessary re: "TWO types of configurations both specified in the webpack config: the server config, and the client config, etc."

Copy link

@thedavidprice thedavidprice left a comment

Choose a reason for hiding this comment

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

Good work chipping away at this @milosivanovic I left a couple comments that aren't critical to address.

I tried this on an SPA test project using:
webpack 5.57.1
webpack-cli 4.8.0
webpack-dev-server 4.3.1

Dev server ran successfully and is displaying error overlay. However, I'm seeing the same console error along with several others:
Screen Shot 2021-10-08 at 11 05 52 PM

These errors are only present when the ErrorOverlayPlugin is used. I did some digging but was unable to diagnose.

The "unexpected token" errors is particularly confusing. Failing the app's index.html... 🤔

Screen Shot 2021-10-08 at 11 15 23 PM

Wish I could be of more help but I do hope this adds some positive momentum.

@milosivanovic
Copy link
Contributor Author

@thedavidprice Thanks for your review! I hope I addressed those comments in my latest commit.

The 404 error is strange, and yes, it does only show when the plugin is used. I can say that the functionality works despite the 404, which puts the plugin in a better place than it is on the main branch (not working at all.) We could either try to resolve that in this PR, or commit a subsequent PR after the issue is identified. I'll leave that up for the maintainer to decide.

@gregberge When you have time to check this out, let us know what you think.

@thedavidprice
Copy link

Changes look good!

I think we need to resolve the 404. But that's up to @gregberge

@mrnav90
Copy link

mrnav90 commented Nov 8, 2021

@gregberge
Please help check and merge this PR.
We need this update for webpack 5.

Thanks

@iMoses-Apiiro
Copy link

any updates? we're waiting on this fix otherwise this package is unusable for us

@gondar00
Copy link

Guys can we please get this merged? @thedavidprice @milosivanovic

@thedavidprice
Copy link

Sorry all, I've no permissions for this repo. And we've now removed the package from RedwoodJS due to the incompatibility so I can't take on responsibility here.

Nudging @gregberge

@gregberge gregberge merged commit 28de3ed into gregberge:main Feb 12, 2022
@milosivanovic milosivanovic deleted the fix-issue-83 branch February 12, 2022 18:07
Comment on lines -26 to +40
const originalBefore = options.devServer.before
options.devServer.before = (app, server) => {
if (originalBefore) {
originalBefore(app, server, compiler)
const originalOnBeforeSetupMiddleware =
options.devServer.onBeforeSetupMiddleware
options.devServer.onBeforeSetupMiddleware = (devServer) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This broke compatibility with webpack-dev-server v3.

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.

Options has an unknown property 'before'
7 participants