@ discord/ rspack- intl- loader (英语)
使用 intl 信件定义文件的 Webpack/ Rspack 加载器 @discord/intl。这个加载器将定义和翻译作为一组处理,根据所提供的文件类型释放适当的文件类型和内容.
使用量
添加装入器作为规则 全部 您应用程序中的信息文件类型 :
const discord_intl_MESSAGES_REGEXP = /\.messages\.(js|json|jsona)$/;
rules = [
{
test: discord_intl_MESSAGES_REGEXP,
loader: '@discord/rspack-intl-loader',
},
];
确保内装车是 仅限 在这些文件中发生的变换,或者至少是第一个变换(例如minification之前). 装载器依赖于原始源的结构(例如有ES6的出入口声明),不会在转接模块上工作. 要做到这一点,只是确保 exclude 所有其他JS变换的文件模式.
消费者
请注意,你也会想要/需要 @discord/swc-intl-message-transformer 插件应用到您的 JS 汇编中, 以确保消息用法汇编到与所汇编定义相匹配的密钥 .
rules = [
{
loader: 'builtin:swc-loader',
exclude: [discord_intl_MESSAGES_REGEXP],
options: {
jsc: {
experimental: {
// Just using a string here causes rspack/swc to error out immediately
// with "failed to get node_modules path", seemingly because it doesn't
// correctly give context for how the module should be resolved. Using
// a resolved path load directly instead.
//
// This plugin is responsible for obfuscating and minimizing intl
// message _usages_ across every source file, to match the compiled
// _definitions_ that are handled by rspack-intl-loader in a
// separate rule.
plugins: [
[
require.resolve('@discord/swc-intl-message-transformer'),
// Add configuration for other message import sources as needed.
{ extraImports: { '@app/intl': ['t', 'untranslated', 'international'] } },
],
],
},
},
},
},
];
Web 软件包兼容性
Webpack处理资产文件与Rspack略有不同,依赖一些不同的惯例. 最值得注意的是,Webpack的默认变换不会自动处理将无害环境管理转换回CommonJS(例如不配置另一个加载器). 要处理这个,请使用 jsonExportMode 选项:
rules = [
{
test: discord_intl_MESSAGES_REGEXP,
use: [
{
loader: '@discord/rspack-intl-loader',
// This should work to be compatible with Webpack builds
options: { jsonExportMode: 'webpack' },
},
],
},
];