|
| 1 | +# Dual Rspack 1.x/2.x Support — Technical Design |
| 2 | + |
| 3 | +A single `@callstack/repack` release supports both `@rspack/core` majors. |
| 4 | +The source **compiles against Rspack 2 types** and **runs against both |
| 5 | +majors**, branching at runtime on the installed major. Behavior for |
| 6 | +Rspack 1 and webpack users is unchanged. |
| 7 | + |
| 8 | +## Version detection |
| 9 | + |
| 10 | +`src/helpers/rspackVersion.ts` resolves `@rspack/core/package.json` instead |
| 11 | +of importing the package: |
| 12 | + |
| 13 | +- `@rspack/core@2` is pure ESM — a CJS `require('@rspack/core')` throws |
| 14 | + `ERR_REQUIRE_ESM` on Node < 20.19, so detection must not load the package; |
| 15 | +- `@rspack/core` is an optional peer dependency and may be absent entirely |
| 16 | + (webpack-only projects). |
| 17 | + |
| 18 | +`getRspackVersion` / `getRspackMajorVersion` / `isRspack2` take an optional |
| 19 | +project-context directory for resolution. `getRspackMajorVersionFromCompiler` |
| 20 | +serves plugin contexts via `compiler.webpack.rspackVersion` (returns `null` |
| 21 | +for webpack compilers). |
| 22 | + |
| 23 | +`@rspack/core` is otherwise loaded with a plain `import` — the published |
| 24 | +package carries no workspace- or monorepo-aware resolution logic. |
| 25 | + |
| 26 | +## Node compatibility |
| 27 | + |
| 28 | +Rspack 2 requires Node `^20.19.0 || >=22.12.0`; Re.Pack's `engines` stays |
| 29 | +`>=18` because Rspack 1 and webpack setups are unaffected. Instead of a hard |
| 30 | +floor, `commands/rspack/ensureNodeCompat.ts` raises a clear error when |
| 31 | +Rspack 2 is detected on an unsupported Node. The rspack commands are |
| 32 | +lazy-loaded inside the command handlers so the guard runs before anything |
| 33 | +touches `@rspack/core`. |
| 34 | + |
| 35 | +Re.Pack's compiled CJS keeps working against the ESM-only core on supported |
| 36 | +Node versions via `require(esm)`: Rspack 2 uses the `module.exports` |
| 37 | +ESM-interop convention, so CJS consumers receive the callable `rspack` |
| 38 | +function with all named exports attached (`core.rspack === core`) — a shape |
| 39 | +identical to v1. |
| 40 | + |
| 41 | +## Config generation |
| 42 | + |
| 43 | +- `experiments.parallelLoader` is emitted only under Rspack 1. Rspack 2 |
| 44 | + removed the global flag (parallel loading is stable and opt-in per rule |
| 45 | + via `use[].parallel`), so the loader's parallel-mode warning probe is |
| 46 | + also skipped under v2 — running non-parallel there is a valid choice, |
| 47 | + not a misconfiguration. |
| 48 | +- `module.parser.javascript.exportsPresence` is set to `'auto'` under |
| 49 | + Rspack 2. The upstream default changed from `'warn'` to `'error'`, which |
| 50 | + breaks builds on technically-invalid imports inside `node_modules` — a |
| 51 | + common occurrence in the React Native ecosystem that Metro tolerates. |
| 52 | + Users can override this in their project config. |
| 53 | + |
| 54 | +## Persistent cache |
| 55 | + |
| 56 | +Rspack 2 moved the persistent cache configuration from `experiments.cache` |
| 57 | +to top-level `cache` (same shape) and **silently ignores** the legacy key. |
| 58 | + |
| 59 | +- `getRspackCacheConfig` reads both locations, so `--reset-cache` works |
| 60 | + against configs written for either major. |
| 61 | +- Under Rspack 2 with `experiments.cache` set, `warnLegacyRspackCacheConfig` |
| 62 | + emits a one-time warning pointing at the top-level option. The config is |
| 63 | + left untouched — migration is the user's action. |
| 64 | + |
| 65 | +## React Refresh |
| 66 | + |
| 67 | +`DevelopmentPlugin` branches on the compiler's major: |
| 68 | + |
| 69 | +- **Rspack ≥ 2**: applies the official `@rspack/plugin-react-refresh@^2` |
| 70 | + (an optional peer dependency — installed by v2 users alongside |
| 71 | + `@rspack/core@2`) with `injectEntry: false`, `forceEnable: true`, and |
| 72 | + `reactRefreshLoader: '@callstack/repack/react-refresh-loader'`. The |
| 73 | + package is ESM-only with a named export, so it is loaded lazily inside |
| 74 | + the version branch. |
| 75 | +- **Rspack 1 and webpack**: manual wiring against client runtime files |
| 76 | + vendored at `packages/repack/vendor/react-refresh/` (adapted from |
| 77 | + upstream plugin v2.0.2, MIT, with a LICENSE/provenance file). The |
| 78 | + `vendor/` directory ships as-is via `package.json#files` and is excluded |
| 79 | + from linting and the babel build. There is no dependency on |
| 80 | + `@rspack/plugin-react-refresh@1`. |
| 81 | + |
| 82 | +## Tracing / profiling |
| 83 | + |
| 84 | +Published Rspack 2 binaries do not include the perfetto trace layer, so |
| 85 | +`--trace-*` profiling defaults to the `'logger'` layer under v2 |
| 86 | +(`commands/rspack/profile/profile-2.ts`); Rspack 1 behavior is unchanged. |
| 87 | + |
| 88 | +## Module Federation v1 |
| 89 | + |
| 90 | +Under Rspack 2, `@module-federation/runtime-tools` is no longer installed |
| 91 | +automatically by `@rspack/core`. `ModuleFederationPluginV1.apply` verifies |
| 92 | +it is resolvable and raises an actionable error otherwise. |
| 93 | + |
| 94 | +## Types |
| 95 | + |
| 96 | +The `@rspack/core` devDependency is `^2`, so type-only imports check against |
| 97 | +v2 while the runtime object may be either major. Notable v2 type |
| 98 | +accommodations: |
| 99 | + |
| 100 | +- `SwcLoaderOptions` became a union discriminated on `detectSyntax`, which |
| 101 | + cannot be spread-and-reassembled; internal helpers operate on a local |
| 102 | + non-union `SwcConfig` alias (Re.Pack never sets `detectSyntax`). |
| 103 | +- `builtin:swc-loader`-only options (`rspackExperiments`, `transformImport`, |
| 104 | + `collectTypeScriptInfo`, `detectSyntax`) are split off before calling the |
| 105 | + raw SWC `transformSync` API. |
| 106 | +- One documented cast in `commands/rspack/start.ts`: Re.Pack's `devServer` |
| 107 | + type augmentation and Rspack 2's bundled `DevServer` type are structurally |
| 108 | + incompatible solely because each pulls `proxy` types from a different copy |
| 109 | + of http-proxy-middleware. `bundle.ts` avoids the cast by destructuring |
| 110 | + `devServer` off (unused when bundling). |
| 111 | + |
| 112 | +## Testing |
| 113 | + |
| 114 | +- **Unit tests run under both majors.** Jest's sandboxed CJS runtime cannot |
| 115 | + load the ESM-only v2 core, so a custom test environment |
| 116 | + (`jest.environment.js`) loads it outside the sandbox and a |
| 117 | + `moduleNameMapper` bridge exposes it to suites. The environment is |
| 118 | + parameterized on `RSPACK_MAJOR`: v2 via `await import('@rspack/core')`, |
| 119 | + v1 via `require` of the aliased `@rspack/core-v1` devDependency. |
| 120 | + `pnpm test:rspack1` runs the v1 lane; a lane-guard test asserts the loaded |
| 121 | + major matches the requested one. |
| 122 | +- **Built-dist smoke suite** (`tests/rspack-compat`) asserts the behavior |
| 123 | + matrix above against the packed dist under both majors — the failure modes |
| 124 | + specific to this design (require(esm) interop, loader resolution, refresh |
| 125 | + runtime source selection) only reproduce against the built output in a |
| 126 | + real project layout. |
| 127 | +- **Tester apps**: `apps/tester-app` runs Rspack 2 (full feature surface). |
| 128 | + `apps/tester-app-rspack1` is a standalone app *outside* the pnpm |
| 129 | + workspace with Re.Pack installed from a packed tarball — required because |
| 130 | + in-workspace apps always resolve Re.Pack's own `@rspack/core` devDependency |
| 131 | + (v2) regardless of their manifest pin, and the published package |
| 132 | + deliberately carries no workspace-aware resolution. |
| 133 | +- **CI**: the unit suite runs under both majors on Linux and Windows (the |
| 134 | + require(esm) loading path and version-helper filesystem resolution are |
| 135 | + Windows-sensitive); Node 18/20 lanes run the Rspack 1 lane only, per the |
| 136 | + v2 Node floor. |
0 commit comments