-
Notifications
You must be signed in to change notification settings - Fork 916
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 hmr for build #1008
add hmr for build #1008
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/pikapkg/snowpack/7434oity3 [update for d8042ca canceled] |
7520e9a
to
5da274f
Compare
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.
Nice! Glad to see that this looks pretty straightforward. Since we still don't have dev tests, can you confirm that you tested this manually yourself?
@@ -75,7 +77,8 @@ export function wrapHtmlResponse({ | |||
}); | |||
|
|||
if (hmr) { | |||
const hmrScript = `<script type="module" src="${getMetaUrlPath('hmr.js', config)}"></script>`; | |||
const setHmrUrlScript = hmrUrl ? `<script type="text/javascript">window.HMR_WEBSOCKET_URL="${hmrUrl}";</script>\n` : ''; |
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.
no need for this, we already have a section in our docs telling users how to configure this themselves outside of Snowpack.
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.
Our default port is 12321. how to inform users what is url of websocket if we don't set HMR_WEBSOCKET_URL by ourselves?
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.
There's definitely still work to be done here, but I'd rather do it all together in a follow up PR and leave this existing limitation in place for now.
The reason is that this was always a bandaid, and now we may just want to move this entirely into a Snowpack config value, and then remove the window. HMR_WEBSOCKET_URL
snippet entirely.
Because ESM-HMR is shared we can't remove window.HMR_WEBSOCKET_URL
entirely, but Snowpack could inject it into the hmr.js
file itself, instead:
// hmr.js
// injected by Snowpack during dev & build, if config value is set
window.HMR_WEBSOCKET_URL = ${"VALUE FROM SNOWPACK CONFIG}
// the static hmr.js file contents
...
@@ -449,6 +460,10 @@ export async function command(commandOptions: CommandOptions) { | |||
await changedPipelineFile.writeProxyToDisk(builtFile); | |||
} | |||
} | |||
|
|||
if (hmrEngine) { | |||
hmrEngine.broadcastMessage({type: 'reload'}); |
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.
👍
I tested and I will test again after fix these nits. |
5da274f
to
5666c15
Compare
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.
Looking good! A few more comments added, but overall direction is 👍
@@ -75,7 +77,8 @@ export function wrapHtmlResponse({ | |||
}); | |||
|
|||
if (hmr) { | |||
const hmrScript = `<script type="module" src="${getMetaUrlPath('hmr.js', config)}"></script>`; | |||
const setHmrUrlScript = hmrUrl ? `<script type="text/javascript">window.HMR_WEBSOCKET_URL="${hmrUrl}";</script>\n` : ''; |
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.
There's definitely still work to be done here, but I'd rather do it all together in a follow up PR and leave this existing limitation in place for now.
The reason is that this was always a bandaid, and now we may just want to move this entirely into a Snowpack config value, and then remove the window. HMR_WEBSOCKET_URL
snippet entirely.
Because ESM-HMR is shared we can't remove window.HMR_WEBSOCKET_URL
entirely, but Snowpack could inject it into the hmr.js
file itself, instead:
// hmr.js
// injected by Snowpack during dev & build, if config value is set
window.HMR_WEBSOCKET_URL = ${"VALUE FROM SNOWPACK CONFIG}
// the static hmr.js file contents
...
snowpack/src/commands/build.ts
Outdated
|
||
const CONCURRENT_WORKERS = require('os').cpus().length; | ||
|
||
let hmrEngine: EsmHmrEngine | null = null; | ||
function getIsHmrEnabled(config: SnowpackConfig) { | ||
return config.buildOptions.watch && config.devOptions.hmr; |
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.
So I just checked config.ts
, and config.buildOptions.watch
is true by default. This is a bit tricky since we originally talked about HMR being opt-in for build (at least to start, until we are more confident that it wouldn't be a breaking change).
I'm kind of torn, should we just ship this as opt-in for build? I just think it's so unexpected to be running a separate server during the build command. In that case, we need to remove the default hmr
value from DEFAULT_CONFIG
and then handle it as default "on" for dev so that this code you have here is opt-in and runs as expected.
// in dev.ts
- const {hmr: isHmr} = config.devOptions;
+ const isHmr = config.devOptions.hmr || true;
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.
There's definitely still work to be done here, but I'd rather do it all together in a follow up PR and leave this existing limitation in place for now.
maybe we should print a message to inform user about web socket's address.
Edit: I deleted the script of setting url of web socket.
Edit: add message in https://github.com/pikapkg/snowpack/pull/1008/files#diff-9e8fbf411449d99e70310e93d8119617R302
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.
Yup, that makes sense to me
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.
In that case, we need to remove the default hmr value from DEFAULT_CONFIG and then handle it as default "on" for dev so that this code you have here is opt-in and runs as expected.
The config.devOptions.hmr
will be undefined
and not match our config schema if we don't set the hmr
value in loadAndValidateConfig()
function. I prefer to set hmr value in loadAndValidateConfig()
function according to dev
or build
.
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.
Our config functions don't know what command they are being run in.
We give commands the chance to modify the config at startup, but at that point it's too late to tell whether the original option was undefined or true/false.
It's okay to make this optional in the config schema as well, there are several places where we already do this. The current behavior isn't changing, just the hmr
config value would now support undefined, which lets the different commands handle this case differently.
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.
Ok, modified 😁
5666c15
to
dcd347c
Compare
dcd347c
to
d8042ca
Compare
I tested 4 cases.
|
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.
Awesome stuff, LGTM!
Great work guys! I just pulled it down, and tested in our app and it works great! Very simple. Loving snowpack. |
I'll just say I still haven't gotten |
When I add I believe it's because |
I think there are a few convos going on related to this issue. I just tried to reproduce and can confirm that The HMR server needs to understand your application to properly bundle HMR events to the correct accept() handler. In dev, we scan files as we serve them. In build, we should scan them when we build them. Issue added to prioritize here: #1935 |
Changes
Part1 of #1002.
I test it by run
yarn build --watch --hmr
command in create-snowpack-app/app-template-react directory.Testing
no test added.