0.11.2-rc.2 • Published 8 months ago

@discord/rspack-intl-loader v0.11.2-rc.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@discord/rspack-intl-loader

A Webpack/Rspack loader for i18n message definition files using @discord/intl. This loader handles both definitions and translations as a single group, emitting the appropriate file types and contents based on the kind of file provided.

Usage

Add the loader as a rule for all kinds of messages files in your application:

const INTL_MESSAGES_REGEXP = /\.messages\.(js|json|jsona)$/;

rules = [
  {
    test: INTL_MESSAGES_REGEXP,
    loader: '@discord/rspack-intl-loader',
  },
];

Make sure that the intl loader is the only transformation that happens on these files, or at least is the first one ( before e.g. minification). The loader relies on the structure of the raw source (with ES6 import and export statements, for example) and will not work on transpiled modules. To do this, just be sure to exclude the file patterns from all other JS transformations.

Consumers

Note that you'll also want/need the @discord/swc-intl-message-transformer plugin applied to your JS compilation to ensure that message usages compile to the same keys that match the compiled definitions.

rules = [
  {
    loader: 'builtin:swc-loader',
    exclude: [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
          // `require.resolve` directly creates an absolute path for rspack to 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'] } },
            ],
          ],
        },
      },
    },
  },
];

Webpack compatibility

Webpack processes asset files slightly differently than Rspack and relies on some different conventions. Most notably, Webpack's default transformations won't handle converting ESM back to CommonJS automatically (without configuring another loader, for example). To handle this, use the jsonExportMode option for the loader to have it emit converted CommonJS modules instead of ESM:

rules = [
  {
    test: INTL_MESSAGES_REGEXP,
    use: [
      {
        loader: '@discord/rspack-intl-loader',
        // This should work to be compatible with Webpack builds
        options: { jsonExportMode: 'webpack' },
      },
    ],
  },
];
0.11.2-rc.2

8 months ago

0.11.2-rc.1

8 months ago

0.11.2-rc.0

8 months ago

0.11.1-rc.0

8 months ago

0.11.1

8 months ago

0.10.2

8 months ago

0.10.3

8 months ago

0.10.4

8 months ago

0.10.0

8 months ago

0.10.0-rc.4

8 months ago

0.10.0-rc.3

8 months ago

0.10.0-rc.2

8 months ago

0.10.0-rc.1

8 months ago

0.9.0

9 months ago

0.9.1

9 months ago

0.9.0-rc.2

9 months ago

0.9.0-rc.1

9 months ago

0.8.0

9 months ago

0.8.0-rc.1

9 months ago

0.8.0-rc.0

9 months ago

0.7.1

9 months ago

0.7.1-rc.0

9 months ago

0.7.0

9 months ago

0.6.2

9 months ago

0.6.1

9 months ago

0.5.2

9 months ago

0.5.0

9 months ago

0.4.2

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.0

9 months ago

0.2.2

9 months ago

0.2.1-rc.2

9 months ago

0.2.1-rc.1

10 months ago

0.2.1-rc.0

10 months ago

0.2.0

10 months ago

0.1.3-rc.0

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0-rc.1

10 months ago

0.1.0-rc.0

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6-rc.2

10 months ago

0.0.6-rc.1

11 months ago

0.0.6-rc.0

11 months ago

0.0.5

11 months ago

0.0.4-rc.0

11 months ago

0.0.3

11 months ago

0.0.2-rc.2

11 months ago

0.0.2-rc.1

11 months ago

0.0.1

11 months ago