All Projects → orenelbaum → babel-plugin-solid-undestructure

orenelbaum / babel-plugin-solid-undestructure

Licence: other
A Babel plugin for SolidJS that allows you to destructure component props without losing reactivity.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-solid-undestructure

djburger
Framework for safe and maintainable web-projects.
Stars: ✭ 75 (+66.67%)
Mutual labels:  solid
NiceDemo
iOS project, built on improved MVP architecture using Coordinator pattern for routing 😎
Stars: ✭ 54 (+20%)
Mutual labels:  solid
babel-plugin-feature-flags
A babel transform for managing feature flags
Stars: ✭ 57 (+26.67%)
Mutual labels:  babel-plugin
babel-plugin-hyperscript-to-jsx
This plugin transforms react-hyperscript into JSX. Intended to be used as codemod.
Stars: ✭ 20 (-55.56%)
Mutual labels:  babel-plugin
solid-client-js
Library for accessing data and managing permissions on data stored in a Solid Pod
Stars: ✭ 201 (+346.67%)
Mutual labels:  solid
solid-ui-react
React SDK using @inrupt/solid-client
Stars: ✭ 82 (+82.22%)
Mutual labels:  solid
solid-javascript
📗 5 examples that describe usage of SOLID in JavaScript.
Stars: ✭ 23 (-48.89%)
Mutual labels:  solid
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (+1004.44%)
Mutual labels:  solid
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-66.67%)
Mutual labels:  babel-plugin
refactoring-for-testability-cpp
Hard-to-test patterns in C++ and how to refactor them
Stars: ✭ 40 (-11.11%)
Mutual labels:  solid
nornj
More exciting JS/JSX based on Template Engine, support control flow tags, custom directives, two-way binding, filters and custom operators.
Stars: ✭ 97 (+115.56%)
Mutual labels:  babel-plugin
penv.macro
A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.
Stars: ✭ 73 (+62.22%)
Mutual labels:  babel-plugin
element
Fast and simple custom elements.
Stars: ✭ 65 (+44.44%)
Mutual labels:  solid
babel-plugin-rewire-exports
Babel plugin for stubbing [ES6, ES2015] module exports
Stars: ✭ 62 (+37.78%)
Mutual labels:  babel-plugin
specification
RDF vocabulary and specification
Stars: ✭ 21 (-53.33%)
Mutual labels:  solid
shepherd
Shepherd is our 2nd prototype to showcase how a truly decentralised social network can be based on SOLID.
Stars: ✭ 14 (-68.89%)
Mutual labels:  solid
previewjs
Preview UI components in your IDE instantly
Stars: ✭ 1,331 (+2857.78%)
Mutual labels:  solid
archimedes-jvm
Archimedes's implementation for the Java Virtual Machine (JVM)
Stars: ✭ 24 (-46.67%)
Mutual labels:  solid
babel-plugin-solid-labels
Simple, reactive labels for SolidJS
Stars: ✭ 127 (+182.22%)
Mutual labels:  babel-plugin
babel-plugin-transform-for-of-as-array
Transform all for-of loops into the equivalent array for loop
Stars: ✭ 14 (-68.89%)
Mutual labels:  babel-plugin

babel-plugin-solid-undestructure

This Babel plugin allows you to destructure your props in your Solid components without losing reactivity.

The plugin will "un-destructure" your props at build time, so the code you pass into the Solid compiler will not have destructured props at runtime. Instead the props will be accessed the normal way with props.someProp.

Note
This plugin is compatible with Solid 1.5

Usage with examples:

// Use the `Component` type to mark components that will be transformed by the plugin
import { Component } from 'solid-js'
const MyComp: Component<...> = ({ a, b, c }) => {a; b; c;};

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓  The above code will compile to
import { Component } from 'solid-js'
const MyComp: Component<...> = props => {props.a; props.b; props.c;}



// You can use a compile time function instead of using the `Component` type (needed for vanilla JS)
import { component } from 'babel-plugin-solid-undestructure'
const MyComp = component(({ a, b, c }) => {a; b; c;})

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
const MyComp = props => {props.a; props.b; props.c;}



