-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@formatjs/swc-plugin-experimental): add Bazel rules for swc-plug…
…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
1 parent
c874911
commit 0466124
Showing
182 changed files
with
2,688 additions
and
3,194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ node_modules | |
.bazelrc.user | ||
*.log | ||
.idea | ||
.swc | ||
rust/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,7 @@ CONFIG_FILES = [ | |
"jest.config.js", | ||
"package.json", | ||
"tsconfig.json", | ||
".taplo.toml", | ||
] | ||
|
||
[ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*", | ||
]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.