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

hot reload poc #54

Merged
merged 16 commits into from
Jul 14, 2023
Merged

hot reload poc #54

merged 16 commits into from
Jul 14, 2023

Conversation

lifeart
Copy link
Owner

@lifeart lifeart commented Jul 14, 2023

Todo:

  • Figure out better ast plugin.
  • Re-register components.
  • Try to compare by reference (original class), instead of module name

How to check it?

Run project, edit src/components/Button/index.ts or src/components/Button/template.hbs and see reflected changes on main page.

Also, try to change .gts file.

Upd:
Now works for all components in /components/ folder.


How it's working?

  1. We rely on moduleName metadata (file name) in compiled component template. Using it we map rendered components to updated.
  2. We convert all components to yielded: <Button .../> to <Hot @component={{Button}} as |Button|><Button/></Hot>
  3. Inside Hot component we render tracked {{yield this.component}} and listen for related template file changes (Button), once changed - we assign updated class to this.component.
  4. In Vite we emit change event if Button component changed (to Hot), event contain new component class and moduleName (to allow it to map to any rendered component)
  5. All transforms hidden in ast plugin
import { getComponentTemplate } from '@glimmer/manager';
// here is component definition
/* 
    usage without babel plugins:
    add this to the bottom of the gts component file
*/
if (import.meta.hot) {
  import.meta.hot.accept((newModule) => {
    const tpl = getComponentTemplate(newModule.default);
    const moduleName = tpl().moduleName;
    window.dispatchEvent(
      new CustomEvent("hot-reload", {
        detail: {
          moduleName,
          component: newModule.default,
        },
      })
    );
  });
}
// hot-reloaded component need to be wrapped

<Hot @component='Button' as |Button|><Button>Text</Button></Hot>

catch emitted change events in Hot component!

@netlify
Copy link

netlify bot commented Jul 14, 2023

Deploy Preview for ember-vite ready!

Name Link
🔨 Latest commit 5b2a82a
🔍 Latest deploy log https://app.netlify.com/sites/ember-vite/deploys/64b17147b710270008930017
😎 Deploy Preview https://deploy-preview-54--ember-vite.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions
Copy link

github-actions bot commented Jul 14, 2023

Code Coverage Report

Coverage after merging hot-reload-poc into master

99.55%
Coverage Report
FileLines (%)Funcs (%)Uncovered Lines
addons
   ember-simple-auth.ts12, 16
.../Bootstrap
   index.ts50
.../Hot
   index.ts93.2446, 47, 48, 68, ...
config
   registry.ts85, 99
   router.ts98.0897, 98
helpers
   memory-usage.ts12, 18, 20, 30, ...
modifiers
   click-tracker.ts20, 21

@lifeart lifeart marked this pull request as ready for review July 14, 2023 15:29
@lifeart lifeart merged commit 6330331 into master Jul 14, 2023
10 checks passed
@lifeart lifeart deleted the hot-reload-poc branch July 14, 2023 16:04
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

1 participant