@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 +1 -1
- package/transform.d.ts +1 -0
- package/transform.js +10 -1
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
|
+
"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
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) {
|