react-intl-webpack-plugin
raw JSON →This webpack plugin integrates with babel-plugin-react-intl to extract and aggregate internationalization messages from React components into a single JSON file, typically named reactIntlMessages.json. Version 0.3.1 is the latest stable release. It assists in the i18n workflow by combining extracted message descriptors during the webpack build and placing the output in the webpack output directory. Compared to other i18n tools, it focuses on the extraction and aggregation step for webpack-based React projects, leveraging babel metadata. It requires babel-loader >= 6.0.0, babel-plugin-react-intl >= 2.3.0, and webpack >= 4.0.0. The plugin is designed to work with translation memory tools and a separate reactIntlJson-loader for loading translated JSON files.
Common errors
error TypeError: ReactIntlPlugin is not a constructor ↓
error Error: Cannot find module 'react-intl-webpack-plugin' ↓
Warnings
gotcha When using webpack-dev-server, assets are only in memory; the output file is not written to disk. ↓
gotcha If no messages are generated, babel-loader's cache may be stale; clean the cache or set cacheDirectory to false. ↓
Install
npm install react-intl-webpack-plugin yarn add react-intl-webpack-plugin pnpm add react-intl-webpack-plugin Imports
- default wrong
const { ReactIntlPlugin } = require('react-intl-webpack-plugin');correctconst ReactIntlPlugin = require('react-intl-webpack-plugin'); // or import ReactIntlPlugin from 'react-intl-webpack-plugin'; - metadataContextFunctionName
ReactIntlPlugin.metadataContextFunctionName
Quickstart
// webpack.config.js
const ReactIntlPlugin = require('react-intl-webpack-plugin');
module.exports = {
// ... other config
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
metadataSubscribers: [ReactIntlPlugin.metadataContextFunctionName],
plugins: [
'@babel/plugin-transform-runtime',
['react-intl', { enforceDescriptions: false }]
],
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}
]
},
plugins: [
new ReactIntlPlugin()
]
};