Skip to content

Latest commit

 

History

History

formatjs

Folders and files

README.md

@swc/plugin-formatjs

FormatJS SWC plugin, maintained by SWC team.

Usage

The default message descriptors for the app's default language will be processed from: defineMessages(), defineMessage(), intl.formatMessage and <FormattedMessage>; all of which are named exports of the React Intl package.

Next.js

import { NextConfig } from 'next';

const nextConfig: NextConfig = {
  experimental: {
    swcPlugins: [
      [
        require.resolve('@swc/plugin-formatjs'),
        {
          idInterpolationPattern: '[md5:contenthash:hex:10]',
          additionalComponentNames: ['F'],
          ast: true,
        },
      ],
    ],
  }
}

Vite

import react from '@vitejs/plugin-react-swc'
import { defineConfig } from 'vite'

export default defineConfig((env) => ({
  plugins: [
    react({
      tsDecorators: true,
      plugins: [
        [
          '@swc/plugin-formatjs',
          {
            idInterpolationPattern: '[md5:contenthash:hex:10]',
            additionalComponentNames: ['F'],
            ast: true,
          },
        ],
      ],
    }),
  ]
}))

Options

idInterpolationPattern

A template string that allows you to override the ID both in the extracted javascript and messages. It's used only if the message ID is empty.

Use [content] to insert the unhashed content instead of a hash: the default message, followed by # and the description when one is present. For example, idInterpolationPattern: '[content]' uses each default message as its own ID.

removeDefaultMessage

Remove defaultMessage field in generated js after extraction.

extractSourceLocation

Whether the metadata about the location of the message in the source file should be extracted. If true, then file, start, and end fields will exist for each extracted message descriptors. Defaults to false.

additionalComponentNames

Additional component names to extract messages from, e.g: ['FormattedFooBarMessage']. NOTE: By default we check for the fact that FormattedMessage are imported from moduleSourceName to make sure variable alias works. This option does not do that so it's less safe.

additionalFunctionNames

Additional function names to extract messages from, e.g: ['']. Use this if you prefer to alias formatMessage to something shorter like ``.

ast

Pre-parse defaultMessage into AST for faster runtime perf. This flag doesn't do anything when removeDefaultMessage is true.

throws

Whether to throw when a message descriptor cannot be statically evaluated for extraction. Defaults to true. Set to false to skip descriptors that fail extraction and leave them unchanged.

preserveWhitespace

Whether to preserve whitespace and newlines.

pragma

A string that allows you to extract metadata from comments in your source files. When set, the plugin will look for comments containing the pragma string and parse key:value pairs that follow it.

For example, with pragma: "@formatjs", a comment like:

// @formatjs project:web locale:en region:us

Will extract the metadata: {project: "web", locale: "en", region: "us"} that gets included with the extracted messages. This is useful for adding contextual information to your message extractions.

@swc/plugin-formatjs

15.0.3

Patch Changes

  • Update swc_core to 85.0.0.

15.0.2

Patch Changes

  • ea2665b: Update swc_core to 84.0.1.

15.0.1

Patch Changes

  • Update swc_core to 83.0.0.

15.0.0

Major Changes

  • 1fa243d: Update swc_core to 82.2.0.

