rxjs vs rxjs-compat
RxJS のバージョン移行と互換性レイヤーの選択
rxjsrxjs-compat類似パッケージ:

RxJS のバージョン移行と互換性レイヤーの選択

rxjs はリアクティブプログラミングのための公式ライブラリであり、Observable を使って非同期イベントストリームを処理します。rxjs-compat は、RxJS 5 から RxJS 6 への移行を容易にするために作られた互換性パッケージです。現在は RxJS 7 以降が主流であり、rxjs-compat はレガシーなコードベースの維持以外では使用されません。

npmのダウンロードトレンド

3 年

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
rxjs031,6984.5 MB1801年前Apache-2.0
rxjs-compat0---5年前Apache-2.0

RxJS vs rxjs-compat: 本家ライブラリと移行用レイヤーの技術比較

rxjs と rxjs-compat は、同じ ReactiveX のエコシステムに属していますが、その役割と寿命は全く異なります。rxjs は現在も活発に開発されている本家ライブラリであり、rxjs-compat は過去のバージョン(RxJS 5)から新しいバージョン(RxJS 6)へ移行するための一時的な橋渡し役でした。現代のフロントエンド開発において、この二つを並べて比較することは、実は「現在進行形のツール」対「過去の遺産」を比較することに近いです。

📦 パッケージの目的と現状

rxjs は、非同期プログラミングを Observable パターンで処理するための標準ライブラリです。

  • 現在バージョン 7 以降が安定版として推奨されています。
  • ツリーシェイキング(不要なコードの削除)に対応しており、バンドルサイズを小さく保てます。
  • 最新の JavaScript 標準に合わせた API 設計がなされています。
// rxjs: 現在の標準的な使い方
import { of, fromEvent } from 'rxjs';
import { map } from 'rxjs/operators';

const source = of(1, 2, 3);
source.pipe(map(x => x * 2)).subscribe(console.log);

rxjs-compat は、RxJS 5 の書き方を RxJS 6 環境で動かすための互換性レイヤーです。

  • RxJS 6 リリース時に、既存コードをすぐに書き換えられないチームのために提供されました。
  • 内部で rxjs をラップして、古い import パスを通るようにしています。
  • 現在ではメンテナンスが終了しており、RxJS 7 以降とは互換性がありません。
// rxjs-compat: 古い書き方(現在は非推奨)
import 'rxjs-compat'; // これだけで RxJS 5 のグローバル設定が有効になる
import { Observable } from 'rxjs/Observable'; // 古いパス

// 上記は現在の rxjs では動作しません

🛠️ Import 構文とモジュールシステム

この二つの最大の違いは、コード内でどのように機能を読み込むかです。

rxjs では、ES6 モジュールの標準的な構文を使用します。

  • 必要な機能だけをピンポイントで import します。
  • これにより、ビルドツールが未使用のコードを削除しやすくなります。
// rxjs: 明示的な import
import { BehaviorSubject } from 'rxjs';
import { filter } from 'rxjs/operators';

const subject = new BehaviorSubject(0);

rxjs-compat を使っていた頃は、RxJS 5 のような「すべてを一度に読み込む」スタイルが可能でした。

  • パッチを当てることで、rxjs/Rx からの import を可能にしていました。
  • これはバンドルサイズが増大する主要原因となっていました。
// rxjs-compat: 古いスタイル(パッチ適用後)
import { Observable } from 'rxjs/Rx'; 
// 上記は rxjs-compat を入れていないとエラーになります

🚀 パフォーマンスとバンドルサイズ

アーキテクチャの観点から、rxjs-compat を使い続けることは技術的負債を抱えることを意味します。

rxjs は、モダンなビルドプロセスと相性が良いです。

  • 余計な互換性コードが含まれていないため、実行速度が速いです。
  • Webpack や Vite などのバンドラと組み合わせた際、最適化が効きます。
// rxjs: バンドル最適化の恩恵を受けられる
// 使っていない operator は最終ビルドから削除される
import { take } from 'rxjs/operators';

rxjs-compat は、互換性を保つために追加のコードをロードします。

  • 本家の rxjs に加えて、互換性レイヤー分のサイズが増えます。
  • 移行期間を過ぎても使い続けると、アプリケーションが不必要に重くなります。
// rxjs-compat: 余分なレイヤーが存在する
// 内部で古い API を新しい API に変換する処理が走るためオーバーヘッドがある

⚠️ 廃止とサポートの状況

これが最も重要な判断基準です。

rxjs は現在もアクティブに開発されています。

  • セキュリティ修正や新機能が定期的にリリースされます。
  • 型定義(TypeScript)も最新の状態に保たれています。
// rxjs: 最新の型定義が利用可能
const source: Observable<number> = of(1);

rxjs-compat は事実上廃止(Deprecated)されています。

  • RxJS 7 のリリースに伴い、サポート対象から外れました。
  • 新しいプロジェクトでこれを選ぶ理由はありません。
  • 既存プロジェクトでも、削除する方向でスケジュールを組むべきです。
// rxjs-compat: 型定義が古く、最新の TS と相性が悪い場合がある
// エラーが発生しても、互換性レイヤー経由のため原因特定が難しい

