@generaltranslation/compiler 1.0.0-alpha.0 → 1.0.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 +20 -165
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ This plugin performs two main functions during the build process:
|
|
|
10
10
|
|
|
11
11
|
Detects and prevents invalid usage patterns in GT translation components:
|
|
12
12
|
|
|
13
|
-
#### JSX Component Violations
|
|
13
|
+
#### JSX Component Violations
|
|
14
14
|
|
|
15
15
|
```jsx
|
|
16
16
|
// ❌ Unwrapped expressions
|
|
@@ -27,31 +27,31 @@ Detects and prevents invalid usage patterns in GT translation components:
|
|
|
27
27
|
<T><Var>{greeting}</Var> world</T>
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
#### Function Call Violations
|
|
30
|
+
#### Function Call Violations
|
|
31
31
|
|
|
32
32
|
```js
|
|
33
33
|
// ❌ Template literals
|
|
34
|
-
const msg =
|
|
35
|
-
const error =
|
|
34
|
+
const msg = gt(`Hello ${name}!`);
|
|
35
|
+
const error = gt(`Error: ${code} - ${message}`);
|
|
36
36
|
|
|
37
37
|
// ❌ String concatenation
|
|
38
|
-
const welcome =
|
|
39
|
-
const path =
|
|
38
|
+
const welcome = gt('Welcome ' + username);
|
|
39
|
+
const path = gt('Go to ' + destination + ' page');
|
|
40
40
|
|
|
41
41
|
// ❌ Dynamic expressions
|
|
42
|
-
const dynamic =
|
|
42
|
+
const dynamic = gt(isError ? errorMsg : successMsg);
|
|
43
43
|
|
|
44
44
|
// ✅ Correct usage
|
|
45
|
-
const msg =
|
|
46
|
-
const welcome =
|
|
47
|
-
const error =
|
|
45
|
+
const msg = gt('Hello world!');
|
|
46
|
+
const welcome = gt('Welcome to our app');
|
|
47
|
+
const error = gt('Something went wrong', { context: 'error' });
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
### 2. Compile-Time Hash Generation
|
|
51
51
|
|
|
52
52
|
Pre-computes translation keys at build time for better performance:
|
|
53
53
|
|
|
54
|
-
- Generates stable hashes for `<T>` components and `
|
|
54
|
+
- Generates stable hashes for `<T>` components and `gt()` function calls
|
|
55
55
|
- Injects hash attributes (`_hash`) into components
|
|
56
56
|
- Creates content arrays for translation functions
|
|
57
57
|
|
|
@@ -71,15 +71,10 @@ If you're using `gt-next`, the plugin is automatically configured for you. No ad
|
|
|
71
71
|
|
|
72
72
|
```js
|
|
73
73
|
// webpack.config.js
|
|
74
|
-
const gtCompiler = require('@generaltranslation/compiler
|
|
74
|
+
const { webpack: gtCompiler } = require('@generaltranslation/compiler');
|
|
75
75
|
|
|
76
76
|
module.exports = {
|
|
77
|
-
plugins: [
|
|
78
|
-
gtCompiler({
|
|
79
|
-
compileTimeHash: true,
|
|
80
|
-
logLevel: 'warn',
|
|
81
|
-
}),
|
|
82
|
-
],
|
|
77
|
+
plugins: [gtCompiler()],
|
|
83
78
|
};
|
|
84
79
|
```
|
|
85
80
|
|
|
@@ -88,15 +83,10 @@ module.exports = {
|
|
|
88
83
|
```js
|
|
89
84
|
// vite.config.js
|
|
90
85
|
import { defineConfig } from 'vite';
|
|
91
|
-
import gtCompiler from '@generaltranslation/compiler
|
|
86
|
+
import { vite as gtCompiler } from '@generaltranslation/compiler';
|
|
92
87
|
|
|
93
88
|
export default defineConfig({
|
|
94
|
-
plugins: [
|
|
95
|
-
gtCompiler({
|
|
96
|
-
compileTimeHash: true,
|
|
97
|
-
logLevel: 'warn',
|
|
98
|
-
}),
|
|
99
|
-
],
|
|
89
|
+
plugins: [gtCompiler()],
|
|
100
90
|
});
|
|
101
91
|
```
|
|
102
92
|
|
|
@@ -104,15 +94,10 @@ export default defineConfig({
|
|
|
104
94
|
|
|
105
95
|
```js
|
|
106
96
|
// rollup.config.js
|
|
107
|
-
import gtCompiler from '@generaltranslation/compiler
|
|
97
|
+
import { rollup as gtCompiler } from '@generaltranslation/compiler';
|
|
108
98
|
|
|
109
99
|
export default {
|
|
110
|
-
plugins: [
|
|
111
|
-
gtCompiler({
|
|
112
|
-
compileTimeHash: true,
|
|
113
|
-
logLevel: 'warn',
|
|
114
|
-
}),
|
|
115
|
-
],
|
|
100
|
+
plugins: [gtCompiler()],
|
|
116
101
|
};
|
|
117
102
|
```
|
|
118
103
|
|
|
@@ -121,16 +106,11 @@ export default {
|
|
|
121
106
|
```js
|
|
122
107
|
// esbuild.config.js
|
|
123
108
|
const { build } = require('esbuild');
|
|
124
|
-
const gtCompiler = require('@generaltranslation/compiler
|
|
109
|
+
const { esbuild: gtCompiler } = require('@generaltranslation/compiler');
|
|
125
110
|
|
|
126
111
|
build({
|
|
127
|
-
plugins: [
|
|
128
|
-
|
|
129
|
-
compileTimeHash: true,
|
|
130
|
-
logLevel: 'warn',
|
|
131
|
-
}),
|
|
132
|
-
],
|
|
133
|
-
};
|
|
112
|
+
plugins: [gtCompiler()],
|
|
113
|
+
});
|
|
134
114
|
```
|
|
135
115
|
|
|
136
116
|
## Configuration Options
|
|
@@ -147,128 +127,3 @@ interface GTCompilerOptions {
|
|
|
147
127
|
disableBuildChecks?: boolean;
|
|
148
128
|
}
|
|
149
129
|
```
|
|
150
|
-
|
|
151
|
-
## How It Works
|
|
152
|
-
|
|
153
|
-
The plugin uses the [unplugin](https://unplugin.unjs.io/) framework to provide universal bundler support. It analyzes your code using Babel's parser and transformer to:
|
|
154
|
-
|
|
155
|
-
### String Collector
|
|
156
|
-
|
|
157
|
-
Manages translation content across the two-pass transformation:
|
|
158
|
-
|
|
159
|
-
- **Pass 1**: Collects translation strings, JSX content, and hash data
|
|
160
|
-
- **Pass 2**: Injects collected data back into `useGT()`/`getGT()` calls
|
|
161
|
-
- Associates content with function calls using deterministic counter IDs
|
|
162
|
-
- Supports multiple `t()` calls per translation function
|
|
163
|
-
|
|
164
|
-
### Scope Tracker
|
|
165
|
-
|
|
166
|
-
Handles variable scoping and import tracking:
|
|
167
|
-
|
|
168
|
-
- Tracks `useGT`/`getGT` variable assignments across nested scopes
|
|
169
|
-
- Manages variable shadowing (`const t = useGT()` in different scopes)
|
|
170
|
-
- Maps component names to their original GT imports (`T`, `Var`, etc.)
|
|
171
|
-
- Handles both named imports and namespace imports (`GT.T`)
|
|
172
|
-
|
|
173
|
-
### AST Traversal
|
|
174
|
-
|
|
175
|
-
Converts JSX components into sanitized hash-able objects:
|
|
176
|
-
|
|
177
|
-
- Recursively processes JSX elements and their children
|
|
178
|
-
- Identifies GT components vs regular HTML elements
|
|
179
|
-
- Extracts content from `Branch`/`Plural` component attributes
|
|
180
|
-
- Generates stable hash representations for consistent builds
|
|
181
|
-
|
|
182
|
-
## Two-Pass Traversal System
|
|
183
|
-
|
|
184
|
-
The plugin uses a two-pass approach to handle the circular dependency between translation functions and their usage:
|
|
185
|
-
|
|
186
|
-
### Pass 1: Collection
|
|
187
|
-
|
|
188
|
-
- **Discover translation functions**: Find `useGT()` and `getGT()` calls, assign unique counter IDs
|
|
189
|
-
- **Track variable assignments**: Follow `const t = useGT()` patterns using scope tracker
|
|
190
|
-
- **Collect content**: Gather `t()` calls and `<T>` components, associate with counter IDs
|
|
191
|
-
- **Generate hashes**: Calculate stable hashes for JSX content using AST traversal
|
|
192
|
-
- **Validate usage**: Check for dynamic content violations and report errors
|
|
193
|
-
|
|
194
|
-
### Pass 2: Transformation
|
|
195
|
-
|
|
196
|
-
- **Inject content arrays**: Add collected `t()` strings to `useGT()`/`getGT()` calls
|
|
197
|
-
- **Add hash attributes**: Insert `_hash` props into `<T>` components
|
|
198
|
-
- **Preserve order**: Use the same counter sequence to match content with functions
|
|
199
|
-
|
|
200
|
-
This approach solves the "chicken-and-egg" problem: we need to know what `t()` calls exist before we can inject content into the `useGT()` function that creates `t()`.
|
|
201
|
-
|
|
202
|
-
## Supported Bundlers
|
|
203
|
-
|
|
204
|
-
This plugin works with:
|
|
205
|
-
|
|
206
|
-
- ✅ **Webpack** 4, 5
|
|
207
|
-
- ✅ **Vite** 2, 3, 4, 5
|
|
208
|
-
- ✅ **Rollup** 2, 3, 4
|
|
209
|
-
- ✅ **esbuild** 0.15+
|
|
210
|
-
- ✅ **Next.js** (via gt-next)
|
|
211
|
-
- ✅ **Nuxt** (via unplugin auto-detection)
|
|
212
|
-
|
|
213
|
-
## Components Tracked
|
|
214
|
-
|
|
215
|
-
- **Translation**: `T`
|
|
216
|
-
- **Variables**: `Var`, `Num`, `Currency`, `DateTime`
|
|
217
|
-
- **Branching**: `Branch`, `Plural`
|
|
218
|
-
- **Functions**: `useGT()`, `getGT()`, and their callbacks
|
|
219
|
-
|
|
220
|
-
## Development Status
|
|
221
|
-
|
|
222
|
-
Files ported from Rust SWC plugin with their implementation status:
|
|
223
|
-
|
|
224
|
-
### ✅ Core Framework
|
|
225
|
-
|
|
226
|
-
- **`src/index.ts`** - Universal plugin entry point with unplugin integration
|
|
227
|
-
|
|
228
|
-
### ✅ Completed (with tests)
|
|
229
|
-
|
|
230
|
-
- **`src/visitor/analysis.ts`** - Component identification functions
|
|
231
|
-
- **`src/visitor/string-collector.ts`** - Two-pass transformation system
|
|
232
|
-
- **`src/logging.ts`** - Logger implementation
|
|
233
|
-
- **`src/visitor/errors.ts`** - Error message creation
|
|
234
|
-
|
|
235
|
-
### 🚧 Implemented (needs integration)
|
|
236
|
-
|
|
237
|
-
- **`src/visitor/scope-tracker.ts`** - Scope tracking and variable management
|
|
238
|
-
- **`src/visitor/import-tracker.ts`** - Import tracking and component resolution
|
|
239
|
-
|
|
240
|
-
### ❌ Not Yet Implemented
|
|
241
|
-
|
|
242
|
-
- **`src/ast/traversal.ts`** - JSX to sanitized objects conversion
|
|
243
|
-
- **`src/ast/utilities.ts`** - AST utility functions
|
|
244
|
-
- **`src/hash.ts`** - Hash generation utilities
|
|
245
|
-
- **`src/whitespace.ts`** - Whitespace handling utilities
|
|
246
|
-
- **`src/visitor/transform.ts`** - Main transformation logic
|
|
247
|
-
- **`src/visitor/jsx-utils.ts`** - JSX processing utilities
|
|
248
|
-
- **`src/visitor/expr-utils.ts`** - Expression analysis utilities
|
|
249
|
-
|
|
250
|
-
## Contributing
|
|
251
|
-
|
|
252
|
-
This plugin is part of the General Translation ecosystem. The transformation logic is gradually being ported from the existing Rust SWC plugin to provide broader bundler support.
|
|
253
|
-
|
|
254
|
-
### Test Files Structure
|
|
255
|
-
|
|
256
|
-
```
|
|
257
|
-
src/
|
|
258
|
-
├── __tests__/
|
|
259
|
-
│ ├── index.test.ts (unplugin integration tests)
|
|
260
|
-
├── visitor/
|
|
261
|
-
│ └── __tests__/
|
|
262
|
-
│ ├── string-collector.test.ts ✅
|
|
263
|
-
│ ├── analysis.test.ts (pending)
|
|
264
|
-
│ ├── scope-tracker.test.ts (pending)
|
|
265
|
-
│ └── import-tracker.test.ts (pending)
|
|
266
|
-
├── logging.test.ts (pending)
|
|
267
|
-
└── ast/
|
|
268
|
-
└── __tests__/
|
|
269
|
-
└── traversal.test.ts (pending)
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
## License
|
|
273
|
-
|
|
274
|
-
MIT - General Translation, Inc.
|
package/dist/index.d.ts
CHANGED
|
@@ -61,7 +61,7 @@ export interface GTUnpluginOptions extends PluginConfig {
|
|
|
61
61
|
*/
|
|
62
62
|
declare const gtUnplugin: import("unplugin").UnpluginInstance<GTUnpluginOptions | undefined, boolean>;
|
|
63
63
|
export default gtUnplugin;
|
|
64
|
-
export declare const webpack: (options?: GTUnpluginOptions | undefined) =>
|
|
64
|
+
export declare const webpack: (options?: GTUnpluginOptions | undefined) => WebpackPluginInstance;
|
|
65
65
|
export declare const vite: (options?: GTUnpluginOptions | undefined) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
|
|
66
66
|
export declare const rollup: (options?: GTUnpluginOptions | undefined) => import("rollup").Plugin<any> | import("rollup").Plugin<any>[];
|
|
67
67
|
export declare const esbuild: (options?: GTUnpluginOptions | undefined) => import("unplugin").EsbuildPlugin;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAYxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,YAAY;CAEtD;AAED;;;;;;;;GAQG;AACH,QAAA,MAAM,UAAU,6EAgFf,CAAC;AAGF,eAAe,UAAU,CAAC;AAC1B,eAAO,MAAM,OAAO,oEAAqB,CAAC;AAC1C,eAAO,MAAM,IAAI,wGAAkB,CAAC;AACpC,eAAO,MAAM,MAAM,4GAAoB,CAAC;AACxC,eAAO,MAAM,OAAO,+EAAqB,CAAC"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAA0C;AAC1C,sDAAwC;AACxC,iEAAwC;AACxC,+DAAuC;AAKvC,kCAAkC;AAClC,yFAAwH;AACxH,0FAA0H;AAC1H,gDAA6C;AAC7C,wDAAqD;AACrD,iGAAoI;AACpI,kGAAsI;AACtI,wDAAiE;AACjE,mEAAgE;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAA0C;AAC1C,sDAAwC;AACxC,iEAAwC;AACxC,+DAAuC;AAKvC,kCAAkC;AAClC,yFAAwH;AACxH,0FAA0H;AAC1H,gDAA6C;AAC7C,wDAAqD;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/compiler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Universal plugin for compile-time optimization of GT translation components",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@babel/traverse": "^7.23.0",
|
|
24
24
|
"@babel/types": "^7.23.0",
|
|
25
25
|
"unplugin": "^2.3.10",
|
|
26
|
-
"generaltranslation": "7.6.
|
|
26
|
+
"generaltranslation": "7.6.5"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/babel__core": "^7.20.0",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"release:alpha": "pnpm run build:clean && pnpm publish --tag alpha",
|
|
53
53
|
"release:beta": "pnpm run build:clean && pnpm publish --tag beta",
|
|
54
54
|
"release:latest": "pnpm run build:clean && pnpm publish --tag latest",
|
|
55
|
-
"lint": "eslint \"src/**/*.{js,ts,tsx}\"
|
|
56
|
-
"lint:fix": "eslint \"src/**/*.{js,ts,tsx}\"
|
|
55
|
+
"lint": "eslint \"src/**/*.{js,ts,tsx}\"",
|
|
56
|
+
"lint:fix": "eslint \"src/**/*.{js,ts,tsx}\" --fix",
|
|
57
57
|
"test": "vitest run",
|
|
58
58
|
"test:watch": "vitest"
|
|
59
59
|
}
|