Skip to content

Commit

Permalink
feat(@formatjs/swc-plugin-experimental): add Bazel rules for swc-plug…
Browse files Browse the repository at this point in the history
…in-experimental (#3881)

This PR introduces the Bazel rules that can build rust crates for swc-plugin-formatjs, as well as cross-compilation to WASM for npm distribution.
  • Loading branch information
pyrocat101 committed Nov 29, 2022
1 parent c874911 commit 0466124
Show file tree
Hide file tree
Showing 182 changed files with 2,688 additions and 3,194 deletions.
2 changes: 1 addition & 1 deletion .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ packages/intl-relativetimeformat/node_modules
packages/intl/node_modules
packages/react-intl/examples/node_modules
packages/react-intl/node_modules
packages/swc-plugin-formatjs/node_modules
packages/swc-plugin-experimental/node_modules
packages/swc-plugin/node_modules
packages/ts-transformer/integration-tests/node_modules
packages/ts-transformer/node_modules
Expand Down
2 changes: 0 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ build --action_env=PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/
# Give me dem stack traces
build --verbose_failures

# Cache action outputs on disk so they persist across output_base and bazel shutdown (eg. changing branches)
build --disk_cache=~/.cache/bazel-disk-cache
build --experimental_remote_cache_compression
build --remote_download_toplevel
common --bes_upload_mode=fully_async
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
.bazelrc.user
*.log
.idea
.swc
rust/target
5 changes: 5 additions & 0 deletions .taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# https://taplo.tamasfe.dev/configuration/formatter-options.html
[formatting]
align_entries = true
indent_tables = true
reorder_keys = true
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recommendations": [
"rust-lang.rust-analyzer",
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"foxundermoon.shell-format",
"ms-azuretools.vscode-docker"
]
}
12 changes: 9 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true,
"dist": true
"dist": true,
".swc": true,
},
"files.watcherExclude": {
"**/.git/objects/**": true,
Expand Down Expand Up @@ -42,10 +43,12 @@
],
"typescript.tsdk": "node_modules/typescript/lib",
"npm.packageManager": "yarn",
"prettier.packageManager": "yarn",
"eslint.packageManager": "yarn",
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"rust-analyzer.linkedProjects": [
"rust/Cargo.toml"
],
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand All @@ -60,5 +63,8 @@
},
"[shellscript]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
},
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
}
}
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ CONFIG_FILES = [
"jest.config.js",
"package.json",
"tsconfig.json",
".taplo.toml",
]

