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

Reload html on html asset change #90

Merged
merged 4 commits into from
Dec 11, 2017

Conversation

Raathigesh
Copy link
Contributor

@Raathigesh Raathigesh commented Dec 7, 2017

Fixes #83

@Raathigesh Raathigesh changed the title Reload html on html asset change. Fix for #83 #83 Reload html on html asset change Dec 7, 2017
@Raathigesh Raathigesh changed the title #83 Reload html on html asset change Reload html on html asset change Dec 7, 2017
@davidnagli
Copy link
Contributor

I feel like the README changes should go in a different Pull Request... this looks a bit messy.

@Raathigesh
Copy link
Contributor Author

Agreed. That's a mistake. Will fix!

Copy link
Contributor

@davidnagli davidnagli left a 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'
});
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link

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

Copy link
Contributor

@shawwn shawwn Dec 7, 2017

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.

Copy link
Member

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.

Copy link
Member

@devongovett devongovett left a 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) {
Copy link
Member

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.

Copy link
Contributor Author

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

@Raathigesh
Copy link
Contributor Author

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

@devongovett devongovett merged commit 0ff76be into parcel-bundler:master Dec 11, 2017
rakannimer pushed a commit to rakannimer/parcel that referenced this pull request Dec 11, 2017
@dumptyd
Copy link

dumptyd commented Apr 12, 2018

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.

@therealshark
Copy link

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?

@DeMoorJasper
Copy link
Member

@therealshark please open up a new feature request for this

Sent with GitHawk

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.

8 participants