npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-solid-undestructure

v1.1.0

Published

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

Downloads

38

Readme

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.6 and is likely to work with every version of Solid 1.x.

Usage with examples:

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

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



// Also works with the `ParentComponent` type
import type { ParentComponent } from 'solid-js'
const MyComp: ParentComponent<...> = ({ a, b, c }) => {a; b; c;};

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import type { ParentComponent } from 'solid-js'
const MyComp: ParentComponent<...> = 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 'undestructure-macros'
const MyComp = component(({ a, b, c }) => {a; b; c;})

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



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

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import { type 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 type { Component } from 'solid-js'
const MyComp: Component<...> = ({ a: d, b: e, c: f }) => {d; e; f;}

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



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

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import { type 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;
}



// You can nest components
import type { Component } from 'solid-js'
const Parent: Component<...> = ({ a, b }) => {
  const Child: Component<...> = ({ c, d }) => {
    a; b; c; d;
  }
}

//  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
import type { Component } from 'solid-js'
const Parent: Component<...> = props1 => {
  const Child: Component<...> = props2 => {
    props1.a; props1.b; props2.c; props2.d;
  }
}

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

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 type { Component } from 'solid-js'

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

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

const MyComponent: ComponentAlias = // ...

This example won't work:

import type { 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 undestructure-macros

You can also use the 'import as' syntax.

Examples:

import { component } from 'undestructure-macros'

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

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

This example won't work:

import { component } from 'undestructure-macros'

const c = component

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

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

Installation and Configuring Vite

Install the plugin and the macro placeholder package with

npm i -D babel-plugin-solid-undestructure undestructure-macros

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.

Roadmap

  • Support for ParentComponent type annotation
  • A pragma to annotate components

Features Under Consideration

  • Nested destructuring
  • Destructure CTF (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:

  • https://github.com/orenelbaum/babel-plugin-reactivars-solid - A Svelte-like "reactive variables" plugin for Solid that lets you pass reactive variables (getter + setter) around in a concise way (also made by me).
  • https://github.com/LXSMNSYC/babel-plugin-solid-labels - Solid labels is more of an all in one plugin. It has Svelte-like reactive variables, prop destructuring (like this plugin) and more.
  • https://github.com/LXSMNSYC/solid-sfc - An experimental SFC compiler for SolidJS.

Development

The project is written in TypeScript and I think that it's relatively well documented and written in a way that makes it easy to understand as long as you're familiar with Babel plugins. If you're not, Babel plugins are much more straight forward than they look, I recommend playing a bit with AST Explorer to get a feel for how they work, and you can also take a look at the Babel plugin handbook.

It's recommended to use pnpm for development.

Get started with:

pnpm i

You can test your changes either by adding a test or by using the playground. A test should be added for every new feature anyway, but you can use the playground if you don't want to start by writing a test, you can also skip writing a test altogether, in this case I'll add a test myself.

For working with the playground and the tests, if you're on VSCode I recommend installing the extension es6-string-javascript which will highlight JS code inside template literals using a pragma, this way:

/*javascript*/`code goes here`

The playground is a file called playground.cjs. To work with the playground just go to the file and edit the code in the src variable. Your can run the playground with:

pnpm run playground

Writing a test is a bit trickier since uvu doesn't support snapshots and I'm too lazy to set it up in a better way. So I won't document the process right now, I'll just mention that you can run the tests with:

pnpm run test