@react-native-esbuild/hmr
TypeScript icon, indicating that this package has built-in type declarations

0.1.0-alpha.43 • Public • Published

@react-native-esbuild/hmr

react-refresh based HMR implementation for @react-native-esbuild

Usage

  1. Add import statement to top of entry file(import 'hmr:runtime';)
  2. Wrap React component module with wrapWithHmrBoundary
import {
  getHmrRuntimeInitializeScript,
  wrapWithHmrBoundary,
  HMR_RUNTIME_IMPORT_NAME
} from '@react-native-esbuild/hmr';

// In esbuild plugin
build.onResolve({ filter: new RegExp(HMR_RUNTIME_IMPORT_NAME) }, (args) => {
  return {
    path: args.path,
    namespace: 'hmr-runtime',
  };
});

build.onLoad({ filter: /(?:.*)/, namespace: 'hmr-runtime' }, (args) => {
  return {
    js: await getHmrRuntimeInitializeScript(),
    loader: 'js',
  };
});

build.onLoad({ filter: /* some filter */ }, (args) => {
  if (isEntryFile) {
    code = await fs.readFile(args.path, 'utf-8');
    code = `import ${HMR_RUNTIME_IMPORT_NAME};\n\n` + code;
    // ...

    return {
      contents: code,
      loader: 'js',
    };
  } else {
    // ...

    return {
      // code, component name, module id
      contents: wrapWithHmrBoundary(code, '', args.path),
      loader: 'js',
    };
  }
});

Package Sidebar

Install

npm i @react-native-esbuild/hmr

Weekly Downloads

0

Version

0.1.0-alpha.43

License

MIT

Unpacked Size

65.4 kB

Total Files

25

Last publish

Collaborators

  • ghlee