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

dust-components

v1.0.0

Published

Migrate your Dust.js codebase to pure components and jsx ( bit by bit )

Downloads

3

Readme

Dust Components

Migrate your Dust.js codebase to pure components and jsx ( bit by bit ).

Motivation

If you use Dust.js as your primary templating language and you have plans to migrate to React or at least to step closer to component-based approach, then this repo will probably be interesting for you. With dust-components it's possible to use pure React components in form of regular dust helpers.

Quick example

Lets go through some refactoring steps of an example template:

<div>
    <div class="container {containerClass}">
        <h1>{containerHeaderText}</h1>
        <div class="container-body">
            {#authors}
                <div class="author {itemClass}">
                    <div class="author-name">{name}</div>
                    <div class="author-surname">{surname}</div>
                    <button class="button button-vote">Like!</button>
                </div>
            {/authors}
        </div>
    </div>
</div>

At every step we will extract some part of a template markup into a component.

Step 1: Button

const cx = require('classnames');

function Button(props) {
    return (
        <button className={cx('button', props.className)}
                name={props.name}
                id={props.id}
                disabled={props.disabled}>{props.text}</button>
    );
}

module.exports = Button;

Now we can rewrite original template as follows:

<div>
    <div class="container {containerClass}">
        <h1>{containerHeaderText}</h1>
        <div class="container-body">
            {#authors}
                <div class="author {itemClass}">
                    <div class="author-name">{name}</div>
                    <div class="author-surname">{surname}</div>
                    {@Button className="button-vote" text="Like!"/}
                </div>
            {/authors}
        </div>
    </div>
</div>

Note: to be able to use @Button helper in template above, we had to import and register Button component first. See How to setup section for all instructions on doing that

Step 2: Author

const cx = require('classnames');
const Button = require('./Button');

function Author(props) {
    return (
        <div className={cx('author', props.className)}>
            <div className="author-name">{props.name}</div>
            <div className="author-surname">{props.surname}</div>
            <Button className="button-vote" text="Like!"/>
        </div>
    );
}

module.exports = Author;

Refactored template:

<div>
    <div class="container {containerClass}">
        <h1>{containerHeaderText}</h1>
        <div class="container-body">
            {#authors}
                {@Author className="{itemClass}" name="{name}" surname="{surname}"/}
            {/authors}
        </div>
    </div>
</div>

Note: notice that button's component could be used both in dust templates as a helper and in other components as a regular component.

Step 3: AuthorsList

const cx = require('classnames');
const Author = require('./Author');

function AuthorsList(props) {
    const authors = props.authors || [];    
    return (
        <div className="authors-list">
            {
                authors.map(function (author) {
                    return <Author className={cx(props.itemClass)} name={author.name} surname={author.surname}/>;
                })
            }
        </div>
    );
}

module.exports = AuthorsList;

Refactored template:

<div>
    <div class="container {containerClass}">
        <h1>{containerHeaderText}</h1>
        <div class="container-body">
            {@AuthorsList itemClass="{itemClass}" authors=authors/}
        </div>
    </div>
</div>

Step 4: Container

const cx = require('classnames');

function Container(props) {
    return (
        <div className={cx('container', props.className)}>
            <h1>{props.headerText}</h1>
            <div className="container-body">
                {props.children}        
            </div>
        </div>
    );
}

module.exports = Container;

Refactored template:

<div>
    {@Container className="{containerClass}" headerText="{containerHeaderText}"}
        {@AuthorsList itemClass="{itemClass}" authors=authors/}
    {/Container}
</div>

Refactoring result

  • Most part of existed code migrated to isolated components, compatible with React
  • Dust.js template slimmed down to it's minimum declarative form

How to setup

Install dust-component as project dependency

with npm:

npm i --save dust-components

with yarn:

yarn add dust-components

Build phase: set up component transformation

Before components can be used - they have to be transformed with custom pragma function:

{
    loader: 'babel-loader',
    options: {
    plugins: [
        ["transform-react-jsx", {
            "pragma": "dcNode"
        }]
    ]
},
    
// ...
    
plugins: [
    new webpack.ProvidePlugin({
        dcNode: ['dust-components', 'pragmaFunction']
    })
]

Runtime: register components as helpers

Components has to be registered as helpers in order to be used in Dust.js templates. To do that use HelperFactory that goes with this repo:

// Import components:
const Button = require('components/Button');
const Author = require('components/Author');
const AuthorsList = require('components/AuthorsList');
const Container = require('components/Container');

// Setup helper factory:
const dust = require('dustjs-linkedin');
const createHelper = require('dust-components').HelperFactory(dust);

// Register components:
dust.helpers['Button'] = createHelper(Button);
dust.helpers['Author'] = createHelper(Author);
dust.helpers['AuthorsList'] = createHelper(AuthorsList);
dust.helpers['Container'] = createHelper(Container);

Note: probably it's a good idea to generate such registration code on build phase

Tests

To run project tests:

yarn run test

Check project tests directory to see migration examples from this README, other usecases or to add more tests.

Implementation notes

TODO: