Skip to content

@formatjs/unplugin silently skips constant-backed defaultMessage values supported by babel-plugin-formatjs #7682

Closed

Description

@alexeyr-ci6

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

https://codesandbox.io/p/devbox/autumn-dew-4dtn6s?workspaceId=ws_EjT9CReErggZ1qw9wBjfGn

Reproducible Steps/Repo

  1. 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]
  2. 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);
  3. Run node repro.mjs. The message remains unchanged, with no generated id or precompiled AST.

  4. Replace defaultMessage={greeting} inside the source string with defaultMessage="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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions