@lingui/swc-plugin 6.0.0 → 6.2.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.
package/README.md CHANGED
@@ -31,8 +31,73 @@ yarn add @lingui/macro
31
31
 
32
32
  ## Usage
33
33
 
34
- `.swcrc`
35
- https://swc.rs/docs/configuration/swcrc
34
+ If your build tool uses a JS config (Next.js, Vite, etc.), use the `linguiMacroSwcPlugin` helper — it reads your Lingui config and prepares all plugin options automatically.
35
+
36
+ If you configure SWC directly via `.swcrc` (e.g. the SWC CLI), pass options manually as described in the [Options](#options) section below.
37
+
38
+ ### JS config (recommended)
39
+
40
+ #### `next.config.js`
41
+
42
+ ```js
43
+ const { linguiMacroSwcPlugin } = require("@lingui/swc-plugin/options")
44
+
45
+ /** @type {import('next').NextConfig} */
46
+ const nextConfig = {
47
+ reactStrictMode: true,
48
+ experimental: {
49
+ swcPlugins: [
50
+ linguiMacroSwcPlugin(),
51
+ ],
52
+ },
53
+ };
54
+
55
+ module.exports = nextConfig;
56
+ ```
57
+
58
+ > **Note**
59
+ > Consult with full working example for NextJS in the `/examples` folder in this repo.
60
+
61
+ #### `vite.config.ts`
62
+
63
+ ```ts
64
+ import { defineConfig } from "vite"
65
+ import react from "@vitejs/plugin-react-swc"
66
+ import { lingui } from "@lingui/vite-plugin"
67
+ import { linguiMacroSwcPlugin } from "@lingui/swc-plugin/options"
68
+
69
+ export default defineConfig({
70
+ plugins: [
71
+ react({
72
+ plugins: [linguiMacroSwcPlugin()],
73
+ }),
74
+ lingui(),
75
+ ],
76
+ })
77
+ ```
78
+
79
+ #### `linguiMacroSwcPlugin(overrides?, configOptions?)`
80
+
81
+ `linguiMacroSwcPlugin` reads your Lingui config and maps relevant options to the SWC plugin format. It returns a `["@lingui/swc-plugin", options]` tuple ready to use in plugin arrays.
82
+
83
+ ```js
84
+ import { linguiMacroSwcPlugin } from "@lingui/swc-plugin/options"
85
+
86
+ // Recommended — reads lingui.config.{js,ts} automatically
87
+ linguiMacroSwcPlugin()
88
+
89
+ // Override specific options
90
+ linguiMacroSwcPlugin({
91
+ useLinguiV5IdGeneration: true,
92
+ })
93
+
94
+ // Specify which lingui config to use
95
+ linguiMacroSwcPlugin({}, { configPath: '../lingui.config.js' })
96
+ ```
97
+
98
+ ### `.swcrc`
99
+
100
+ When using SWC directly via CLI or a JSON-only configuration, pass options manually. All options are optional — if your have a standard setup, an empty object `{}` is sufficient:
36
101
 
37
102
  ```json5
38
103
  {
@@ -43,31 +108,16 @@ https://swc.rs/docs/configuration/swcrc
43
108
  [
44
109
  "@lingui/swc-plugin",
45
110
  {
46
- // Optional
47
- // Unlike the JS version this option must be passed as object only.
48
- // Docs https://lingui.dev/ref/conf#runtimeconfigmodule
49
- // "runtimeModules": {
50
- // "i18n": ["@lingui/core", "i18n"],
51
- // "trans": ["@lingui/react", "Trans"]
52
- // }
53
- //
54
- // Optional. Controls which descriptor fields are preserved in output.
55
- // "descriptorFields": "auto" (default) | "all" | "id-only" | "message"
56
- //
57
- // Compatibility option allows to use v6.* SWC Plugin release channel with @lingui/cli@5.*
58
- // Controls the BASE64 alphabet used for generating message IDs.
59
- // - false (default): Uses URL-safe BASE64 alphabet (Lingui v6 behavior)
60
- // - true: Uses standard BASE64 alphabet (Lingui v5 behavior for compatibility)
61
- //
62
- // IMPORTANT: This option is temporal and will be removed in the next major release.
63
- // "useLinguiV5IdGeneration": true
64
- //
65
- // To configure custom JSX placeholder attribute and its defaults:
66
- // "jsxPlaceholderAttribute": "_t",
67
- // "jsxPlaceholderDefaults": {
68
- // "a": "link",
69
- // "em": "em"
70
- // }
111
+ "runtimeModules": {
112
+ "i18n": ["@lingui/core", "i18n"],
113
+ "Trans": ["@lingui/react", "Trans"],
114
+ "useLingui": ["@lingui/react", "useLingui"]
115
+ },
116
+ "descriptorFields": "auto",
117
+ "jsxPlaceholderAttribute": "_t",
118
+ "jsxPlaceholderDefaults": {
119
+ "a": "link"
120
+ }
71
121
  },
72
122
  ],
73
123
  ],
@@ -76,6 +126,8 @@ https://swc.rs/docs/configuration/swcrc
76
126
  }
77
127
  ```
78
128
 
129
+ ## Options
130
+
79
131
  ### `descriptorFields`
80
132
 
81
133
  Controls which fields are preserved in the transformed message descriptors. Accepts one of:
@@ -85,30 +137,35 @@ Controls which fields are preserved in the transformed message descriptors. Acce
85
137
  - **`"id-only"`** — Keeps only the `id`. Most optimized for production bundles.
86
138
  - **`"message"`** — Keeps `id`, `message`, and `context` (but not `comment`). Useful when you need message content at runtime.
87
139
 
88
- Check [this article](https://lingui.dev/guides/optimizing-bundle-size) for more info about this configuration.
140
+ See [Optimizing bundle size](https://lingui.dev/guides/optimizing-bundle-size) for more info about this configuration.
89
141
 
90
- Or Next JS Usage:
142
+ ### `idPrefixLeader`
91
143
 
92
- `next.config.js`
93
- ```js
94
- /** @type {import('next').NextConfig} */
95
- const nextConfig = {
96
- reactStrictMode: true,
97
- experimental: {
98
- swcPlugins: [
99
- ['@lingui/swc-plugin', {
100
- // the same options as in .swcrc
101
- }],
102
- ],
103
- },
104
- };
144
+ The SWC plugin matches the Babel macro behavior
105
145
 
106
- module.exports = nextConfig;
107
- ```
146
+ See [Configuration Doc](https://lingui.dev/ref/conf#macroidprefixleader) and [`lingui-set` / `lingui-reset` Comment Directives Doc](https://lingui.dev/ref/macro#lingui-directive)
108
147
 
109
- > **Note**
110
- > Consult with full working example for NextJS in the `/examples` folder in this repo.
148
+ ### `jsxPlaceholderAttribute`
149
+
150
+ Sets the JSX attribute name used to provide explicit placeholder names inside `<Trans>` content.
151
+
152
+ ### `jsxPlaceholderDefaults`
153
+
154
+ Defines default placeholder names for JSX tags when no explicit placeholder attribute is present.
111
155
 
156
+ ### `runtimeModules`
157
+
158
+ Overrides the runtime imports used by the plugin. Unlike [the Babel macro configuration](https://lingui.dev/ref/conf#runtimeconfigmodule), this option must be passed as an object.
159
+
160
+ ### `useLinguiV5IdGeneration`
161
+
162
+ Compatibility option for using the v6 SWC plugin release channel with `@lingui/cli@5.*`.
163
+
164
+ - **`false`** (default) — Uses the URL-safe Base64 alphabet used by Lingui v6.
165
+ - **`true`** — Uses the standard Base64 alphabet used by Lingui v5.
166
+
167
+ > **Note**
168
+ > This option is temporary and will be removed in the next major release.
112
169
 
113
170
  ## Compatibility
114
171
  SWC Plugin support is still experimental. They do not guarantee a semver backwards compatibility between different `swc-core` versions.
@@ -119,29 +176,30 @@ Below is a table referencing the `swc_core` version used during the plugin build
119
176
 
120
177
  To learn more about SWC Plugins compatibility check this issue https://github.com/lingui/swc-plugin/issues/179
121
178
 
122
- | Plugin Version | used `swc_core` |
123
- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
124
- | `0.1.0`, `4.0.0-next.0` | `0.52.8` |
125
- | `0.2.*`, `4.0.0-next.1` ~ `4.0.0-next.3` | `0.56.1` |
126
- | `4.0.0` | `0.75.33` |
127
- | `4.0.1` | `0.76.0` |
128
- | `4.0.2` | `0.76.41` |
129
- | `4.0.3` | `0.78.28` |
130
- | `4.0.4` | `0.79.x` |
131
- | `4.0.5`, `4.0.6` | [`0.87.x`](https://plugins.swc.rs/versions/range/10) |
132
- | `4.0.7`, `4.0.8`, `5.0.0-next.0` ~ `5.0.0-next.1` | [`0.90.35`](https://plugins.swc.rs/versions/range/12) |
133
- | `4.0.9` | [`0.96.9`](https://plugins.swc.rs/versions/range/15) |
134
- | `4.0.10` | [`0.101.4`](https://plugins.swc.rs/versions/range/94) |
135
- | `4.1.0`, `5.0.0` ~ `5.2.0` | [`0.106.3`](https://plugins.swc.rs/versions/range/95) |
136
- | `5.3.0` | [`5.0.4`](https://plugins.swc.rs/versions/range/116) |
137
- | `5.4.0` | [`14.1.0`](https://plugins.swc.rs/versions/range/138) |
138
- | `5.5.0` ~ `5.5.2` | [`15.0.1`](https://plugins.swc.rs/versions/range/271) |
139
- | `5.6.0` ~ `5.6.1` | [`27.0.6`](https://plugins.swc.rs/versions/range/364) |
140
- | `5.7.0` | [`39.0.3`](https://plugins.swc.rs/versions/range/426) |
141
- | `5.8.0` | [`45.0.2`](https://plugins.swc.rs/versions/range/497) |
142
- | `5.9.0` | [`46.0.3`](https://plugins.swc.rs/versions/range/713) |
143
- | `5.10.0` | [`50.2.3`](https://plugins.swc.rs/versions/range/768) |
144
- | `5.10.1` ~ `*` <br/> Starting from this version Wasm plugins are compatible between `@swc/core` versions to some extent. Read more [here](https://swc.rs/docs/plugin/ecmascript/compatibility#make-your-plugin-compatible). | [`50.2.3`](https://plugins.swc.rs/versions/range/768) with [`--cfg=swc_ast_unknown`](https://swc.rs/docs/plugin/ecmascript/compatibility#make-your-plugin-compatible) |
179
+ | Plugin Version | used `swc_core` |
180
+ |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
181
+ | `0.1.0`, `4.0.0-next.0` | `0.52.8` |
182
+ | `0.2.*`, `4.0.0-next.1` ~ `4.0.0-next.3` | `0.56.1` |
183
+ | `4.0.0` | `0.75.33` |
184
+ | `4.0.1` | `0.76.0` |
185
+ | `4.0.2` | `0.76.41` |
186
+ | `4.0.3` | `0.78.28` |
187
+ | `4.0.4` | `0.79.x` |
188
+ | `4.0.5`, `4.0.6` | [`0.87.x`](https://plugins.swc.rs/versions/range/10) |
189
+ | `4.0.7`, `4.0.8`, `5.0.0-next.0` ~ `5.0.0-next.1` | [`0.90.35`](https://plugins.swc.rs/versions/range/12) |
190
+ | `4.0.9` | [`0.96.9`](https://plugins.swc.rs/versions/range/15) |
191
+ | `4.0.10` | [`0.101.4`](https://plugins.swc.rs/versions/range/94) |
192
+ | `4.1.0`, `5.0.0` ~ `5.2.0` | [`0.106.3`](https://plugins.swc.rs/versions/range/95) |
193
+ | `5.3.0` | [`5.0.4`](https://plugins.swc.rs/versions/range/116) |
194
+ | `5.4.0` | [`14.1.0`](https://plugins.swc.rs/versions/range/138) |
195
+ | `5.5.0` ~ `5.5.2` | [`15.0.1`](https://plugins.swc.rs/versions/range/271) |
196
+ | `5.6.0` ~ `5.6.1` | [`27.0.6`](https://plugins.swc.rs/versions/range/364) |
197
+ | `5.7.0` | [`39.0.3`](https://plugins.swc.rs/versions/range/426) |
198
+ | `5.8.0` | [`45.0.2`](https://plugins.swc.rs/versions/range/497) |
199
+ | `5.9.0` | [`46.0.3`](https://plugins.swc.rs/versions/range/713) |
200
+ | `5.10.0` | [`50.2.3`](https://plugins.swc.rs/versions/range/768) |
201
+ | `5.10.1` ~ `6.1.0` <br/> Starting from this version Wasm plugins are compatible between `@swc/core` versions to some extent. Read more [here](https://swc.rs/docs/plugin/ecmascript/compatibility#make-your-plugin-compatible). | [`50.2.3`](https://plugins.swc.rs/versions/range/768) with [`--cfg=swc_ast_unknown`](https://swc.rs/docs/plugin/ecmascript/compatibility#make-your-plugin-compatible) |
202
+ | `6.2.0` ~ `*` | `66.0.3` |
145
203
 
146
204
 
147
205
  > **Note**
@@ -0,0 +1,63 @@
1
+ export type RuntimeModuleConfig = readonly [modulePath: string, exportName?: string];
2
+ /** Options accepted by the `@lingui/swc-plugin` WASM plugin. */
3
+ export type LinguiMacroOptions = {
4
+ /** JSX attribute name used to provide explicit placeholder names inside `<Trans>` content. */
5
+ jsxPlaceholderAttribute?: string;
6
+ /** Default placeholder names for JSX tags when no explicit placeholder attribute is present. */
7
+ jsxPlaceholderDefaults?: Record<string, string>;
8
+ /** Overrides the runtime imports used by the plugin. Unlike the Babel macro configuration, must be passed as an object. */
9
+ runtimeModules: {
10
+ i18n: RuntimeModuleConfig;
11
+ Trans: RuntimeModuleConfig;
12
+ useLingui: RuntimeModuleConfig;
13
+ };
14
+ /**
15
+ * Compatibility option for using the v6 SWC plugin with `@lingui/cli@5.*`.
16
+ * - `false` (default) — URL-safe Base64 alphabet (Lingui v6).
17
+ * - `true` — Standard Base64 alphabet (Lingui v5).
18
+ *
19
+ * Temporary — will be removed in the next major release.
20
+ */
21
+ useLinguiV5IdGeneration?: boolean;
22
+ /**
23
+ * Controls which descriptor fields are preserved in output.
24
+ * - `"auto"` (default) — `"id-only"` in production, `"all"` otherwise.
25
+ * - `"all"` — Keeps id, message, context, and comment.
26
+ * - `"id-only"` — Keeps only id. Most optimized for production bundles.
27
+ * - `"message"` — Keeps id, message, and context (not comment).
28
+ */
29
+ descriptorFields?: 'auto' | 'all' | 'id-only' | 'message';
30
+ /** Restricts directive-based `idPrefix` to explicit ids starting with this leader string. When omitted, `idPrefix` is prepended to all explicit static ids. */
31
+ idPrefixLeader?: string;
32
+ };
33
+ /** Makes all properties in `T` optional, recursing into nested objects but preserving tuples/arrays as-is. */
34
+ export type DeepPartial<T> = {
35
+ [Key in keyof T]?: T[Key] extends readonly unknown[] ? T[Key] : T[Key] extends object ? DeepPartial<T[Key]> : T[Key];
36
+ };
37
+ /** Controls how the Lingui config is located and loaded. */
38
+ export type GetConfigOptions = {
39
+ /** Working directory for config discovery. Defaults to `process.cwd()`. */
40
+ cwd?: string;
41
+ /** Explicit path to a Lingui config file, bypassing discovery. */
42
+ configPath?: string;
43
+ /** Skip schema validation of the loaded config. */
44
+ skipValidation?: boolean;
45
+ };
46
+ /**
47
+ * Loads the Lingui config, maps relevant options to the SWC plugin format,
48
+ * and returns a ready-to-use `["@lingui/swc-plugin", options]` tuple.
49
+ *
50
+ * @example
51
+ * ```js
52
+ * // next.config.js
53
+ * const nextConfig = {
54
+ * experimental: {
55
+ * swcPlugins: [linguiMacroSwcPlugin()],
56
+ * },
57
+ * };
58
+ * ```
59
+ *
60
+ * @param overrides - Plugin options merged over values derived from the Lingui config.
61
+ * @param configOptions - Controls how the Lingui config is discovered or loaded.
62
+ */
63
+ export declare function linguiMacroSwcPlugin(overrides?: DeepPartial<LinguiMacroOptions>, configOptions?: GetConfigOptions): (string | LinguiMacroOptions)[];
@@ -0,0 +1,31 @@
1
+ import { getConfig } from "@lingui/conf";
2
+ /**
3
+ * Loads the Lingui config, maps relevant options to the SWC plugin format,
4
+ * and returns a ready-to-use `["@lingui/swc-plugin", options]` tuple.
5
+ *
6
+ * @example
7
+ * ```js
8
+ * // next.config.js
9
+ * const nextConfig = {
10
+ * experimental: {
11
+ * swcPlugins: [linguiMacroSwcPlugin()],
12
+ * },
13
+ * };
14
+ * ```
15
+ *
16
+ * @param overrides - Plugin options merged over values derived from the Lingui config.
17
+ * @param configOptions - Controls how the Lingui config is discovered or loaded.
18
+ */
19
+ export function linguiMacroSwcPlugin(overrides, configOptions = {}) {
20
+ const config = getConfig(configOptions);
21
+ const macroOptions = {
22
+ jsxPlaceholderAttribute: config.macro.jsxPlaceholderAttribute,
23
+ jsxPlaceholderDefaults: config.macro.jsxPlaceholderDefaults,
24
+ ...overrides,
25
+ runtimeModules: {
26
+ ...config.runtimeConfigModule,
27
+ ...overrides?.runtimeModules,
28
+ },
29
+ };
30
+ return ["@lingui/swc-plugin", macroOptions];
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/swc-plugin",
3
- "version": "6.0.0",
3
+ "version": "6.2.0",
4
4
  "description": "A SWC Plugin for LinguiJS",
5
5
  "author": {
6
6
  "name": "Timofei Iatsenko",
@@ -23,13 +23,27 @@
23
23
  ],
24
24
  "main": "target/wasm32-wasip1/release/lingui_macro_plugin.wasm",
25
25
  "exports": {
26
- ".": "./target/wasm32-wasip1/release/lingui_macro_plugin.wasm"
26
+ ".": "./target/wasm32-wasip1/release/lingui_macro_plugin.wasm",
27
+ "./options": {
28
+ "types": "./dist/options.d.ts",
29
+ "default": "./dist/options.js"
30
+ }
27
31
  },
28
32
  "scripts": {
29
- "prepublishOnly": "cargo build-wasi --release",
33
+ "prepublishOnly": "yarn build:ts && yarn build:wasm",
34
+ "build:ts": "tsc -p tsconfig.build.json",
35
+ "build:wasm": "cargo build-wasi --release",
30
36
  "test:e2e": "cargo build-wasi --release && vitest run"
31
37
  },
32
- "files": [],
38
+ "files": [
39
+ "LICENSE",
40
+ "README.md",
41
+ "dist/",
42
+ "target/wasm32-wasip1/release/lingui_macro_plugin.wasm"
43
+ ],
44
+ "dependencies": {
45
+ "@lingui/conf": "5 || 6"
46
+ },
33
47
  "peerDependencies": {
34
48
  "@lingui/core": "5 || 6"
35
49
  },
@@ -42,10 +56,10 @@
42
56
  }
43
57
  },
44
58
  "devDependencies": {
45
- "@swc/core": "^1.15.11",
59
+ "@swc/core": "^1.15.33",
46
60
  "@types/node": "22.13.14",
47
- "typescript": "^5.9.3",
48
- "vitest": "^4.0.18"
61
+ "typescript": "^6.0.3",
62
+ "vitest": "^4.1.7"
49
63
  },
50
64
  "packageManager": "yarn@4.12.0+sha512.f45ab632439a67f8bc759bf32ead036a1f413287b9042726b7cc4818b7b49e14e9423ba49b18f9e06ea4941c1ad062385b1d8760a8d5091a1a31e5f6219afca8"
51
65
  }