-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Reload html on html asset change #90
Conversation
I feel like the README changes should go in a different Pull Request... this looks a bit messy. |
Agreed. That's a mistake. Will fix! |
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.
Consider changing this to just use a string
src/HMRServer.js
Outdated
emitReload() { | ||
let msg = JSON.stringify({ | ||
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.
Why would you stringify a JSON literal right after creating it o_0? I’m sure it’s not a huge performance penalty, but it feels a bit wrong
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.
We are sending the string through the socket. See line 45.
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.
Ya exactly, so why not just keep it a string the whole time?
So instead of:
let msg = JSON.stringify({
foo: 'bar'
})
We can do:
let msg = `{
foo: ‘bar’
}`
You get me? Like in the original, we are creating a new JavaScript object using JSON literal notation, and then passing it into JSON.stringify
as a parameter, and then taking the outputted string and assigning it to the variable msg
.
What I’m saying, is to just make it a string in the first place.
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.
agreed with @davidnagli, if parcel is aiming to be fast, we can't spent time on stringify'ing object, even with native JSON.stringify
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.
@davidnagli It's a mistake to optimize prematurely at the expense of code quality.
There are other reasons to prefer the JSON.stringify version. For example, webstorm IDE support will be able to detect the keys and values being used, but can't for string literals. When refractoring code, typos become harder to detect, since you don't get syntax highlighting. And if a refactor wants to interpolate some data into the object, you'll have to revert it to JSON.stringify anyway.
That said, yeah, you could do `{type: 'reload'}`
if you really wanted to, but there is zero chance that a reload emission would impact performance in any measurable way.
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.
Yeah, JSON.stringify is not going to affect perf. leave it.
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.
This might cause the page to be reloaded when you edit a page that you're not currently on since it emits reload for any HTML asset that changed. Not sure how we'd detect the current page though, so might be ok to start with. Thoughts?
src/Bundler.js
Outdated
@@ -208,7 +214,11 @@ class Bundler extends EventEmitter { | |||
// Emit an HMR update for any new assets (that don't have a parent bundle yet) | |||
// plus the asset that actually changed. | |||
if (this.hmr && !isInitialBundle) { | |||
this.hmr.emitUpdate([...this.findOrphanAssets(), ...loadedAssets]); | |||
if (didProcessHtmlAsset) { |
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.
Can we do the detection of HTML assets in the HMRServer
instead of here? All of the updated assets are already passed to emitUpdate
so it should be easy to do the detection in there.
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'll do that. thanks
@devongovett I did think about that but couldn't find a way to identify the current HTML file. We could restrict the reload only when the entry file is an HTML file and reload only when then entry file changes. What do you think? |
Has this been published to npm yet? I just installed parcel and changing html file still doesn't reload the page. EDIT: nevermind, it works only if I include at least one file. As soon as I included a css file, it started working. |
I have the same problem as @dumptyd. I often like to test things in a single html file and parcel seemed perfect to provide autoreload functionality for this case. Is it possible to activate the reload functionality for this case as well? |
@therealshark please open up a new feature request for this Sent with GitHawk |
Fixes #83