Skip to content

Commit 08db5e1

Browse files
authored
docs(formatjs): Add usage to README (#511)
fixes #489
1 parent 16c5ac4 commit 08db5e1

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

.changeset/proud-onions-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@swc/plugin-formatjs": patch
3+
---
4+
5+
docs(formatjs): add Usage to readme

packages/formatjs/README.tmpl.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,95 @@
22

33
FormatJS SWC plugin, maintained by SWC team.
44

5+
## Usage
6+
7+
The default message descriptors for the app's default language will be processed from: `defineMessages()`, `defineMessage()`, `intl.formatMessage` and `<FormattedMessage>`; all of which are named exports of the React Intl package.
8+
9+
### Next.js
10+
11+
```tsx
12+
import { NextConfig } from 'next';
13+
14+
const nextConfig: NextConfig = {
15+
experimental: {
16+
swcPlugins: [
17+
[
18+
require.resolve('@swc/plugin-formatjs'),
19+
{
20+
idInterpolationPattern: '[md5:contenthash:hex:10]',
21+
additionalComponentNames: ['F'],
22+
ast: true,
23+
},
24+
],
25+
],
26+
}
27+
}
28+
```
29+
30+
### Vite
31+
```tsx
32+
import react from '@vitejs/plugin-react-swc'
33+
import { defineConfig } from 'vite'
34+
35+
export default defineConfig((env) => ({
36+
plugins: [
37+
react({
38+
tsDecorators: true,
39+
plugins: [
40+
[
41+
'@swc/plugin-formatjs',
42+
{
43+
idInterpolationPattern: '[md5:contenthash:hex:10]',
44+
additionalComponentNames: ['F'],
45+
ast: true,
46+
},
47+
],
48+
],
49+
}),
50+
]
51+
}))
52+
```
53+
54+
## Options
55+
56+
### **`idInterpolationPattern`**
57+
58+
A template string that allows you to override the ID both in the extracted javascript and messages. It's used only if the message ID is empty.
59+
60+
### **`removeDefaultMessage`**
61+
62+
Remove `defaultMessage` field in generated js after extraction.
63+
64+
### **`extractSourceLocation`**
65+
66+
Whether the metadata about the location of the message in the source file should be extracted. If `true`, then `file`, `start`, and `end` fields will exist for each extracted message descriptors. Defaults to `false`.
67+
68+
### **`additionalComponentNames`**
69+
70+
Additional component names to extract messages from, e.g: `['FormattedFooBarMessage']`. **NOTE**: By default we check for the fact that `FormattedMessage` are imported from `moduleSourceName` to make sure variable alias works. This option does not do that so it's less safe.
71+
72+
### **`additionalFunctionNames`**
73+
74+
Additional function names to extract messages from, e.g: `['$formatMessage']`. Use this if you prefer to alias `formatMessage` to something shorter like `$t`.
75+
76+
### **`ast`**
77+
78+
Pre-parse `defaultMessage` into AST for faster runtime perf. This flag doesn't do anything when `removeDefaultMessage` is `true`.
79+
80+
### **`preserveWhitespace`**
81+
82+
Whether to preserve whitespace and newlines.
83+
84+
### **`pragma`**
85+
86+
A string that allows you to extract metadata from comments in your source files. When set, the plugin will look for comments containing the pragma string and parse key:value pairs that follow it.
87+
88+
For example, with `pragma: "@formatjs"`, a comment like:
89+
90+
```js
91+
// @formatjs project:web locale:en region:us
92+
```
93+
94+
Will extract the metadata: `{project: "web", locale: "en", region: "us"}` that gets included with the extracted messages. This is useful for adding contextual information to your message extractions.
95+
596
${CHANGELOG}

0 commit comments

Comments
 (0)