Patch Changes

  • 58dafd0: Support the [content] placeholder in idInterpolationPattern, which inserts the unhashed default message (plus #description, if present).
  • 58dafd0: Generate the same message ID for an empty description as for an omitted one, matching babel-plugin-formatjs.

14.0.0

Major Changes

  • 5c6d86d: Update swc_core to 82.0.0.

13.0.0

Major Changes

  • e995ea5: build!: Update swc_core to v81.0.2

12.0.0

Major Changes

  • d90fa6c: build!: Update swc_core to v81.0.0

11.0.0

Major Changes

  • a9a7fe9: build!: Update swc_core to v78.0.0

10.0.0

Major Changes

  • 74931df: build!: Update swc_core to v77.0.0

9.19.0

Minor Changes

  • 8d2db7f: build: Update swc_core to v76.0.0

9.18.0

Minor Changes

  • 1a6fdee: build: Update swc_core to v75.0.0

9.17.0

Minor Changes

  • 6051cea: build: Update swc_core to v74.0.0

9.16.0

Minor Changes

  • b7be81b: build: Update swc_core to v73.0.0

9.15.0

Minor Changes

  • 4b382e7: build: Update swc_core to v72.0.0

9.14.1

Patch Changes

  • 4f2efe7: Add throws: false support to skip non-static FormatJS descriptors without failing the transform.
  • e785517: build: Update swc_core to v71.0.5

9.14.0

Minor Changes

  • 7cc5e96: build: Update swc_core to v72

9.13.0

Minor Changes

  • c3013d7: build: Update swc_core to v69

9.12.1

Patch Changes

  • c5bd878: Avoid hygiene proxy crashes by evaluating only static formatjs descriptor values. The legacy Rust visitor constructor now ignores its evaluator argument; use create_formatjs_visitor_without_evaluator for new integrations.

9.12.0

Minor Changes

  • 3b04284: build: Update swc_core to v68

9.11.0

Minor Changes

  • 3b4d5b9: build: Update swc_core to v67

9.10.0

Minor Changes

  • 5e967fc: build: Update swc_core to v66.0.3

9.9.0

Minor Changes

  • d2f9cfa: build: Update swc_core to v65.0.0

9.8.0

Minor Changes

  • a0fb975: Update swc_core to v61.0.1

9.7.0

Minor Changes

  • 1cbeb5a: build: Update swc_core to v58.0.1

9.6.0

Minor Changes

  • 2113ddb: build: Update swc_core to v57

9.5.0

Minor Changes

  • 870774a: build: Update swc_core to v56

9.4.0

Minor Changes

  • 15f32da: fix(formatjs): Variable evaluate concatenation
  • 66f5258: build: Update swc_core to v55.x.x

9.3.0

Minor Changes

  • 9c163ec: Update swc_core to v53, really

9.2.0

Minor Changes

  • 3799fa4: Update swc_core to v53.0.0

9.1.0

Minor Changes

  • 0a1d66f: Update swc_core to v52.0.0

9.0.1

Patch Changes

  • a73246f: build: Update swc_core to v49.0.0

9.0.0

Major Changes

  • 6c46f86: feat: Enable backward-compatibility feature

8.1.1

Patch Changes

  • ad79e09: Update swc_core to v48.0.4

8.1.0

Minor Changes

  • 7a0fbdb: Update swc_core to v48

8.0.0

Major Changes

  • 593f438: Update swc_core to v47

7.0.0

Major Changes

  • 25e0c2c: Update swc_core to v46.0.0

6.5.0

Minor Changes

  • c324a59: Update swc_core to v45

6.4.0

Minor Changes

  • 8bad98d: Update swc_core to v44

6.3.0

Minor Changes

  • 47be132: Update swc_core to v42

6.2.0

Minor Changes

  • 0c9d7a1: build: Update swc_core to v39.0.0

6.1.0

Minor Changes

  • a872100: build: Update swc_core to v38

6.0.4

Patch Changes

  • 9b08ff7: Update swc_core to v35

6.0.3

Patch Changes

  • 08db5e1: docs(formatjs): add Usage to readme
  • beabfa1: Add proper README

6.0.2

Patch Changes

  • 27cae40: Disallow typescript types to shrink size

6.0.1

Patch Changes

  • df280ad: Update swc_core to v34.0.0

6.0.0

Major Changes

  • 562e755: Update swc_core to v33

5.1.4

Patch Changes

  • 9a27285: fix(formatjs): Fix description extract for the template literal string

5.1.3

Patch Changes

  • 85719ca: Update swc_core to v32

5.1.2

Patch Changes

  • 467573b: fix(formatjs): Fix ast transform in jsx

5.1.1

Patch Changes

  • 45d1ac7: Update swc_core to v31

5.1.0

Minor Changes

  • 899d803: Add support for md5 hash_type in idInterpolationPattern

5.0.0

Major Changes

  • f7c8ad4: swc_core v29

4.0.1

Patch Changes

  • 8d5ce5c: Update swc_core to v28.0.0

4.0.0

Major Changes

  • cf2636b: Update swc_core to v27

3.2.4

Patch Changes

  • e3e743d: Update swc_core to v27

3.2.3

Patch Changes

  • f226c4b: Generate the same ID after React Compiler

3.2.2

Patch Changes

  • 5ddbaeb: Update swc_core to v23

3.2.1

Patch Changes

  • 1abaa28: fix(formatjs): Sort description objects before serialization

3.2.0

Minor Changes

  • 648faf7: add support for hex and base64url digest encodings in idInterpolationPattern placeholders

3.1.2

Patch Changes

  • d51d525: Update swc_core to v22.0.0

3.1.1

Patch Changes

  • cb94b92: Update swc_core to v21.0.1

3.1.0

Minor Changes

  • af2e35d: Add support for sha1 hash_type in idInterpolationPattern

3.0.1

Patch Changes

  • 31e3254: build: Update swc_core to v19.0.0

3.0.0

Major Changes

  • f0fee1d: Update swc_core to v15.0.1

2.4.0

Minor Changes

  • 72385e1: add support for idInterpolationPattern placeholders

2.3.1

Patch Changes

  • 04465bc: Update swc_core to v14.0.0, really

2.3.0

Minor Changes

  • bfa0a51: Update swc_core to v13

2.2.0

Minor Changes

  • b8c4e6c: Update swc_core to v12

2.1.0

Minor Changes

  • 4c8b0e2: Update swc_core

2.0.6

Patch Changes

  • e8973e8: Update swc_core to v10.2.3

2.0.5

Patch Changes

  • f436a09: Update swc_core to v10.

2.0.4

Patch Changes

  • 2ae5319: Fix parsing of unicode

2.0.3

Patch Changes

  • bb13cc8: Fix missing quotes of plural keys

2.0.2

Patch Changes

  • f155bce: Update swc_core to v9

2.0.1

Patch Changes

  • c9e75ce: Bump crate versions

2.0.0

Major Changes

  • 4574a70: Update swc_core to v8.0.1

1.0.3

Patch Changes

  • f3cea5f: Bump versions

1.0.2

Patch Changes

  • a73664c: Update swc_core to v6.0.2

1.0.1

Patch Changes

  • 4ff3b22: Move formatjs plugin to official plugin repository