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

swc-plugin-strip-components

v0.3.9

Published

Simple swc plugin to nuke components added to props

Downloads

3

Readme

swc-plugin-strip-components

This swc plugin can be used to do one (or both) of 2 things

1. Lobotomize React 'use client' components in your code

Sometimes, you may have to import files which import client components on the server. This happens a lot of payload, where the payload config contains both server-side code, as well as client component imports.

This swc plugin automatically deletes the contents of files marked with 'use client' and keeps ONLY functions / variables which are exported. Additionally, those are nullified.

That way, code which relies on those imports will not break, and those client components are effectively "disabled".

Example:

Input

'use client'

import './index.scss' // Should be removed
export { MyFn as MyFnOtherFile } from "../use client/ClientComponentInput"; // should be kept, due to it being exported
import { MyFn as MyFnOtherFile2 } from "../use client/ClientComponentOutput"; // should be kept due to re-export below
export { MyFnOtherFile2 }  // should be kept, due to it being exported
import { MyFn2 as MyFn2FromAnotherFile } from "../use client/ClientComponentInput"; // should be removed. Not exported and not used anywhere


import { lazy } from 'react' // Should be removed


const RichTextEditor = lazy(() =>
    // @ts-ignore
    import('../use client/ClientComponentOutput').then((module) => ({ default: module.MyFn })),
) // Should be removed completely.


const MyFn = () => {
    return <div>MyFn</div>
} // Should be kept due to exported below

const MyFn2 = () => {
    return <div>MyFn2</div>
} // Should be kept due to exported below

export function MyFn4 () {
    return <div>MyFn3</div>
} // Should be kept due to exported directly


function MyFn5 () {
    return <div>MyFn3</div>
} // Should be kept due to exported below

function MyFn6 () { // Removed
    return <div>MyFn3</div>
} // Should be removed completely. Not exported and not exported below

export const someVariable = 'someVariable' // Should be kept. Exported directly

const someVariable2 = 'someVariable2' // Should be kept. Exported below

const someVariable3 = 'someVariable3' // Should be removed


export { MyFn, MyFn2, MyFn5, someVariable2 }

Output of this plugin:

'use client';
export { MyFn as MyFnOtherFile } from "../use client/ClientComponentInput"; // should be kept, due to it being exported
import { MyFn as MyFnOtherFile2 } from "../use client/ClientComponentOutput"; // should be kept due to re-export below
export { MyFnOtherFile2 }; // should be kept, due to it being exported
const MyFn = ()=>null// Should be kept due to exported below
;
const MyFn2 = ()=>null// Should be kept due to exported below
;
export function MyFn4() {
    return null;
} // Should be kept due to exported directly
function MyFn5() {
    return null;
} // Should be kept due to exported below
export const someVariable = null// Should be kept. Exported directly
;
const someVariable2 = null// Should be kept. Exported below
;
export { MyFn, MyFn2, MyFn5, someVariable2 };

2. Nullify arguments to a specified function

This can be useful if this plugin is conditionally enabled, and you only want components (or any code, really) to be available on the client-only (so, if this swc plugin is not enabled).

If the identifier in your swc config is set to Component, this will be the example input / output:

Input:

type Component = <C>(
    C: C,
) => C | null

const Component: Component = (c) => c as any

{
    someProp: Component(MyComponent)
}

Output:

type Component = <C>(
    C: C,
) => C | null

const Component: Component = (c) => c as any

{
    someProp: Component(null)
}

Installation

pnpm add swc-plugin-strip-components

then add it to your swc config:

const modifiedConfig = {
        ...swcRegisterConfig,
        swc: {
            ...{
                ...swcRegisterConfig?.swc ?? {}
            },
            jsc: {
                ...{
                    ...swcRegisterConfig?.swc?.jsc ?? {}
                },
                experimental: {
                    ...{
                        ...swcRegisterConfig?.swc?.jsc?.experimental ?? {}
                    },
                    plugins: [
                        ...swcRegisterConfig?.swc?.jsc?.experimental?.plugins ?? [],
                        [
                            'swc-plugin-strip-components',
                            {
                                identifier: 'Component', // the name of the function whose props should be nullified
                                lobotomize_use_client_files: true
                            }
                        ]
                    ]
                }
            }
        }
    };