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

Supports hot reload #38

Open
12 of 13 tasks
leegeunhyeok opened this issue Oct 11, 2023 · 8 comments
Open
12 of 13 tasks

Supports hot reload #38

leegeunhyeok opened this issue Oct 11, 2023 · 8 comments
Assignees
Labels

Comments

@leegeunhyeok
Copy link
Owner

leegeunhyeok commented Oct 11, 2023

Description

Supports hot reload on development environment.

Tasks

  • Implement custom file watcher instead esbuild.context.watch()
    • On some files change, trigger rebuild() and get the metadata(path, etc..) of the changed module.
    • Implement custom file system watcher using chokidar
  • Transformer
    • Skip if file is not changed (transform changed file only)
    • Wrap modules with react-refresh context boundary
    • Custom module system
    • Send HMR update message to client via web socket
    • Evaluate updated code on client runtime
  • Get updated module code from bundle
  • Add experimental config for toggle hot reload

References

Limitations

  • Esbuild isn't supports HMR(Hot Module Replacement).
  • Cannot skip transform(js, ts) loader in Esbuild.

Notes

  • 0.1.0-alpha.42

// before
var __commonJS = (cb, mod) => function __require2() {
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};

// after
global.cachedModule = {};
var __commonJS = (cb, mod) => {
  var name = __getOwnPropNames(cb)[0];
  Object.defineProperty(global.cachedModule, name, {
    get: () => mod
  });
  return function __require2() {
    return mod || (0, cb[name])((mod = { exports: {} }).exports, mod), mod.exports;
  };
};

// when file changes detected -> don't rebuild, just transform target file with swc only.
// `import { a, b, c } from 'module-path';`

try {
  const { a, b, c } = global.cachedModule['module-path'];

  // ...
} catch (error) {
  // hot reload failed. fully reload instead
}
// Esbuild metafile
interface Metafile {
  // `inputs` key is module file path.
  inputs: Record<string, {
    byte: number;
    imports: {
      path: string; // eg. 'node_modules/react/cjs/index.js'
      original: string; // eg. 'react'
      kind: '...',
    }[];
  }>[],
  // ...
}

// {
//   react: 'node_modules/react/cjs/index.js',
//   '../components': 'src/components/index.ts',
// }
const mappedModules = metafile.inputs[changedFilePath]?.imports.reduce((prev, curr) => {
  return { ...prev, [curr.original]: curr.path };
}, {});

// Example usage
import { transform } from '@swc/core';

await transform(code, {
  jsc: {
    experimental: {
      plugins: [
        // https://github.com/leegeunhyeok/swc-plugin-react-native-esbuild-module
        ['react-native-esbuild-module-plugin', {
          alias: mappedModules ?? {},
        }],
      ],
    },
  },
});
@leegeunhyeok leegeunhyeok self-assigned this Oct 11, 2023
@leegeunhyeok leegeunhyeok converted this from a draft issue Oct 11, 2023
@leegeunhyeok leegeunhyeok moved this from Todo to In Progress in react-native-esbuild Oct 18, 2023
@leegeunhyeok
Copy link
Owner Author

leegeunhyeok commented Oct 19, 2023

Notes

1st PoC

Screen.Recording.2023-10-20.at.12.50.02.AM.mov

@leegeunhyeok
Copy link
Owner Author

leegeunhyeok commented Oct 22, 2023

Notes

  • Implement swc plugin for react-refresh (https://github.com/leegeunhyeok/swc-plugin-react-refresh)
    • Register React components
    • Create signature when component is using hooks
  • Find updated module and its transformed code
    • Enhance file system watcher
    • Enhance bundled code (add boundary to each modules for find it)
  • And.. many things
Screen.Recording.2023-10-23.at.1.34.59.AM.mov

@leegeunhyeok leegeunhyeok changed the title HMR Support Supports HMR Oct 22, 2023
@leegeunhyeok leegeunhyeok changed the title Supports HMR Supports Hot reload Oct 23, 2023
@leegeunhyeok leegeunhyeok changed the title Supports Hot reload Supports hot reload Oct 23, 2023
@leegeunhyeok leegeunhyeok pinned this issue Oct 24, 2023
leegeunhyeok added a commit that referenced this issue Oct 27, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Oct 27, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Oct 27, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Oct 27, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Oct 28, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Nov 20, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
@leegeunhyeok
Copy link
Owner Author

Notes

  • Search reverse dependecies
  • Actual module path (eg. like a '../Component' to /path/to/source/Component.tsx)
  • Custom Hot Module Replacement API
image

leegeunhyeok added a commit that referenced this issue Dec 12, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Dec 12, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Dec 13, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
@leegeunhyeok
Copy link
Owner Author

leegeunhyeok commented Dec 14, 2023

Notes

Screen.Recording.2023-12-14.at.22.23.52.mov

Reverse dependencies

image image image

leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- supports hot reloading with `react-refresh`
  - currently, only React component can be hot reload
- add hot reload runtime script and inject into entry

close #38
leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- now supports HMR
  - In node_modules/* modules are not supported HMR yet.
  - Only supports <projectRoot>

close #38
leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
@github-project-automation github-project-automation bot moved this from In Progress to Done in react-native-esbuild Dec 14, 2023
leegeunhyeok added a commit that referenced this issue Dec 14, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 16, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 16, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 16, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
@leegeunhyeok leegeunhyeok reopened this Dec 16, 2023
@leegeunhyeok
Copy link
Owner Author

Mistake > de55ffb

leegeunhyeok added a commit that referenced this issue Dec 17, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 26, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
@leegeunhyeok leegeunhyeok moved this from Done to In Progress in react-native-esbuild Dec 27, 2023
leegeunhyeok added a commit that referenced this issue Dec 27, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 29, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 29, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 29, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 29, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
leegeunhyeok added a commit that referenced this issue Dec 30, 2023
- now supports HMR
- limitations
  - in node_modules/* modules are not supported HMR yet.
- custom runtime module system by `swc-plugin-global-module`
  - https://github.com/leegeunhyeok/swc-plugin-global-module

close #38
@leegeunhyeok
Copy link
Owner Author

Notes

@benjamin7zero
Copy link

please bring HMR to this project, it would be crazy

@laogui
Copy link

laogui commented Nov 5, 2024

I have been waiting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: In Progress
Development

No branches or pull requests

3 participants