GitXplorerGitXplorer
k

swc-plugin-formatjs

public
9 stars
0 forks
8 issues

Commits

List of commits on branch main.
Verified
830563ba8e20cad7a0c3072df9ca2c9b64244ce8

Merge pull request #46 from kwonoj/bump-deps

kkwonoj committed 2 years ago
Unverified
d21f4cd21fd7d932a1a4700a318745646da77f27

build(cargo): bump up version

kkwonoj committed 2 years ago
Verified
b3247d43a26df5f3991998138b092e739fdc9968

docs(readme): correct typo

kkwonoj committed 2 years ago
Unverified
9d9478a89e93cc31b9af08d906b9249d250f619e

build(cargo): update definitions

kkwonoj committed 2 years ago
Verified
ffa3854ca3ffe82ea8f81188800bbfe3f81e5432

Merge pull request #45 from kwonoj/release-docs

kkwonoj committed 2 years ago
Unverified
e9bdfeb9a2fd21863d6b5f3245323e7fcf894c4c

docs(readme): update docs

kkwonoj committed 2 years ago

README

The README file for this repository.

SWC-plugin-formatjs

SWC-plugin-formatjs

swc-plugin-formatjs is a port of babel-plugin-formatjs for the SWC. Transform can be performed either via SWC's wasm-based plugin, or using custom passes in rust side transform chains.

What does compatible exactly means?

This plugin attempts to mimic most of defined behavior of original plugin's test fixture. However, due to differences of plugin interfaces, as well as known limitations there are numbers of unsupported features with differences. You may able to check the list of github issues, as well as checking test fixtures.

Notably, any dynamic configuration options (overrideFn, onMsg...) are not supported, and few internal behaviors relying on static evaluation won't work.

NOTE: Package can have breaking changes without major semver bump

Given SWC's plugin interface itself is under experimental stage does not gaurantee semver-based major bump yet, this package also does not gaurantee semver compliant breaking changes yet. Please refer changelogs if you're encountering unexpected breaking behavior across versions.

Also, current implementation is largely unoptimized, and may have performance issues as initial focus was to pass existing test fixtures only.

Usage

Using SWC's wasm-based experimental plugin

First, install package via npm:

npm install --save-dev swc-plugin-formatjs

Then add plugin into swc's configuration:

interface PluginOptions {
  pragma: string;
  removeDefaultMessage: bool,
  idInterpolationPattern?: string,
  ast: bool,
  extractSorceLocation: bool,
  preserveWhitespace: bool,
  additionalFunctionNames: Array<string>,
  additionalComponentNames: Array<string>
}

jsc: {
  ...
  experimental: {
    plugins: [
      ["SWC-plugin-formatjs", {
        //pluginOptions
      }]
    ]
  }
}

Using custom transform pass in rust

There is a single interface exposed to create a visitor for the transform, which you can pass into before_custom_pass.

create_formatjs_visitor<C: Clone + Comments, S: SourceMapper>(
    source_map: std::sync::Arc<S>,
    comments: C,
    plugin_options: FormatJSPluginOptions,
    filename: &str,
) -> FormatJSVisitor<C, S>

Building / Testing

This package runs slightly modified original plugin's fixture tests against SWC with its wasm plugin & custom transform both. spec contains set of the fixtures & unit test to run it, as well as supplimental packages to interop between instrumentation visitor to node.js runtime.

Few npm scripts are supported for wrapping those setups.

  • build:all: Build all relative packages as debug build.
  • test: Runs unit test for wasm plugin & custom transform.