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

automatically configure wsPort in html file #1147

Merged
merged 1 commit into from
Oct 14, 2020

Conversation

MoonBall
Copy link
Contributor

@MoonBall MoonBall commented Sep 29, 2020

Changes

Part 2 of #1041.
The change is adding window.HMR_WEBSOCKET_PORT in hmr-client.js file and only setting HMR_WEBSOCKET_PORT in production mode.

Testing

manually test 5 cases:

  1. run yarn build --watch --hmr --hmrPort=15555. check setting window.HMR_WEBSOCKET_PORT=15555.
  2. run yarn build --watch --hmr. check setting window.HMR_WEBSOCKET_PORT=12321.
  3. run yarn build --watch --hmr and set window.HMR_WEBSOCKET_URL in html file. check that window.HMR_WEBSOCKET_URL takes precedence over window.HMR_WEBSOCKET_PORT.
  4. run yarn build --watch. check no generating hmr code.
  5. run yarn start. check no generating code of setting HMR_WEBSOCKET_PORT.
  6. run yarn start --hmrPort=15555. check setting window.HMR_WEBSOCKET_PORT=15555 and hmr server listens on 15555 port.

@vercel
Copy link

vercel bot commented Sep 29, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/ixf69davx
✅ Preview: https://snowpack-git-feat-configure-hmr-port.pikapkg.vercel.app

@@ -75,7 +75,12 @@ export function wrapHtmlResponse({
});

if (hmr) {
const hmrScript = `<script type="module" src="${getMetaUrlPath('hmr-client.js', config)}"></script><script type="module" src="${getMetaUrlPath('hmr-error-overlay.js', config)}"></script>`;
let hmrScript = ``;
if (mode === 'production' && config.devOptions.hmrPort) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not a good way to check whether mode is production. need your suggestions.

Copy link
Owner

Choose a reason for hiding this comment

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

Why only production? Does this not work in dev mode?

Copy link
Owner

Choose a reason for hiding this comment

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

bump! would love an answer so that we can resolve and merge!

Copy link
Contributor Author

@MoonBall MoonBall Oct 13, 2020

Choose a reason for hiding this comment

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

@FredKSchott sorry, I've been very busy recently.

Why only production? Does this not work in dev mode?

It's a good problem. I think hmrPort should work in dev mode 🤣.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@FredKSchott I made a fix and the hmrPort option would take effect in dev mode. I manually test 6 cases above.

Copy link
Owner

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM with one final change! Thanks for helping push this over the line

@@ -49,7 +49,7 @@ const DEFAULT_CONFIG: Partial<SnowpackConfig> = {
output: 'dashboard',
fallback: 'index.html',
hmrDelay: 0,
hmrPort: 12321,
Copy link
Owner

Choose a reason for hiding this comment

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

Any reason to remove this? Whenever possible, it's good to give these defaults so that the rest of the codebase doesn't need to handle undefined values. So I'd vote to keep this here and remove DEFAULT_PORT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we set default hmrPort to 12321, we cannot distinguish between snowpack dev and snowpack dev --hmrPort=12321. Because we use port of devServer as hmrEngine's port while using snowpack dev, but we use 12321 as hmrEngine's port while using snowpack dev --hmrPort=12321.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@FredKSchott There is another way to implement it. we can add a DEFAULT_BUILD_CONFIG in this file and merge DEFAULT_BUILD_CONFIG to config if snowpack is in build mode. The code of DEFAULT_CONFIG and DEFAULT_BUILD_CONFIG is as below.

const DEFAULT_CONFIG = {
  // other default config
  devOptions: {
     hmrPort: undefined,
  }
}

const DEFAULT_BUILD_CONFIG = {
  devOptions: {
     hmrPort: 12321,
  }
}

I prefer this way since we usually need to set different configuration according to whether snowpack runs in dev mode or build mode.

I would like to use this way to modify this pr if you agree with it 😁.

Copy link
Owner

Choose a reason for hiding this comment

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

Ah right, of course. Okay, let's keep as it is now for this PR. If you'd like to create a follow up PR to simplify this config further, that would be great!

Copy link
Owner

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM!

@FredKSchott FredKSchott merged commit a7991a8 into FredKSchott:master Oct 14, 2020
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.

None yet

2 participants