// Default props using `mergeProps`
import { Component } from 'solid-js'
const MyComp: Component<...> = (
  { a = 1, b = 2, c = 3 } = defaultProps
) => {a; b; c;}

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import { Component, mergeProps } from 'solid-js'
const MyComp: Component<...> = props => {
  props = mergeProps(defaultProps, { a: 1, b: 2, c: 3 }, props);
  props.a; props.b; props.c;
}


// Rename props
import { Component } from 'solid-js'
const MyComp: Component<...> = ({ a: d, b: e, c: f }) => {d; e; f;}

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import { Component, mergeProps } from 'solid-js'
const MyComp: Component<...> = props => {props.a; props.b; props.c;}



// Rest element destructuring using `splitProps`
import { Component } from 'solid-js'
const MyComp: Component<...> = ({ a, b, c, ...other }) => {a; b; c; other;}

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import { Component, splitProps } from 'solid-js'
const MyComp: Component<...> = props => {
  let other;
  [props, other] = splitProps(props, ["a", "b", "c"]);
  props.a; props.b; props.c; other;
}

See also undestructure-example (or Open in Stackblitz).

Warning
This plugin doesn't have any known bugs at the moment but it's still not ready for production use.

Component type annotation

When this option is enabled (it is enabled by default), the plugin transforms all arrow function components which are part of a variable declaration with a Component type annotation.

The type annotation must be a direct reference to the Solid Component type import, or an annotation of the form Solid.Component where Solid is a reference to the default import of Solid.

In both cases you can use the 'import as' syntax.

Examples:

import { Component } from 'solid-js'

const MyComponent: Component = // ...
import Solid from 'solid-js'

const MyComponent: Solid.Component = // ...
import { Component as ComponentAlias } from 'solid-js'

const MyComponent: ComponentAlias = // ...

This example won't work:

import { Component } from 'solid-js'

type ComponentAlias = Component

const MyComponent: ComponentAlias = // ...

In this last example, MyComponent won't be transformed.

Compile time function annotation (needed for vanilla JS)

This option can be used if you are using vanilla JS or you don't want to rely on types to manipulate runtime behavior like the type annotation does. When this option is enabled (it is enabled by default), the plugin transforms all component that are wrapped in the component compile-time function provided by this plugin.

The component compile-time function must be a direct reference to the component named export from babel-plugin-solid-undestructure

You can also use the 'import as' syntax.

Examples:

import { component } from 'babel-plugin-solid-undestructure'

const MyComponent = component(/* your component goes here. */)
import { component as c } from 'babel-plugin-solid-undestructure'

const MyComponent = c(/* your component goes here. */)

This example won't work:

import { component } from 'babel-plugin-solid-undestructure'

const c = component

const MyComponent = c(/* your component goes here. */)

In this last example, MyComponent won't be transformed.

Configuring Vite

Install the plugin with

npm i -D babel-plugin-solid-undestructure

In your Vite config, import undestructurePlugin from babel-plugin-solid-undestructure

import { undestructurePlugin } from "babel-plugin-solid-undestructure"

TS with Type annotation support

If your'e working with TypeScript code and you want to use the Component type annotation, add this to the top of the plugin list in your Vite config:

...undestructurePlugin("ts")

With this configuration you can use both the Component type and the component compile time function to annotate components.

TS or vanilla JS, no type annotation support

In your Vite config, find the your vite-plugin-solid initialization (in the default Solid template it will be imported as solidPlugin).

The first argument this initialization function takes, is the options object.

Add this field to the initializer options:

babel: {
	plugins: [undestructurePlugin("vanilla-js")]
} 

With this configuration you can use both the Component type and the component compile time function to annotate components.

Features under consideration

  • A pragma to annotate components
  • Add option to automatically apply the transformation to every function annotated with a type with a Component suffix
  • Nested component support
  • Nested destructuring
  • Destructure CTF (probably not but maybe)
  • Add option to automatically apply the transformation to every function assigned to an identifier that starts with a capital letter and (probably not but maybe)

If you want a feature to be added to this plugin, whether it's on this list or not, please open a feature request or tell me in this discussion.

Other cool plugins for Solid:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].