@bluecateng/l10n-swc-plugin
v0.1.1
Published
SWC plugin to compile the l10n macros
Keywords
Readme
@bluecateng/l10n-swc-plugin
SWC plugin to compile the l10n macros, it can be used instead of babel-plugin-macros with any tool which
supports SWC Wasm plugins, such as Rspack's builtin:swc-loader, @swc/core, @swc/cli, or @swc/jest.
The source code keeps importing the macros from @bluecateng/l10n.macro, and the generated code is the same as the one
generated by the macro, so both can be used in the same project, e.g. SWC for the build and Babel for the tests.
Installation
npm i -D @bluecateng/l10n-swc-plugin @bluecateng/l10n.macro@bluecateng/l10n.macro is only required for the type definitions.
Compatibility
SWC Wasm plugins are only compatible with a range of SWC versions, this plugin is built with swc_core 82, and it has
been verified with @swc/core 1.16 and @rspack/core 2.2.
See selecting the version for more details.
Options
| Option | Description |
| ------------ | ----------------------------------------------------------------------------------------------- |
| module | Path to the l10n module, relative paths are resolved from the current directory of the process. |
| hashLength | The length of the hash used as message key, if not set the ICU string is used as key. |
SWC always passes the current directory of the process to the plugin, the cwd option of @swc/core is ignored, so a
relative module only works when the build runs from the directory it is relative to, usually the project root.
@bluecateng/l10n-swc-plugin/config always returns an absolute path.
Usage
JavaScript configuration
When the SWC configuration is written in JavaScript, @bluecateng/l10n-swc-plugin/config returns the plugin entry using
the values from the l10n configuration (bc-l10n in package.json or .bc-l10nrc), so they don't need to be repeated.
Rspack
import l10nSwcPlugin from '@bluecateng/l10n-swc-plugin/config';
export default {
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {syntax: 'ecmascript', jsx: true},
transform: {react: {runtime: 'automatic'}},
experimental: {plugins: [l10nSwcPlugin()]},
},
},
},
{test: /\.po$/, loader: '@bluecateng/l10n-loader'},
],
},
};@swc/core
import {transformFile} from '@swc/core';
import l10nSwcPlugin from '@bluecateng/l10n-swc-plugin/config';
const {code} = await transformFile('src/index.js', {
jsc: {
parser: {syntax: 'ecmascript', jsx: true},
experimental: {plugins: [l10nSwcPlugin()]},
},
});.swcrc
.swcrc is a JSON file, so the options must be specified explicitly, they must match the l10n configuration, and a
relative module must be relative to the directory where swc is run.
This can be used with @swc/cli for packages which are built without a bundler, e.g.
swc src -d dist --strip-leading-paths.
{
"jsc": {
"parser": {"syntax": "ecmascript", "jsx": true},
"transform": {"react": {"runtime": "automatic"}},
"experimental": {
"plugins": [["@bluecateng/l10n-swc-plugin", {"module": "src/l10n", "hashLength": 3}]]
}
}
}Differences with the macro
- Macros used inside a non-macro expression in another macro, e.g.
t`Items: ${items.map((i) => t`Item ${i}`)}`, are also compiled,babel-plugin-macrosleaves them unchanged. - The trailing
/*l10n: …*/comments are always added, the macro omits them whenNODE_ENVisproduction. - Duplicated hashes are only detected in the same file,
l10n-clistill detects them when extracting the strings. - An invalid usage reports an error with the source location instead of throwing.
