Unplugin for Lingui Macros
unplugin-lingui-macro is a bundler-agnostic plugin that compiles Lingui macros in plain JavaScript and TypeScript modules. It provides `unplugin` entrypoints for various build tools including Vite, Rollup, Webpack, esbuild, Rolldown, Rspack, and Bun. This package is specifically designed for applications that use Lingui macros outside of framework-specific files (e.g., `.svelte` or `.astro`), where dedicated framework plugins handle the transformation. The current stable version is 0.3.1, with releases occurring as part of the broader `lingui-for` monorepo, suggesting an active but coordinated release cadence. A key differentiator is its focus on generic JS/TS files, offering optional `linguiConfig` overrides for macro and runtime bindings, but it does not perform Lingui message extraction; that remains a task for `@lingui/cli` and related extractors.
Common errors
-
Error: Cannot find module 'unplugin-lingui-macro/vite'
cause Incorrect or missing bundler-specific suffix in the import path (e.g., `/vite`, `/rollup`, `/webpack`).fixEnsure you are importing the plugin with the correct suffix for your bundler, e.g., `import linguiMacro from 'unplugin-lingui-macro/vite';` for Vite. -
Internal server error: The macro you imported from "@lingui/react/macro" is not transformed.
cause The `unplugin-lingui-macro` plugin might not be correctly configured or enabled in your bundler, or `unplugin-auto-import` is not working as expected.fixVerify that `unplugin-lingui-macro` is added to your bundler's plugin list (e.g., `plugins: [linguiMacro()]` in Vite config). Ensure that Lingui dependencies are correctly installed and version compatible. -
[plugin:unplugin-lingui-macro] ReferenceError: i18n is not defined
cause The `@lingui/core` package, which provides the `i18n` runtime, is either not installed or not correctly configured/imported at runtime.fixInstall `@lingui/core` as a runtime dependency (`npm install @lingui/core`) and ensure that Lingui's `I18nProvider` (if using React) or `i18n` instance is initialized and available where needed.
Warnings
- breaking Version 0.3.0 unified transform-time Lingui config loading around a new `config` option and `defineConfig` helpers. Old methods for configuring Lingui through the plugin options might be removed or behave differently.
- gotcha This plugin requires `@lingui/core` version `^5.0.0` and Node.js 22+ for proper operation. Using older versions may lead to unexpected errors or incompatible macro compilation.
- gotcha The plugin is designed for plain JavaScript and TypeScript modules only. For framework-specific files (e.g., `.svelte` or `.astro`), you should use the corresponding framework-specific Lingui plugins like `lingui-for-svelte` or `lingui-for-astro`.
- gotcha This plugin is solely responsible for compiling Lingui macros at build time; it does not perform message extraction. You still need to configure and run `@lingui/cli` with appropriate extractors for translation message extraction.
Install
-
npm install unplugin-lingui-macro -
yarn add unplugin-lingui-macro -
pnpm add unplugin-lingui-macro
Imports
- linguiMacro
const linguiMacro = require('unplugin-lingui-macro');import linguiMacro from 'unplugin-lingui-macro/vite';
- defineConfig
import { defineConfig } from 'vite'; - t
import { t } from '@lingui/core/macro'; - LinguiMacroOptions
import type { LinguiMacroOptions } from 'unplugin-lingui-macro/types';
Quickstart
import { defineConfig } from "vite";
import linguiMacro from "unplugin-lingui-macro/vite";
export default defineConfig({
plugins: [
linguiMacro({
linguiConfig: {
runtimeConfigModule: {
i18n: ["@lingui/core", "i18n"],
},
},
}),
],
});
// Example of usage in a plain TS/JS module:
// src/constants.ts
import { t } from "@lingui/core/macro";
export const pageTitle = t`Settings`;
export const welcomeMessage = (name: string) => t`Hello, ${name}!`;