Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Unified documentation of all exports"v9.0.06/25/2026
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
@intlayer/babel Package
The @intlayer/babel package provides a set of specialized Babel plugins for Intlayer. These plugins cover the entire build cycle: extracting content declarations, rewriting useIntlayer / getIntlayer calls to optimized dictionary imports, pruning unused fields, and minifying field names.
Installation
Copy the code to the clipboard
Exports
Import:
Copy the code to the clipboard
Plugins
Open the table in a modal to view all data content clearly
| Function / Class | Description |
|---|---|
intlayerExtractBabelPlugin | Babel plugin that extracts translatable content from source files and injects useIntlayer / getIntlayer hooks automatically. Designed for use with Next.js and Babel-based build tools. |
intlayerOptimizeBabelPlugin | Babel plugin that transforms useIntlayer and getIntlayer calls and rewrites their imports to optimized JSON dictionary imports (static, dynamic, or via fetch). |
intlayerPurgeBabelPlugin | Babel plugin that analyses source files and rewrites compiled dictionary JSON files to remove unused fields (build.purge) or rename them to short aliases (build.minify). |
intlayerMinifyBabelPlugin | Babel plugin that rewrites source files to use the short field aliases assigned during the minification phase (e.g. content.title ← content.a). Relies on intlayerPurgeBabelPlugin. |
makeFieldRenameBabelPlugin | Factory function that produces a Babel plugin to rename dictionary content field accesses in source files according to the dictionaryKeyToFieldRenameMap populated in the PruneContext. |
makeUsageAnalyzerBabelPlugin | Factory function that produces a Babel plugin to analyze the usage of useIntlayer / getIntlayer in the source code and aggregate field usage data in the shared PruneContext. |
getSharedPruneContext | Helper function that returns the shared PruneContext object for the specified base directory, or null if it has not been initialized yet. |
Plugin Configuration Utilities
Open the table in a modal to view all data content clearly
| Function | Description |
|---|---|
getExtractPluginOptions | Loads the Intlayer configuration and returns ExtractPluginOptions ready for use with intlayerExtractBabelPlugin. |
getOptimizePluginOptions | Loads the Intlayer configuration and compiled dictionaries, and returns OptimizePluginOptions ready for use with intlayerOptimizeBabelPlugin. |
getPurgePluginOptions | Loads the Intlayer configuration and returns PurgePluginOptions ready for use with intlayerPurgeBabelPlugin. |
getMinifyPluginOptions | Loads the Intlayer configuration and returns MinifyPluginOptions ready for use with intlayerMinifyBabelPlugin. |
Types
Open the table in a modal to view all data content clearly
| Type | Description |
|---|---|
CompilerMode | Compiler mode: 'dev' for development with HMR support, or 'build' for production builds. |
ExtractPluginOptions | Options for intlayerExtractBabelPlugin: files list, configuration, onExtract callback, etc. |
ExtractResult | Extraction result: dictionary key, file path, content, and locale. |
OptimizePluginOptions | Options for intlayerOptimizeBabelPlugin: dictionary paths, import mode, mode map per dictionary, etc. |
PurgePluginOptions | Options for intlayerPurgeBabelPlugin: base directory, purge/minify/optimize flags, component files list. |
MinifyPluginOptions | Options for intlayerMinifyBabelPlugin: base directory, minify/optimize/editorEnabled flags. |
PruneContext | Shared state between analysis and pruning plugins: field usage maps, rename maps, etc. |
DictionaryFieldUsage | Field usage result for a single dictionary: Set<string> or 'all' when static analysis is inconclusive. |
NestedRenameEntry | Node in the rename tree: the shortName and the children map. |
NestedRenameMap | One level in the rename tree: Map<string, NestedRenameEntry>. |
CompatCallerConfig | Configuration for a compatible usage analyzer for compat-adapter packages (caller name and processing options). |
ScriptBlock | Script block extracted from a SFC file (Vue or Svelte): content, start offset, and end offset. |
Utility Functions
Open the table in a modal to view all data content clearly
| Function | Description |
|---|---|
generateShortFieldName | Converts an integer (starting from zero) into a short alphabetic identifier: 0 → 'a', 25 → 'z', 26 → 'aa', etc. |
buildNestedRenameMapFromContent | Recursively builds a NestedRenameMap from the content value of a compiled dictionary, respecting Intlayer node structures (translation, enumeration, etc.). |
createPruneContext | Creates a new empty PruneContext object initialized with default values. |
extractScriptBlocks | Extracts <script> blocks from SFC files (Vue / Svelte) for subsequent Babel analysis. |
BABEL_PARSER_OPTIONS | Constant representing Babel parser options covering the supported frameworks (React/Vue/Svelte/Angular/...). |
INTLAYER_CALLER_NAMES | Constant list of original Intlayer caller names: ['useIntlayer', 'getIntlayer']. |
Example Usage
Copy the code to the clipboard
Note: TheintlayerPurgeBabelPluginmust be declared beforeintlayerMinifyBabelPlugin, because the latter reads the rename map built by the former. In addition, they both must be preceded byintlayerOptimizeBabelPluginso that it can see the dictionary keys before theuseIntlayercalls are rewritten.
