Skip to content
Prev Previous commit
Next Next commit
fix(fmt): inject SWC Next version into cache namespace
  • Loading branch information
SoonIter committed Sep 22, 2026
commit 8b237034b42bb5b07e622b9795bc3292760c7c3e
8 changes: 8 additions & 0 deletions packages/rstack/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { readFileSync } from 'node:fs';
import { findPackageJSON } from 'node:module';
import { defineConfig } from '@rslib/core';
import prettierPkgJson from 'prettier/package.json' with { type: 'json' };
import pkgJson from './package.json' with { type: 'json' };

// @swc-next/parser does not export its package.json.
const swcNextPkgJson = JSON.parse(
readFileSync(findPackageJSON('@swc-next/parser', import.meta.url)!, 'utf8'),
) as { version: string };

const fullyMinifiedChunks =
/(?:fmt(?:Lsp|Plugins)?|sortPackageJsonPlugin|staged)\.js$/;

Expand All @@ -27,6 +34,7 @@ export default defineConfig({
define: {
PRETTIER_VERSION: JSON.stringify(prettierPkgJson.version),
RSTACK_VERSION: JSON.stringify(pkgJson.version),
SWC_NEXT_VERSION: JSON.stringify(swcNextPkgJson.version),
},
},
output: {
Expand Down
3 changes: 2 additions & 1 deletion packages/rstack/src/fmt/cacheIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ResolvedFmtOptions } from './types.ts';

declare const PRETTIER_VERSION: string;
declare const RSTACK_VERSION: string;
declare const SWC_NEXT_VERSION: string;

type CacheKeyResolver = (filePath: string) => string | undefined;
type OptionsHasher = (options: ResolvedFmtOptions) => string | undefined;
Expand All @@ -21,7 +22,7 @@ const cacheNamespace: string = JSON.stringify([
fmtCacheVersion,
RSTACK_VERSION,
PRETTIER_VERSION,
'swc-next@0.2.2:npm',
`swc-next@${SWC_NEXT_VERSION}:npm`,
]);

/** Creates project-relative POSIX cache keys without repeating path setup. */
Expand Down
8 changes: 7 additions & 1 deletion packages/rstack/tests/fmt/cacheIdentity.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFileSync } from 'node:fs';
import { findPackageJSON } from 'node:module';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import prettierPkgJson from 'prettier/package.json' with { type: 'json' };
Expand Down Expand Up @@ -84,11 +86,15 @@ test('bypasses user plugins and unserializable options', () => {
});

test('includes formatter implementation versions in the namespace', () => {
const swcNextPkgJson = JSON.parse(
readFileSync(findPackageJSON('@swc-next/parser', import.meta.url)!, 'utf8'),
) as { version: string };

expect(JSON.parse(cacheNamespace)).toEqual([
fmtCacheVersion,
pkgJson.version,
prettierPkgJson.version,
'swc-next@0.2.2:npm',
`swc-next@${swcNextPkgJson.version}:npm`,
]);
});

Expand Down
Loading