@better-intl/compiler 0.1.0 → 0.1.2
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/README.md +115 -0
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @better-intl/compiler
|
|
2
|
+
|
|
3
|
+
AST extractor and Babel/SWC transform for **better-intl**.
|
|
4
|
+
|
|
5
|
+
Extracts translatable text from JSX at build time and transforms it into optimized `t()` calls — or inlines translations for zero-runtime cost.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @better-intl/compiler
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Extract messages from source
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { extract } from "@better-intl/compiler";
|
|
19
|
+
|
|
20
|
+
const messages = extract(
|
|
21
|
+
`export function Hero() {
|
|
22
|
+
return <h1>Hello world</h1>;
|
|
23
|
+
}`,
|
|
24
|
+
{ filePath: "src/Hero.tsx" }
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// [{ id: "0rr2b7c", defaultMessage: "Hello world", filePath: "src/Hero.tsx", ... }]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Transform JSX
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { transform } from "@better-intl/compiler";
|
|
34
|
+
|
|
35
|
+
// Runtime mode — replaces text with t() calls
|
|
36
|
+
const result = transform(source, {
|
|
37
|
+
filePath: "src/Hero.tsx",
|
|
38
|
+
mode: "auto",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Zero-runtime mode — inlines translated text
|
|
42
|
+
const result = transform(source, {
|
|
43
|
+
filePath: "src/Hero.tsx",
|
|
44
|
+
locale: "pt-BR",
|
|
45
|
+
messages: { "0rr2b7c": "Olá mundo" },
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Catalog utilities
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import {
|
|
53
|
+
generateDefaultCatalog,
|
|
54
|
+
mergeCatalog,
|
|
55
|
+
findMissingKeys,
|
|
56
|
+
findDeadKeys,
|
|
57
|
+
} from "@better-intl/compiler";
|
|
58
|
+
|
|
59
|
+
// Generate default locale catalog from extracted messages
|
|
60
|
+
const catalog = generateDefaultCatalog(messages);
|
|
61
|
+
|
|
62
|
+
// Merge new messages into existing catalog (preserves translations)
|
|
63
|
+
const { messages, added, removed } = mergeCatalog(existing, extracted);
|
|
64
|
+
|
|
65
|
+
// Find missing translations
|
|
66
|
+
const missing = findMissingKeys(enCatalog, ptBrCatalog);
|
|
67
|
+
|
|
68
|
+
// Find unused keys
|
|
69
|
+
const dead = findDeadKeys(catalog, extracted);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Babel plugin
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
// babel.config.js
|
|
76
|
+
module.exports = {
|
|
77
|
+
plugins: [
|
|
78
|
+
["@better-intl/compiler/babel", {
|
|
79
|
+
locale: "pt-BR",
|
|
80
|
+
messages: require("./locales/pt-BR.json"),
|
|
81
|
+
}],
|
|
82
|
+
],
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Webpack loader
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
// webpack.config.js
|
|
90
|
+
module.exports = {
|
|
91
|
+
module: {
|
|
92
|
+
rules: [
|
|
93
|
+
{
|
|
94
|
+
test: /\.[jt]sx$/,
|
|
95
|
+
use: ["@better-intl/compiler/webpack-loader"],
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## API
|
|
103
|
+
|
|
104
|
+
| Export | Description |
|
|
105
|
+
|--------|-------------|
|
|
106
|
+
| `extract(source, options)` | Extract translatable messages from JSX |
|
|
107
|
+
| `transform(source, options)` | Transform JSX text into `t()` calls or inline translations |
|
|
108
|
+
| `generateDefaultCatalog(messages)` | Generate catalog for default locale |
|
|
109
|
+
| `mergeCatalog(existing, extracted)` | Merge new messages preserving existing translations |
|
|
110
|
+
| `findMissingKeys(default, target)` | Find keys missing in target locale |
|
|
111
|
+
| `findDeadKeys(catalog, extracted)` | Find unused keys in catalog |
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-intl/compiler",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "AST extractor and Babel/SWC transform for better-intl",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -24,10 +24,11 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"dist"
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md"
|
|
28
29
|
],
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@better-intl/core": "0.1.
|
|
31
|
+
"@better-intl/core": "0.1.2"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@babel/core": "^7.26.0",
|