@formatjs/unplugin 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/unplugin",
3
3
  "description": "Universal build plugin for FormatJS — supports Vite, Webpack, Rollup, esbuild, and Rspack",
4
- "version": "1.1.4",
4
+ "version": "1.1.5",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
package/transform.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface Options {
7
7
  additionalFunctionNames?: string[];
8
8
  ast?: boolean;
9
9
  preserveWhitespace?: boolean;
10
+ flatten?: boolean;
10
11
  }
11
12
  export declare function transform(code: string, id: string, options: Options): {
12
13
  code: string;
package/transform.js CHANGED
@@ -2,9 +2,11 @@ import { parseSync, Visitor } from "oxc-parser";
2
2
  import MagicString from "magic-string";
3
3
  import { interpolateName } from "@formatjs/ts-transformer";
4
4
  import { parse } from "@formatjs/icu-messageformat-parser";
5
+ import { hoistSelectors } from "@formatjs/icu-messageformat-parser/manipulator.js";
6
+ import { printAST } from "@formatjs/icu-messageformat-parser/printer.js";
5
7
  const DEFAULT_ID_INTERPOLATION_PATTERN = "[sha512:contenthash:base64:6]";
6
8
  export function transform(code, id, options) {
7
- const { overrideIdFn, idInterpolationPattern = DEFAULT_ID_INTERPOLATION_PATTERN, removeDefaultMessage = false, additionalComponentNames = [], additionalFunctionNames = [], ast: preParseAst = false, preserveWhitespace = false } = options;
9
+ const { overrideIdFn, idInterpolationPattern = DEFAULT_ID_INTERPOLATION_PATTERN, removeDefaultMessage = false, additionalComponentNames = [], additionalFunctionNames = [], ast: preParseAst = false, preserveWhitespace = false, flatten = true } = options;
8
10
  const componentNames = new Set(["FormattedMessage", ...additionalComponentNames]);
9
11
  const functionNames = new Set([
10
12
  "formatMessage",
@@ -122,6 +124,13 @@ export function transform(code, id, options) {
122
124
  if (defaultMessage && !preserveWhitespace) {
123
125
  defaultMessage = defaultMessage.trim().replace(/\s+/gm, " ");
124
126
  }
127
+ // Apply flatten transformation (hoist selectors + normalize ICU format via printAST)
128
+ // This must happen before ID generation so the ID matches formatjs extract and babel-plugin-formatjs
129
+ if (flatten && defaultMessage) {
130
+ try {
131
+ defaultMessage = printAST(hoistSelectors(parse(defaultMessage)));
132
+ } catch (e) {}
133
+ }
125
134
  // Generate ID
126
135
  let newId = existingId;
127
136
  if (overrideIdFn) {