@intlayer/swc 8.12.5-canary.0 → 9.0.0-canary.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 ADDED
@@ -0,0 +1,173 @@
1
+ # intlayer-swc-plugin
2
+
3
+ An [SWC](https://swc.rs) transform plugin for [Intlayer](https://intlayer.org) —
4
+ the open-source i18n framework for React, Next.js, Vue, Svelte, and more.
5
+
6
+ The plugin rewrites `useIntlayer` / `getIntlayer` / `useTranslations` call
7
+ arguments at compile time, replacing string dictionary keys with pre-loaded
8
+ dictionary imports. This eliminates runtime registry lookups and enables
9
+ tree-shaking for per-locale bundles.
10
+
11
+ ## How it works
12
+
13
+ **Before** (source code):
14
+
15
+ ```ts
16
+ import { useIntlayer } from "react-intlayer";
17
+ const t = useIntlayer("locale-switcher");
18
+ ```
19
+
20
+ **After** (transformed output):
21
+
22
+ ```ts
23
+ import _FsHhNfuhm85 from "../../.intlayer/dictionaries/locale-switcher.json" with { type: "json" };
24
+ import { useDictionary as useIntlayer } from "react-intlayer";
25
+ const t = useIntlayer(_FsHhNfuhm85);
26
+ ```
27
+
28
+ Three import modes are supported:
29
+
30
+ | Mode | Helper function | Import type |
31
+ | --------- | ---------------------- | --------------------- |
32
+ | `static` | `useDictionary` | JSON import assertion |
33
+ | `dynamic` | `useDictionaryDynamic` | Dynamic `.mjs` import |
34
+ | `fetch` | `useDictionaryDynamic` | Fetch `.mjs` import |
35
+
36
+ ---
37
+
38
+ ## Usage: Next.js / SWC Wasm plugin (recommended)
39
+
40
+ The Wasm binary is distributed via npm as
41
+ [`@intlayer/swc`](https://www.npmjs.com/package/@intlayer/swc).
42
+ You do not need to add this Rust crate as a dependency for that use-case.
43
+
44
+ ```bash
45
+ npm install @intlayer/swc
46
+ # or
47
+ bun add @intlayer/swc
48
+ ```
49
+
50
+ Configure in `next.config.ts`:
51
+
52
+ ```ts
53
+ import type { NextConfig } from "next";
54
+
55
+ const nextConfig: NextConfig = {
56
+ experimental: {
57
+ swcPlugins: [
58
+ [
59
+ "@intlayer/swc",
60
+ {
61
+ dictionariesDir: "/absolute/path/.intlayer/dictionaries",
62
+ dictionariesEntryPath: "/absolute/path/.intlayer/dictionaries.mjs",
63
+ dynamicDictionariesDir:
64
+ "/absolute/path/.intlayer/dynamic_dictionaries",
65
+ fetchDictionariesDir: "/absolute/path/.intlayer/fetch_dictionaries",
66
+ importMode: "static", // "static" | "dynamic" | "fetch"
67
+ replaceDictionaryEntry: false,
68
+ filesList: [], // empty = transform all files
69
+ dictionaryModeMap: {}, // per-key overrides, e.g. { "heavy-dict": "dynamic" }
70
+ },
71
+ ],
72
+ ],
73
+ },
74
+ };
75
+
76
+ export default nextConfig;
77
+ ```
78
+
79
+ In practice you should use the
80
+ [`@intlayer/webpack`](https://www.npmjs.com/package/@intlayer/webpack) or
81
+ [`@intlayer/vite`](https://www.npmjs.com/package/@intlayer/vite) plugin, which
82
+ configures the SWC plugin automatically based on your `intlayer.config.*` file.
83
+
84
+ ---
85
+
86
+ ## Usage: native Rust library
87
+
88
+ Add to `Cargo.toml`:
89
+
90
+ ```toml
91
+ [dependencies]
92
+ intlayer-swc-plugin = "7"
93
+ ```
94
+
95
+ Then call [`process_transform`] directly from your own SWC pipeline:
96
+
97
+ ```rust
98
+ use intlayer_swc_plugin::{PluginConfig, process_transform};
99
+ use swc_core::ecma::ast::Program;
100
+
101
+ fn my_transform(program: Program, file_path: &str) -> Program {
102
+ let config = PluginConfig {
103
+ dictionaries_dir: "/project/.intlayer/dictionaries".into(),
104
+ dictionaries_entry_path: "/project/.intlayer/dictionaries.mjs".into(),
105
+ dynamic_dictionaries_dir: "/project/.intlayer/dynamic_dictionaries".into(),
106
+ fetch_dictionaries_dir: "/project/.intlayer/fetch_dictionaries".into(),
107
+ import_mode: Some("static".into()),
108
+ replace_dictionary_entry: Some(false),
109
+ files_list: vec![],
110
+ dictionary_mode_map: None,
111
+ };
112
+ process_transform(program, config, file_path.into())
113
+ }
114
+ ```
115
+
116
+ ### Building the Wasm plugin yourself
117
+
118
+ The `plugin` Cargo feature enables the `#[plugin_transform]` Wasm entry point.
119
+ Without it the crate compiles as a standard native Rust library.
120
+
121
+ ```bash
122
+ # Uses the alias defined in .cargo/config.toml
123
+ cargo build-wasip1 --release
124
+ # equivalent to:
125
+ cargo build --target wasm32-wasip1 --features plugin --release
126
+ ```
127
+
128
+ ---
129
+
130
+ ## Plugin configuration reference
131
+
132
+ All fields correspond to the JSON object passed as the second element of each
133
+ `swcPlugins` tuple.
134
+
135
+ | Field | Type | Default | Description |
136
+ | ------------------------ | ---------------------------------- | ---------- | --------------------------------------------------- |
137
+ | `dictionariesDir` | `string` | required | Absolute path to compiled `.json` dictionaries |
138
+ | `dictionariesEntryPath` | `string` | required | Absolute path to the generated entry `.mjs` file |
139
+ | `dynamicDictionariesDir` | `string` | required | Absolute path for dynamic `.mjs` modules |
140
+ | `fetchDictionariesDir` | `string` | required | Absolute path for fetch `.mjs` modules |
141
+ | `importMode` | `"static" \| "dynamic" \| "fetch"` | `"static"` | Global import strategy |
142
+ | `replaceDictionaryEntry` | `boolean` | `false` | Replace entry file with empty stubs |
143
+ | `filesList` | `string[]` | `[]` | Allowlist of absolute file paths; empty = all files |
144
+ | `dictionaryModeMap` | `Record<string, string>` | `{}` | Per-dictionary import mode overrides |
145
+
146
+ ---
147
+
148
+ ## Public Rust API
149
+
150
+ The following symbols are exported by this crate:
151
+
152
+ - **`PluginConfig`** – configuration struct (mirrors the JSON options above).
153
+ - **`process_transform(program, cfg, filename) -> Program`** – core transform
154
+ function; accepts and returns an SWC `Program` AST.
155
+ - **`normalize_path(path: &str) -> String`** – normalises Windows-style
156
+ backslash paths to forward slashes for cross-platform path diffing.
157
+
158
+ ---
159
+
160
+ ## Related packages
161
+
162
+ | Package | Description |
163
+ | ---------------------------------------------------------------------- | -------------------------------------- |
164
+ | [`@intlayer/webpack`](https://www.npmjs.com/package/@intlayer/webpack) | Webpack plugin (Babel-based transform) |
165
+ | [`@intlayer/vite`](https://www.npmjs.com/package/@intlayer/vite) | Vite plugin |
166
+ | [`react-intlayer`](https://www.npmjs.com/package/react-intlayer) | React hooks |
167
+ | [`next-intlayer`](https://www.npmjs.com/package/next-intlayer) | Next.js integration |
168
+
169
+ ---
170
+
171
+ ## License
172
+
173
+ Apache-2.0 © [Aymeric Pineau](https://github.com/aymericzip)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/swc",
3
- "version": "8.12.5-canary.0",
3
+ "version": "9.0.0-canary.0",
4
4
  "private": false,
5
5
  "description": "A SWC plugin for Intlayer that transforms declaration files and provides internationalization features during the build process according to the Intlayer configuration.",
6
6
  "keywords": [