SWC Plugin
SWC is an extensible Rust-based platform for the next generation of fast developer tools.
If you're using SWC in your project, you can opt for the @lingui/swc-plugin. This plugin, designed for SWC, is a Rust version of Lingui Macros.
SWC Compatibility
SWC Plugin support is still experimental. Semver backwards compatibility between different @swc/core versions is not guaranteed. You need to choose an appropriate version of the @lingui/swc-plugin to match the compatible @swc/core version.
It is recommended to check the plugins.swc.rs site to find the compatible version.
Installation
Install @lingui/swc-plugin as a development dependency:
- npm
- Yarn
- pnpm
npm install --save-dev @lingui/swc-plugin
yarn add --dev @lingui/swc-plugin
pnpm add --save-dev @lingui/swc-plugin
To ensure that the resolved version of @swc/core is one of the supported versions, you can use the resolutions field in the package.json file, which is supported by Yarn:
"resolutions": {
"@swc/core": "1.3.56"
}
or overrides for >npm@8.3
"overrides": {
"@swc/core": "1.3.56"
}
Usage
Add the following configuration to your .swcrc file:
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"experimental": {
"plugins": [
[
"@lingui/swc-plugin",
{
// Additional Configuration
}
]
]
}
}
}
If you use Next.js, add the following to your next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
experimental: {
swcPlugins: [
[
"@lingui/swc-plugin",
{
// Additional Configuration
},
],
],
},
};
module.exports = nextConfig;
Additional Configuration
Runtime Modules Configuration
You can configure the plugin by passing the runtimeModules option. This option is an object that maps runtime module names to their corresponding module paths and export names. It is essential for macros, which rely on referencing the global i18n object.
[
"@lingui/swc-plugin",
{
"runtimeModules": {
"i18n": ["@lingui/core", "i18n"],
"trans": ["@lingui/react", "Trans"]
}
}
]
For more details, refer to the Runtime Configuration section of the documentation.
Strip Non-Essential Fields
Lingui strips non-essential fields from builds if NODE_ENV is set to production. You can override this behavior by using the stripNonEssentialFields option. For example, if you want to keep all fields regardless of the environment, you can set:
[
"@lingui/swc-plugin",
{
"stripNonEssentialFields": false
}
]
Examples
If you would like to suggest a new feature or report a bug, please open an issue on the GitHub repository.