React 19以下でもReact Compilerを有効化する
はじめに
一応React Compilerは、React 19を必須としていますが、なくても動きます
Reactチームの方によりますと、React 19のキャッシュ機構が過去のバージョンより効率が良いとされていますね。
しかしReactチームは、React 19じゃないプロジェクトでもReact Compilerが動くようにpolyfill
を用意してくれてます。
the gist isn’t unsafe, it’s not efficient and not maintained basically. so yes, it is viable as a stop gap solution for the short term.
このpolyfillは安全ではないということではなく、一時的な解決策として
とされてますね。非効率かつメンテされないので本番適用は自己責任でお願いします😗
入れてみる
React v17.0.2
+ Vite
のプロジェクトに導入しました。
最初は以下に従って、ViteのプロジェクトにReact Compilerを入れます。
- React Compilerインストール
pnpm add babel-plugin-react-compiler
vite.config.js
を以下のように設定します。
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
const ReactCompilerConfig = {};
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
babel: {
plugins: [["babel-plugin-react-compiler", ReactCompilerConfig]],
},
}),
],
});
-
polyfill
を導入する
root
にてpolyfillの置く場所を作成します。自分はlib
のフォルダーを作成し、中にreact-compiler-runtime
のフォルダーを置きました。
lib/react-compiler-runtime
配下にて以下のファイルとその内容を作成します。
{
"name": "react-compiler-runtime",
"version": "0.0.1",
"license": "MIT",
"main": "index.js",
"dependencies": {
"react": "^18.2.0"
}
}
const React = require("react");
const $empty = Symbol.for("react.memo_cache_sentinel");
/**
* DANGER: this hook is NEVER meant to be called directly!
*
* Note that this is a temporary userspace implementation of this function
* from React 19. It is not as efficient and may invalidate more frequently
* than the official API. Please upgrade to React 19 as soon as you can.
**/
export function c(size) {
return React.useState(() => {
const $ = new Array(size);
for (let ii = 0; ii < size; ii++) {
$[ii] = $empty;
}
// @ts-ignore
$[$empty] = true;
return $;
})[0];
}
作成後、lib/react-compiler-runtime
配下に遷移し、依存パッケージをインストールします
-
polyfill
を依存パッケージとして設定する
root
のpackage.json
にて以下のように設定しました
"dependencies":{
"react-compiler-runtime": "file:./lib/react-compiler-runtime"
....
file:
と設定し./lib/react-compiler-runtime
作成したpolyfill
のパスを設定します。
4.React Compilerのbabel
設定をpolyfill
に向かせる
Vite
使っていますので、vite.config.js
の設定変更します。
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
const ReactCompilerConfig = { runtimeModule: "react-compiler-runtime" }; // 👈react-compiler-runtimeを設定
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
babel: {
plugins: [
[
"babel-plugin-react-compiler",
ReactCompilerConfig
],
],
},
}),
],
});
これで設定完了です。
開発環境を立ち上げ挙動確認をしましょう。
Reactの開発ツールにてHighlight updates when components render.
をオンしましょう。
開発ツールにてMemo✨
とタッグ付されているコンポーネントはReact Compilerによってメモ化されています!
参考レポジトリを以下にて確認できますので、ぜひ確認してください
Discussion
React 18で試しても
とエラー出力される方は、
@vitejs/plugin-react
を4.2.1
にダウングレードしてみてください