Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rspack-2-cache-warning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Show a one-time warning when a legacy `experiments.cache` value is detected under Rspack 2. Rspack 2 moved persistent cache configuration to the top-level `cache` option and silently ignores the legacy key, so persistent caching would be lost without any signal. The config is left untouched - migrate the value to the top-level `cache` option in your Rspack config.
9 changes: 9 additions & 0 deletions .changeset/rspack-2-config-routing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@callstack/repack": patch
---

Route the default Rspack configuration by the installed `@rspack/core` major version:

- `experiments.parallelLoader` is only set under Rspack 1 - Rspack 2 removed the flag (parallel loading is stable and opt-in per rule via `use[].parallel`), so it is no longer injected there
- `module.parser.javascript.exportsPresence` defaults to `'auto'` under Rspack 2 to keep Metro-like tolerance for invalid imports inside node_modules (Rspack 2 changed the default to `'error'`, which breaks builds on imports Metro tolerates; overridable in your project config)
- `RSPACK_PROFILE` profiling defaults to the `logger` trace layer under Rspack 2 (published Rspack 2 binaries do not include the perfetto layer; `RSPACK_TRACE_LAYER=perfetto` is still accepted for custom builds that enable it)
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
describe('warnLegacyRspackCacheConfig', () => {
let warnSpy: jest.SpyInstance;

beforeEach(() => {
// the helper keeps module-level once-only state - re-evaluate it per test
jest.resetModules();
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
warnSpy.mockRestore();
});

const loadHelper = () => {
// require instead of import so jest.resetModules() re-evaluates the module
const module: typeof import(
'../warnLegacyRspackCacheConfig.js'
) = require('../warnLegacyRspackCacheConfig.js');
return module.warnLegacyRspackCacheConfig;
};

it('does not warn when no config uses the legacy experiments.cache key', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{},
{ cache: true },
{ cache: { type: 'persistent' } },
{ experiments: {} },
]);

expect(warnSpy).not.toHaveBeenCalled();
});

it('warns when a config has experiments.cache and no top-level cache', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{ experiments: { cache: { type: 'persistent' } } },
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatch(/experiments\.cache/);
expect(warnSpy.mock.calls[0][0]).toMatch(/top-level 'cache'/);
});

it('warns for the typical Rspack 1 shape (cache: true + experiments.cache)', () => {
const warnLegacyRspackCacheConfig = loadHelper();

// under Rspack 2 `cache: true` enables the memory cache only,
// so persistent caching is still not in effect
warnLegacyRspackCacheConfig([
{ cache: true, experiments: { cache: { type: 'persistent' } } },
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
});

it('warns when top-level cache is memory-only next to the legacy key', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{
cache: { type: 'memory' },
experiments: { cache: { type: 'persistent' } },
},
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
});

it('does not warn for a migrated config that only left the inert legacy key behind', () => {
const warnLegacyRspackCacheConfig = loadHelper();

// persistent caching IS enabled and the legacy key carries no options
// beyond `type` - there is nothing Rspack 2 could drop
warnLegacyRspackCacheConfig([
{
cache: {
type: 'persistent',
storage: { type: 'filesystem', directory: '/custom' },
},
experiments: { cache: { type: 'persistent' } },
},
]);

expect(warnSpy).not.toHaveBeenCalled();
});

it('does not warn when the legacy key is deep-equal to the top-level cache', () => {
const warnLegacyRspackCacheConfig = loadHelper();

// every option under the legacy key is mirrored at the top level,
// so ignoring the legacy key changes nothing
warnLegacyRspackCacheConfig([
{
cache: {
type: 'persistent',
storage: { type: 'filesystem', directory: '/custom' },
buildDependencies: ['/project/rspack.config.mjs'],
},
experiments: {
cache: {
type: 'persistent',
storage: { type: 'filesystem', directory: '/custom' },
buildDependencies: ['/project/rspack.config.mjs'],
},
},
},
]);

expect(warnSpy).not.toHaveBeenCalled();
});

it('warns softly for a partial migration that leaves options under the legacy key', () => {
const warnLegacyRspackCacheConfig = loadHelper();

// persistent caching IS enabled, but the storage directory only lives
// under the legacy key - Rspack 2 drops it and uses the default location
warnLegacyRspackCacheConfig([
{
cache: { type: 'persistent' },
experiments: {
cache: {
type: 'persistent',
storage: { type: 'filesystem', directory: '/custom' },
},
},
},
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatch(/experiments\.cache/);
expect(warnSpy.mock.calls[0][0]).toMatch(/not applied/);
// caching IS enabled - the soft warning must not claim otherwise
expect(warnSpy.mock.calls[0][0]).not.toMatch(/NOT enabled/);
});

it('warns strongly when legacy options come without a top-level persistent cache', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{
experiments: {
cache: {
type: 'persistent',
storage: { type: 'filesystem', directory: '/custom' },
},
},
},
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatch(/NOT enabled/);
});

it('prefers the strong warning when configs trigger both tiers', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{
cache: { type: 'persistent' },
experiments: {
cache: {
type: 'persistent',
storage: { type: 'filesystem', directory: '/custom' },
},
},
},
{ experiments: { cache: { type: 'persistent' } } },
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatch(/NOT enabled/);
});

it('warns when any config of a multi-config array is misconfigured', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{
cache: { type: 'persistent' },
experiments: { cache: { type: 'persistent' } },
},
{ experiments: { cache: { type: 'persistent' } } },
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
});