🔄 移行パスの実際

もしあなたが今 rxjs-compat を使っているなら、以下の手順で rxjs へ移行すべきです。

  1. rxjs-compat をアンインストールする。
  2. import 'rxjs/Rx' などの古い記述を、import { ... } from 'rxjs' に書き換える。
  3. rxjs-marbles などのテストツールも最新版に合わせる。
// 移行前 (rxjs-compat 使用)
import 'rxjs-compat';
Observable.of(1).subscribe();

// 移行後 (rxjs 使用)
import { of } from 'rxjs';
of(1).subscribe();

📊 比較サマリー

特徴rxjsrxjs-compat
ステータス✅ アクティブ(推奨)❌ 廃止(非推奨)
目的非同期処理の標準実装RxJS 5→6 移行のためのつなぎ
Import 構文import { ... } from 'rxjs'import 'rxjs/Rx' (パッチ)
バンドルサイズ最適化可能互換性コードで増大
RxJS 7 対応✅ 対応❌ 非対応
使用場面すべての新規・既存プロジェクト移行中のレガシーコードのみ

💡 結論:迷わず rxjs を選ぶ

アーキテクトとしての明確な推奨は、rxjs 一択です。

rxjs-compat は、2018 年頃の RxJS 6 リリース時に発生した「破壊的変更」を吸収するための緊急措置でした。それから数年が経過し、エコシステムは完全に新しい構文に移行しています。今さら互換性レイヤーに依存することは、パフォーマンスの低下と、将来的なバージョンアップの困難さを招くだけです。

もしレガシーなコードベースを維持している場合でも、rxjs-compat に頼り続けるのではなく、少しずつでも標準的な rxjs の import 構文に書き換える「リファクタリングタスク」をバックログに追加すべきです。それが、長期的なプロジェクトの健全性を守ることになります。

選び方: rxjs vs rxjs-compat

  • rxjs:

    新しいプロジェクトや既存プロジェクトの更新では、必ず rxjs を選択してください。これは公式の最新バージョンであり、すべての機能、パフォーマンス改善、セキュリティ修正が含まれています。モダンな import 構文(import { from } from 'rxjs')を使用することで、バンドルサイズも最適化されます。

  • rxjs-compat:

    このパッケージは、RxJS 5 のコード(import 'rxjs/Rx' など)を RxJS 6 に移行する間の「つなぎ」として設計されました。現在は RxJS 7 以降でサポートが終了しているため、新規プロジェクトでは絶対に使用しないでください。既存のレガシーシステムで、すぐに書き換えられない場合のみ一時的に検討しますが、早急な移行を推奨します。

rxjs のREADME

RxJS Logo RxJS: Reactive Extensions For JavaScript

CI npm version Join the chat at https://gitter.im/Reactive-Extensions/RxJS

The Roadmap from RxJS 7 to 8

Curious what's next for RxJS? Follow along with Issue 6367.

RxJS 7

FOR 6.X PLEASE GO TO THE 6.x BRANCH

Reactive Extensions Library for JavaScript. This is a rewrite of Reactive-Extensions/RxJS and is the latest production-ready version of RxJS. This rewrite is meant to have better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface.

Apache 2.0 License

Versions In This Repository

  • master - This is all of the current work, which is against v7 of RxJS right now
  • 6.x - This is the branch for version 6.X

Most PRs should be made to master.

Important

By contributing or commenting on issues in this repository, whether you've read them or not, you're agreeing to the Contributor Code of Conduct. Much like traffic laws, ignorance doesn't grant you immunity.

Installation and Usage

ES6 via npm

npm install rxjs

It's recommended to pull in the Observable creation methods you need directly from 'rxjs' as shown below with range. If you're using RxJS version 7.2 or above, you can pull in any operator you need from the same spot, 'rxjs'.

import { range, filter, map } from 'rxjs';

range(1, 200)
  .pipe(
    filter(x => x % 2 === 1),
    map(x => x + x)
  )
  .subscribe(x => console.log(x));

If you're using RxJS version below 7.2, you can pull in any operator you need from one spot, under 'rxjs/operators'.

import { range } from 'rxjs';
import { filter, map } from 'rxjs/operators';

range(1, 200)
  .pipe(
    filter(x => x % 2 === 1),
    map(x => x + x)
  )
  .subscribe(x => console.log(x));

CDN

For CDN, you can use unpkg:

https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js

The global namespace for rxjs is rxjs:

const { range } = rxjs;
const { filter, map } = rxjs.operators;

range(1, 200)
  .pipe(
    filter(x => x % 2 === 1),
    map(x => x + x)
  )
  .subscribe(x => console.log(x));

Goals

  • Smaller overall bundles sizes
  • Provide better performance than preceding versions of RxJS
  • To model/follow the Observable Spec Proposal to the observable
  • Provide more modular file structure in a variety of formats
  • Provide more debuggable call stacks than preceding versions of RxJS

Building/Testing

  • npm run compile build everything
  • npm test run tests
  • npm run dtslint run dtslint tests

Adding documentation

We appreciate all contributions to the documentation of any type. All of the information needed to get the docs app up and running locally as well as how to contribute can be found in the documentation directory.