@swc/plugin-formatjs 3.0.0 → 3.1.0
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.
|
Binary file
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @swc/plugin-formatjs
|
|
2
2
|
|
|
3
|
+
## 3.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- af2e35d: Add support for sha1 hash_type in idInterpolationPattern
|
|
8
|
+
|
|
9
|
+
## 3.0.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 31e3254: build: Update `swc_core` to `v19.0.0`
|
|
14
|
+
|
|
3
15
|
## 3.0.0
|
|
4
16
|
|
|
5
17
|
### Major Changes
|
package/README.md
CHANGED
|
@@ -4,6 +4,18 @@ FormatJS SWC plugin, maintained by SWC team.
|
|
|
4
4
|
|
|
5
5
|
# @swc/plugin-formatjs
|
|
6
6
|
|
|
7
|
+
## 3.1.0
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- af2e35d: Add support for sha1 hash_type in idInterpolationPattern
|
|
12
|
+
|
|
13
|
+
## 3.0.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 31e3254: build: Update `swc_core` to `v19.0.0`
|
|
18
|
+
|
|
7
19
|
## 3.0.0
|
|
8
20
|
|
|
9
21
|
### Major Changes
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { transform } from "@swc/core";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import url from "node:url";
|
|
5
|
+
|
|
6
|
+
const transformCode = async (code: string, options = {}) => {
|
|
7
|
+
const result = await transform(code, {
|
|
8
|
+
jsc: {
|
|
9
|
+
parser: {
|
|
10
|
+
syntax: "typescript",
|
|
11
|
+
tsx: true,
|
|
12
|
+
},
|
|
13
|
+
experimental: {
|
|
14
|
+
plugins: [
|
|
15
|
+
[
|
|
16
|
+
path.join(
|
|
17
|
+
path.dirname(url.fileURLToPath(import.meta.url)),
|
|
18
|
+
"..",
|
|
19
|
+
"swc_plugin_formatjs.wasm",
|
|
20
|
+
),
|
|
21
|
+
options,
|
|
22
|
+
],
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
return result.code;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe("formatjs swc plugin", () => {
|
|
31
|
+
it("should transform FormattedMessage component", async () => {
|
|
32
|
+
const input = `
|
|
33
|
+
import React from 'react';
|
|
34
|
+
import { FormattedMessage } from 'react-intl';
|
|
35
|
+
|
|
36
|
+
export function Greeting() {
|
|
37
|
+
return (
|
|
38
|
+
<FormattedMessage
|
|
39
|
+
defaultMessage="Hello, {name}!"
|
|
40
|
+
description="Greeting message"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
const output = await transformCode(input);
|
|
47
|
+
|
|
48
|
+
expect(output).toMatch(/id: \".+\"/);
|
|
49
|
+
expect(output).toMatch(/defaultMessage: "Hello, \{name\}!"/);
|
|
50
|
+
expect(output).not.toMatch(/description/);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should transform defineMessage function", async () => {
|
|
54
|
+
const input = `
|
|
55
|
+
import { defineMessage } from 'react-intl';
|
|
56
|
+
|
|
57
|
+
const message = defineMessage({
|
|
58
|
+
defaultMessage: "Welcome to {site}",
|
|
59
|
+
description: "Welcome message"
|
|
60
|
+
});
|
|
61
|
+
`;
|
|
62
|
+
|
|
63
|
+
const output = await transformCode(input);
|
|
64
|
+
|
|
65
|
+
expect(output).toMatch(/id: "[^"]+"/);
|
|
66
|
+
expect(output).toMatch(/defaultMessage: "Welcome to \{site\}"/);
|
|
67
|
+
expect(output).not.toMatch(/description/);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should transform multiple messages in defineMessages", async () => {
|
|
71
|
+
const input = `
|
|
72
|
+
import { defineMessages } from 'react-intl';
|
|
73
|
+
|
|
74
|
+
const messages = defineMessages({
|
|
75
|
+
greeting: {
|
|
76
|
+
defaultMessage: "Hello",
|
|
77
|
+
description: "Greeting"
|
|
78
|
+
},
|
|
79
|
+
farewell: {
|
|
80
|
+
defaultMessage: "Goodbye",
|
|
81
|
+
description: "Farewell"
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
`;
|
|
85
|
+
|
|
86
|
+
const output = await transformCode(input);
|
|
87
|
+
|
|
88
|
+
const idMatches = output.match(/id:/g);
|
|
89
|
+
expect(idMatches).toHaveLength(2);
|
|
90
|
+
|
|
91
|
+
expect(output).toMatch(/defaultMessage: "Hello"/);
|
|
92
|
+
expect(output).toMatch(/defaultMessage: "Goodbye"/);
|
|
93
|
+
|
|
94
|
+
expect(output).not.toMatch(/description/);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should handle formatMessage calls", async () => {
|
|
98
|
+
const input = `
|
|
99
|
+
import { useIntl } from 'react-intl';
|
|
100
|
+
|
|
101
|
+
function MyComponent() {
|
|
102
|
+
const intl = useIntl();
|
|
103
|
+
return intl.formatMessage({
|
|
104
|
+
defaultMessage: "Click here",
|
|
105
|
+
description: "Button text"
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
const output = await transformCode(input);
|
|
111
|
+
|
|
112
|
+
expect(output).toMatch(/id: "[^"]+"/);
|
|
113
|
+
expect(output).toMatch(/defaultMessage: "Click here"/);
|
|
114
|
+
expect(output).not.toMatch(/description/);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should preserve whitespace when option is enabled", async () => {
|
|
118
|
+
const input = `
|
|
119
|
+
import { FormattedMessage } from 'react-intl';
|
|
120
|
+
|
|
121
|
+
export function Greeting() {
|
|
122
|
+
return (
|
|
123
|
+
<FormattedMessage
|
|
124
|
+
defaultMessage="Hello, {name}!"
|
|
125
|
+
description="Greeting message"
|
|
126
|
+
/>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
`;
|
|
130
|
+
|
|
131
|
+
const output = await transformCode(input, {
|
|
132
|
+
preserve_whitespace: true,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(output).toMatch(/defaultMessage: "Hello, {4}\{name\}!"/);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("should use custom id interpolation pattern", async () => {
|
|
139
|
+
const input = `
|
|
140
|
+
import { FormattedMessage } from 'react-intl';
|
|
141
|
+
|
|
142
|
+
export function Greeting() {
|
|
143
|
+
return (
|
|
144
|
+
<FormattedMessage
|
|
145
|
+
defaultMessage="Hello, {name}!"
|
|
146
|
+
description="Greeting message"
|
|
147
|
+
/>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
`;
|
|
151
|
+
|
|
152
|
+
const output = await transformCode(input, {
|
|
153
|
+
idInterpolationPattern: "[name]_[hash:base64:5]",
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(output).toMatch(/id: "file_[a-zA-Z0-9]{5}"/);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should handle additional component names", async () => {
|
|
160
|
+
const input = `
|
|
161
|
+
import { CustomMessage } from './custom-intl';
|
|
162
|
+
|
|
163
|
+
export function Greeting() {
|
|
164
|
+
return (
|
|
165
|
+
<CustomMessage
|
|
166
|
+
defaultMessage="Hello, {name}!"
|
|
167
|
+
description="Greeting message"
|
|
168
|
+
/>
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
`;
|
|
172
|
+
|
|
173
|
+
const output = await transformCode(input, {
|
|
174
|
+
additionalComponentNames: ["CustomMessage"],
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expect(output).toMatch(/id: "[^"]+"/);
|
|
178
|
+
expect(output).toMatch(/defaultMessage: "Hello, \{name\}!"/);
|
|
179
|
+
expect(output).not.toMatch(/description/);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should be able to use sha1 and sha512 hashing in interpolation", async () => {
|
|
183
|
+
const input = `
|
|
184
|
+
import { FormattedMessage } from 'react-intl';
|
|
185
|
+
|
|
186
|
+
export function Greeting() {
|
|
187
|
+
return (
|
|
188
|
+
<FormattedMessage
|
|
189
|
+
defaultMessage="Hello!"
|
|
190
|
+
description="Greeting message"
|
|
191
|
+
/>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
`;
|
|
195
|
+
|
|
196
|
+
const sha1output = await transformCode(input, {
|
|
197
|
+
idInterpolationPattern: "[sha1:contenthash:base64:6]",
|
|
198
|
+
});
|
|
199
|
+
const sha512output = await transformCode(input, {
|
|
200
|
+
idInterpolationPattern: "[sha512:contenthash:base64:6]",
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
expect(sha1output).toMatch(/id: "[a-zA-Z0-9]{6}"/);
|
|
204
|
+
expect(sha512output).toMatch(/id: "[a-zA-Z0-9]{6}"/);
|
|
205
|
+
expect(sha1output).not.toMatch(sha512output);
|
|
206
|
+
});
|
|
207
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swc/plugin-formatjs",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "FormatJS SWC plugin",
|
|
5
5
|
"main": "swc_plugin_formatjs.wasm",
|
|
6
6
|
"homepage": "https://swc.rs",
|
|
@@ -19,5 +19,9 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@swc/counter": "^0.1.3"
|
|
21
21
|
},
|
|
22
|
-
"scripts": {
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "cargo build --release -p swc_plugin_formatjs --target wasm32-wasip1 && cp ../../target/wasm32-wasip1/release/swc_plugin_formatjs.wasm .",
|
|
24
|
+
"build:debug": "cargo build -p swc_plugin_formatjs --target wasm32-wasip1 && cp ../../target/wasm32-wasip1/debug/swc_plugin_formatjs.wasm .",
|
|
25
|
+
"test": "pnpm run build:debug && vitest run"
|
|
26
|
+
}
|
|
23
27
|
}
|
package/swc_plugin_formatjs.wasm
CHANGED
|
Binary file
|