@swc/plugin-formatjs 3.0.0 → 3.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @swc/plugin-formatjs
2
2
 
3
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 31e3254: build: Update `swc_core` to `v19.0.0`
8
+
3
9
  ## 3.0.0
4
10
 
5
11
  ### Major Changes
package/README.md CHANGED
@@ -4,6 +4,12 @@ FormatJS SWC plugin, maintained by SWC team.
4
4
 
5
5
  # @swc/plugin-formatjs
6
6
 
7
+ ## 3.0.1
8
+
9
+ ### Patch Changes
10
+
11
+ - 31e3254: build: Update `swc_core` to `v19.0.0`
12
+
7
13
  ## 3.0.0
8
14
 
9
15
  ### Major Changes
@@ -0,0 +1,181 @@
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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swc/plugin-formatjs",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
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-wasi && cp ../../target/wasm32-wasi/release/swc_plugin_formatjs.wasm .",
24
+ "build:debug": "cargo build -p swc_plugin_formatjs --target wasm32-wasi && cp ../../target/wasm32-wasi/debug/swc_plugin_formatjs.wasm .",
25
+ "test": "npm run build:debug && vitest run"
26
+ }
23
27
  }
Binary file