Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/desdaemon/swc-plugin-static-jsx

SWC plugin to transform JSX calls to static templates
https://github.com/desdaemon/swc-plugin-static-jsx

jsx swc-plugin templating

Last synced: about 2 months ago
JSON representation

SWC plugin to transform JSX calls to static templates

Awesome Lists containing this project

README

        

# swc-plugin-static-jsx

[![npm](https://img.shields.io/npm/v/swc-plugin-static-jsx)](https://www.npmjs.com/package/swc-plugin-static-jsx)
[![Rust](https://github.com/Desdaemon/swc-plugin-static-jsx/actions/workflows/rust.yml/badge.svg)](https://github.com/Desdaemon/swc-plugin-static-jsx/actions/workflows/rust.yml)
[![Node.js E2E](https://github.com/Desdaemon/swc-plugin-static-jsx/actions/workflows/e2e.yml/badge.svg)](https://github.com/Desdaemon/swc-plugin-static-jsx/actions/workflows/e2e.yml)

SWC plugin to transform JSX calls to static templates

## Install

```shell
npm i -D @swc/core swc-plugin-static-jsx
```

### From source

```shell
git clone https://github.com/Desdaemon/swc-plugin-static-jsx
cd swc-plugin-static-jsx
rustup target add wasm32-wasi
npm run dist
npm link
```

## Usage

```jsonc
// In .swcrc:
{
"jsc": {
"experimental": {
"plugins": [
// All config values are optional.
[
"swc-plugin-static-jsx",
{
// If an identifier is supplied, it should not be an ambient global. Can be null.
"template": "String.raw",
// If supplied, template will be imported as `import { template } from 'my-library'`
"importSource": "my-library",
"spread": "$$spread",
"child": "$$child",
"children": "$$children"
}
]
]
}
}
}
```

In your tsconfig.json, `compilerOptions.jsx` should be set to 'preserve'. You will also need to
provide your own JSX-related types under `namespace JSX`.

## Sample

```jsx
let unsanitized = 'alert("You\'ve been pwned!")';
// input
function MyComponent() {
return (


The quick brown fox jumps over the lazy dog.
{unsanitized}
{...children}

);
}

// output (approximate)
function MyComponent() {
return html`


The quick brown fox jumps over thelazydog. ${{
$$child: unsanitized,
}} ${{ $$children: children }}
`;
}
```

Sample implementation of `html`:

```tsx
function html(raw, ...children: Record[]) {
all: for (const child of children) {
if ("$$spread" in child) {
// ..
continue all;
}
if ("$$child" in child) {
// ..
continue all;
}
if ("$$children" in child) {
// ..
continue all;
}
// ..
}
}
```

## License

SPDX Identifier: `MIT OR Apache-2`