[
Expand Down
44 changes: 44 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,47 @@ buildbuddy_deps()
load("@io_buildbuddy_buildbuddy_toolchain//:rules.bzl", "buildbuddy")

buildbuddy(name = "buildbuddy_toolchain")

# Rust
# ----

# To find additional information on this release or newer ones visit:
# https://github.com/bazelbuild/rules_rust/releases
http_archive(
name = "rules_rust",
sha256 = "324c2a86a8708d30475f324846b35965c432b63a35567ed2b5051b86791ce345",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.13.0/rules_rust-v0.13.0.tar.gz"],
# Workaround for https://github.com/bazelbuild/rules_rust/issues/1330
patches = ["//tools:rules_rust.patch"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
load("@rules_rust//crate_universe:defs.bzl", "crates_repository")

rules_rust_dependencies()

rust_register_toolchains(
edition="2021",
extra_target_triples = [
"wasm32-unknown-unknown",
"wasm32-wasi",
],
)

# Run `CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index` after updating
crates_repository(
name = "crate_index",
cargo_lockfile = "//rust:Cargo.lock",
manifests = [
"//rust:Cargo.toml",
"//rust/icu-messageformat-parser:Cargo.toml",
"//rust/swc-formatjs-visitor:Cargo.toml",
"//rust/swc-plugin-formatjs:Cargo.toml",
# FIX THIS
# "//rust/swc-formatjs-custom-transform:Cargo.toml",
],
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
"@jest/types": "28",
"@lerna-lite/cli": "^1.10.0",
"@lerna-lite/list": "^1.10.0",
"@napi-rs/cli": "^2.11.0",
"@swc-node/register": "^1.5.1",
"@swc/core": "^1.3.20",
"@swc/jest": "^0.2.23",
"@taplo/cli": "^0.4.2",
"@testing-library/jest-dom": "^5.11.5",
"@testing-library/react": "^13.4.0",
"@types/babel__core": "^7.1.7",
Expand Down Expand Up @@ -113,7 +118,7 @@
"test262-harness": "10",
"ts-jest": "28",
"ts-loader": "^9.1.2",
"ts-node": "^10.8",
"ts-node": "^10.8.1",
"tsd": "^0.24.1",
"tslib": "2.4.0",
"typescript": "^4.7",
Expand Down
45 changes: 45 additions & 0 deletions packages/swc-plugin-experimental/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//npm/private:npm_package.bzl", "npm_package")
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
load("//tools:jest.bzl", "jest_test")

npm_link_all_packages(name = "node_modules")

PACKAGE_NAME = "swc-plugin-experimental"

copy_file(
name = "wasm",
src = "//rust/swc-plugin-formatjs:swc_plugin_formatjs.wasm",
out = "index.wasm",
)

npm_package(
name = PACKAGE_NAME,
srcs = [
"LICENSE.md",
"README.md",
"package.json",
"index.wasm",
],
package = "@formatjs/%s" % PACKAGE_NAME,
visibility = ["//visibility:public"],
)

jest_test(
name = "test_plugin",
srcs = glob(
["tests/**/*.test.ts"],
),
snapshots = glob([
"tests/**/*.snap",
]),
deps = [
"index.wasm",
"tests/transform.ts",
"//:node_modules/@swc/core",
"//:node_modules/@swc/jest",
"//:node_modules/@types/node",
] + glob([
"tests/fixtures/**/*",
]),
)
10 changes: 10 additions & 0 deletions packages/swc-plugin-experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# 0.1.0 (2022-10-17)

### Features

* **@formatjs/swc-plugin-experimental:** move kwonoj/swc-plugin-formatjs into the monorepo ([#3880](https://github.com/formatjs/formatjs/pull/3880)) ([448d07b](https://github.com/formatjs/formatjs/commit/448d07bf9398acc34b12752e3507f0a1e6739a83))
File renamed without changes.
95 changes: 95 additions & 0 deletions packages/swc-plugin-experimental/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# @formatjs/swc-plugin-experimental

`@formatjs/swc-plugin-experimental` is a port of `babel-plugin-formatjs` written
in Rust, for the SWC's experimental [plugin][] system. Transform can currently
be performed via SWC's wasm-based plugin, and in the future also as a custom
pass in Rust-side transform chains.

[plugin]: https://github.com/swc-project/swc/issues/2635

## 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 @formatjs/swc-plugin-experimental
```

Then add plugin into swc's configuration:

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

jsc: {
// ...
experimental: {
plugins: [
[
'@formatjs/swc-plugin-experimental',
{
// 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`.
```rust
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. `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.
-->
27 changes: 27 additions & 0 deletions packages/swc-plugin-experimental/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "swc-plugin-experimental",
"version": "0.1.0",
"description": "SWC plugin for formatjs",
"main": "./index.wasm",
"files": [
"package.json",
"README.md",
"LICENSE",
"index.wasm"
],
"repository": {
"type": "git",
"url": "git+https://github.com/formatjs/formatjs"
},
"keywords": [
"SWC",
"plugin",
"formatjs"
],
"author": "OJ Kwon <kwon.ohjoong@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/formatjs/formatjs/issues"
},
"homepage": "https://github.com/formatjs/formatjs#readme"
}

0 comments on commit 0466124

Please sign in to comment.