Wener Notes

react-compiler

约 1 分钟阅读
标签:编译器
bash
pnpm add -D babel-plugin-react-compiler
TypeScript
// 部分
const ReactCompilerConfig = {
  // annotation opt-in 模式,组件增加 'use memo'
  compilationMode: 'annotation',
  sources: (filename) => {
    return filename.indexOf('src/path/to/dir') !== -1;
  },
};

vite

vite.config.ts
const ReactCompilerConfig = {
  /* ... */
};

export default defineConfig(() => {
  return {
    plugins: [
      react({
        babel: {
          plugins: [['babel-plugin-react-compiler', ReactCompilerConfig]],
        },
      }),
    ],
    // ...
  };
});
TypeScript
export default defineConfig(({ command }) => {
  const babelPlugins = [['babel-plugin-react-compiler', {}]];
  if (command === 'serve') {
    babelPlugins.push(['@babel/plugin-transform-react-jsx-development', {}]);
  }

  return {
    plugins: [react({ babel: { plugins: babelPlugins } })],
  };
});

NextJS

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    reactCompiler: true,
  },
};

lint

bash
npx -y react-compiler-healthcheck@latest --src 'src/**/*.{tsx,ts}'
bash
npm install eslint-plugin-react-compiler
JavaScript
module.exports = {
  plugins: ['eslint-plugin-react-compiler'],
  rules: {
    'react-compiler/react-compiler': 'error',
  },
};

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • million
    笔记 · react compiler

    aidenybai/million · MIT · <4kb · The Virtual DOM Replacement for React · https://million.dev/blog/virtual-dom · Diff the data, not the DOM.

References

其他外链4 条
最近更新commit 4d57414Edit

On this page