Repository navigation
Description
Which package?
@formatjs/[email protected]
Describe the bug
When defaultMessage references a local const string, the unplugin doesn't transform it. The equivalent inline string literal works.
[email protected] resolves the same constant successfully, so this loses supported behavior when migrating from Babel.
To Reproduce
Codesandbox URL
Reproducible Steps/Repo
-
Create a temporary project and install the affected version:
mkdir formatjs-constant-repro cd formatjs-constant-repro npm init -y npm install --save-dev @formatjs/[email protected]
-
Save this as
repro.mjs:import { transform } from '@formatjs/unplugin/transform'; const source = ` import { FormattedMessage } from 'react-intl'; const greeting = 'Hello'; export const Greeting = () => ( <FormattedMessage defaultMessage={greeting} /> ); `; const result = transform(source, '/tmp/example.jsx', { ast: true, flatten: false, idInterpolationPattern: '[sha256:contenthash:hex:6]', preserveWhitespace: true, }); console.log(result?.code ?? source);
-
Run
node repro.mjs. The message remains unchanged, with no generatedidor precompiled AST. -
Replace
defaultMessage={greeting}inside the source string withdefaultMessage="Hello"and rerun. The literal receives a generated ID and precompiled AST.
Expected behavior
Resolve locally declared, statically known string constants, respecting lexical scope and shadowing, and transform the message like the equivalent literal.
If constant-backed values are intentionally unsupported, please document that difference and provide a diagnostic when a descriptor cannot receive a required generated ID.
Screenshots
Not applicable.
Desktop (please complete the following information):
- OS: Linux
- Browser: Not applicable; build-time transformation
- Version: Node.js 24.14.0;
@formatjs/[email protected]
Additional context
Confirmed Babel comparison: @babel/[email protected] and [email protected], using ast: true, preserveWhitespace: true and the same overrideIdFn, resolve a constant declared inside a component and generate the expected ID and message AST. The unplugin's published getStaticValue supports literals, literal templates and concatenations, but does not resolve identifier bindings. The issue was also observed with the official Rspack integration in a production build.
This concerns lexical static-value resolution, not runtime evaluation of dynamic messages or cross-module imports. A regression test should cover inner declarations/parameters shadowing outer constants, so resolution cannot select the wrong message text.
Activity