leegeunhyeok / react-native-esbuild

⚡️ esbuild based universal bundler for React Native
https://react-native-esbuild.vercel.app
MIT License
190 stars 10 forks source link

Supports hot reload #38

Open leegeunhyeok opened 12 months ago

leegeunhyeok commented 12 months ago

Description

Supports hot reload on development environment.

Tasks


References

Limitations

Notes


// 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 commented 11 months ago

Notes

1st PoC

https://github.com/leegeunhyeok/react-native-esbuild/assets/26512984/4ff44d54-57c7-4bbc-942f-4c2fb7b4e4fe

leegeunhyeok commented 11 months ago

Notes

https://github.com/leegeunhyeok/react-native-esbuild/assets/26512984/8249589c-ceba-43d9-a8e3-aa9e49d830d2

leegeunhyeok commented 10 months ago

Notes

image
leegeunhyeok commented 9 months ago

Notes

https://github.com/leegeunhyeok/react-native-esbuild/assets/26512984/f384f89b-6766-4ad4-8291-d6cc3470e252

Reverse dependencies

image image image
leegeunhyeok commented 9 months ago

Mistake > https://github.com/leegeunhyeok/react-native-esbuild/commit/de55ffb175d66437e18c8075122d94524eefdcb0

leegeunhyeok commented 9 months ago

Notes

benjamin7zero commented 3 months ago

please bring HMR to this project, it would be crazy