it('warns only once across repeated calls', () => {
const warnLegacyRspackCacheConfig = loadHelper();

warnLegacyRspackCacheConfig([
{ experiments: { cache: { type: 'persistent' } } },
]);
warnLegacyRspackCacheConfig([
{ experiments: { cache: { type: 'persistent' } } },
]);

expect(warnSpy).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { getRspackMajorVersion } from '../../../../helpers/index.js';
import { getRepackConfig } from '../getRepackConfig.js';

jest.mock('../getMinimizerConfig.js');
jest.mock('../../../../helpers/index.js', () => ({
...jest.requireActual('../../../../helpers/index.js'),
getRspackMajorVersion: jest.fn(),
}));

const getRspackMajorVersionMock = jest.mocked(getRspackMajorVersion);

describe('getRepackConfig', () => {
describe('rspack 1', () => {
beforeEach(() => {
getRspackMajorVersionMock.mockReturnValue(1);
});

it('enables experiments.parallelLoader', async () => {
const config = await getRepackConfig('rspack', '/project/root');
expect(config.experiments).toEqual({ parallelLoader: true });
});

it('does not override module parser defaults', async () => {
const config = await getRepackConfig('rspack', '/project/root');
expect(config.module).toBeUndefined();
});
});

describe('rspack 2', () => {
beforeEach(() => {
getRspackMajorVersionMock.mockReturnValue(2);
});

it('omits experiments.parallelLoader (removed in Rspack 2)', async () => {
const config = await getRepackConfig('rspack', '/project/root');
expect(config.experiments).toBeUndefined();
});

it('relaxes exportsPresence to auto to keep Metro-like tolerance', async () => {
const config = await getRepackConfig('rspack', '/project/root');
expect(config.module).toEqual({
parser: { javascript: { exportsPresence: 'auto' } },
});
});
});

it('falls back to rspack 1 behavior when the rspack version is unresolvable', async () => {
getRspackMajorVersionMock.mockReturnValue(null);
const config = await getRepackConfig('rspack', '/project/root');
expect(config.experiments).toEqual({ parallelLoader: true });
expect(config.module).toBeUndefined();
});

describe('webpack', () => {
it('sets no rspack-specific experiments or module overrides', async () => {
const config = await getRepackConfig('webpack', '/project/root');
expect(config.experiments).toBeUndefined();
expect(config.module).toBeUndefined();
});

it('never probes the installed rspack version', async () => {
await getRepackConfig('webpack', '/project/root');
expect(getRspackMajorVersionMock).not.toHaveBeenCalled();
});
});
});
30 changes: 27 additions & 3 deletions packages/repack/src/commands/common/config/getRepackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
import { getRspackMajorVersion } from '../../../helpers/index.js';
import { getMinimizerConfig } from './getMinimizerConfig.js';

function getExperimentsConfig(bundler: 'rspack' | 'webpack') {
function getExperimentsConfig(bundler: 'rspack' | 'webpack', rootDir: string) {
if (bundler === 'rspack') {
return { parallelLoader: true };
const rspackMajor = getRspackMajorVersion(rootDir) ?? 1;
if (rspackMajor < 2) {
return { parallelLoader: true };
}
// Rspack 2 removed `experiments.parallelLoader` (parallel loading is
// stable and opt-in per rule via `use[].parallel`) - nothing to set.
}
}

function getModuleConfig(bundler: 'rspack' | 'webpack', rootDir: string) {
if (bundler === 'rspack') {
const rspackMajor = getRspackMajorVersion(rootDir) ?? 1;
if (rspackMajor >= 2) {
// Rspack 2 changed the `exportsPresence` default from 'warn' to 'error',
// which breaks builds on invalid imports inside node_modules - a common
// occurrence in the React Native ecosystem that Metro tolerates.
// Restore Metro-like leniency by default; users can override this
// in their project config.
return {
parser: { javascript: { exportsPresence: 'auto' as const } },
};
}
}
}

export async function getRepackConfig(
bundler: 'rspack' | 'webpack',
rootDir: string
) {
const experiments = getExperimentsConfig(bundler);
const experiments = getExperimentsConfig(bundler, rootDir);
const moduleConfiguration = getModuleConfig(bundler, rootDir);
const minimizerConfiguration = await getMinimizerConfig(bundler, rootDir);

return {
devtool: 'source-map',
experiments,
module: moduleConfiguration,
output: {
clean: true,
hashFunction: 'xxhash64',
Expand Down
1 change: 1 addition & 0 deletions packages/repack/src/commands/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export * from './runAdbReverse.js';
export * from './setupEnvironment.js';
export * from './setupInteractions.js';
export * from './setupStatsWriter.js';
export * from './warnLegacyRspackCacheConfig.js';

export * from './config/makeCompilerConfig.js';
Loading
Loading