Compiler
gt-next's rust-based SWC plugin.
Compiler
gt-next includes a Rust-based SWC plugin that performs build-time analysis to catch common translation errors and optimize performance.
Features
Dynamic Content Detection
Detects unwrapped dynamic content in translation components:
// ❌ Invalid - dynamic content not wrapped
<T>Hello {userName}</T>
// ✅ Valid - dynamic content wrapped in variable component
<T>Hello <Var>{userName}</Var></T>
Function Call Validation
Detects non-literal arguments passed to translation functions:
const t = useGT();
// ❌ Invalid - template literals and concatenation
t(`Hello ${name}`)
t("Hello " + name)
// ✅ Valid - string literals with variable substitution
t("Hello, {name}!", { name })
Compile-time Hash Generation
Pre-computes translation hashes for better runtime performance:
// Input
<T>Hello world</T>
// Output (when enabled)
<T _hash="a1b2c3d4">Hello world</T>
Configuration
Configure the SWC plugin in your next.config.js
:
import { withGTConfig } from 'gt-next/config';
export default withGTConfig(nextConfig, {
locales: ['en', 'es'],
swcPluginOptions: {
logLevel: 'silent', // Control warning output
compileTimeHash: false, // Enable hash generation
},
});
Options
-
logLevel
: Controls warning output level'silent'
- No warnings (default for production)'error'
- Show as build errors'warn'
- Show as warnings (default for development)'info'
- Show info-level messages'debug'
- Show all debug information
-
compileTimeHash
: Enables compile-time hash generationfalse
- Disabled (default)true
- Generate hashes at build time for better performance
Limitations
The SWC plugin processes files individually and cannot detect violations in re-exported components:
// File A: export { T as Translate } from 'gt-next'
// File B: import { Translate } from './A'
<Translate>Hello {name}</Translate> // Won't be detected
For comprehensive coverage including re-exports, consider using the gt-next ESLint plugin.
How is this guide?