GithubHelp home page GithubHelp logo

ljbc1994 / babel-plugin-react-pug Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 112 KB

๐Ÿ• Use pug in your react components!

License: MIT License

JavaScript 100.00%
pug pug-templates babel-plugin interpolation jsx react-components babel react

babel-plugin-react-pug's Introduction

babel-plugin-react-pug

Chuck out JSX and use Pug!

Build Status Coverage Status dependencies Status Standard - JavaScript Style Guide NPM Downloads

A tiny, performant babel plugin that lets you use Pug over JSX, giving you a productive and readable alternative for defining React Component templates. In essence, the plugin transforms Pug templates into React function calls. Supports React Native!

This is not the official Pug plugin for converting Pug into JSX. Please see babel-plugin-transform-react-pug!

Example

In

class Profile extends React.Component {
    ...
    render() {
        return pug`
            #profile.profile__container
                h1.profile__name ${this.state.name}
        `
    }
}

Out

class Profile extends React.Component {
    ...
    render() {
        return React.createElement('div', { id: 'profile', className: 'profile__container' },
            React.createElement('h1', { className: 'profile__name' }, this.state.name));
    }
}

Installation

$ yarn add babel-plugin-react-pug --dev

Features

babel-plugin-react-pug supports Pug features that make the most sense when using Pug as a template language for React.

Attributes

Class

Using the pug class syntax will automatically rename the attribute to className - so you won't have to worry about this!

class Profile extends React.Component {
    ...
    render() {
        return pug`
            .profile__card
        `
    }
}

Other Attributes / Events

class Profile extends React.Component {
    ...
    render() {
        return pug`
            #profile__01.profile__card(title="Profile Title")
        `
    }
}

...or with interpolations:

class Profile extends React.Component {
    ...
    render() {
        return pug`
            #profile__01.profile__card(onClick=${ this.update })
        `
    }
}

Conditionals

class ProfileList extends React.Component {
    ...
    render() {
        return this.state.profiles.length
            ? pug`ul#profile__list Your list of profiles.`
            : pug`p.profile__error An error has occurred.`
    }
}

Loops

class ProfileList extends React.Component {
    ...
    render() {
        return pug`
            ul#profile__list ${ this.state.profiles.map((item) => pug`li ${item.name}`) }
        `
    }
}

Components

To include components you don't need to use interpolation, just ensure that the component name is capitalised. For example:

class Profile extends React.Component {
    ...
    render() {
        return pug`
            ProfileCard(cardImage=${ this.state.imgSrc })
        `
    }
}

Include

You can include pug templates into your components, for example say you have tpls/profile-footer.pug:

.profile__footer
    .profile__footer__img
        img(src="http://placehold.it/200x200")

...now you can include the file in the component:

class Profile extends React.Component {
    ...
    render() {
        return pug`
            .profile__container
                h1.profile__title ${ this.state.title }
                .profile__body
                    h2.profile__subtitle ${ this.state.subtitle }
                include ./tpls/profile-footer.pug
        `
    }
}

Extends

You can harness the awesome power of Pug's extends to have component template inheritance!

For example, you could specify a base component template (tpls/base-profile.pug):

.profile__container
    .profile__header
    block content
.profile__footer
    h3 This is the footer!
    block footer
    p This is the sub footer text!

...now reference this in the component:

class Profile extends React.Component {
    ...
    render() {
        return pug`
            extends ./tpls/base-profile.pug
            block content
                h2.profile__title ${ this.state.title }
            block footer
                ul.profile__links ${ this.state.links.map((link) => pug`li.link ${ link }`) } 
        `
    }
}

Block append / prepend

You can also use append and prepend blocks in your React components.

For example, if you have the following base component template (tpls/base-profile.pug):

.profile__container
    block content
        h1.profile__title Profile
.profile__footer
    h3 This is the footer!
    block footer
    p This is the sub footer text!

...now reference this in the component, with the added keyword append to the block:

class Profile extends React.Component {
    ...
    render() {
        return pug`
            extends ./tpls/base-profile.pug
            block append content
                h2.profile__name ${ this.state.name }
            block footer
                ul.profile__links ${ this.state.links.map((link) => pug`li.link ${ link }`) } 
        `
    }
}

Usage

Via .babelrc

{
    "plugins": ["react-pug"]
}

Via CLI

$ babel --plugins react-pug index.js

Via Node API

require('babel-core').transform('code', {
    plugins: ['react-pug']
});

React Native

Just install babel-plugin-react-pug in your React Native project, add react-pug to your .babelrc and bam!

{
    "presets": ["react-native"], 
    "plugins": ["react-pug"]
}

Issues and Potential Features

If you have any issues or bugs concerning babel-plugin-react-pug, please do not hesitate to raise an issue!

Furthermore, if there are any features in Pug that you feel would be awesome to have - please raise an issue and I'll get back to you!

Contributions

Any sort of contribution is welcome, just follow these steps:

  1. Fork the repo
  2. Create a feature branch git checkout -b new-feature
  3. Ensure the code meets the standard code style - just run npm run static-test
  4. Write a fixture test
  5. Commit and push your changes
  6. Submit a pull request!

Licence

MIT

babel-plugin-react-pug's People

Contributors

davidmroth avatar ljbc1994 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

davidmroth

babel-plugin-react-pug's Issues

How to add child React elements?

How can we include a react element as a child?

The trivial example given below fails to compile.

const Div = (props) => {
  return pug`
    div
      ${ props.children }
  `;
};

Similar constructs come up often with props.children and other child elements that come from a Javascript variable.

TypeScript support?

I'm migrating to TypeScript and I'd love to continue using this fantastic plugin.

I currently get errors when doing so,

error TS2304: Cannot find name 'pug'.

and I'm not really sure how to proceed with creating typings or otherwise.

Any help appreciated.

Differences from the official babel-plugin-transform-react-pug

Hi, I maintain pug, and released https://github.com/pugjs/babel-plugin-transform-react-pug quite a while ago. Unfortunately, I haven't had time to actually document the usage properly, but essentially you just add the babel-plugin-transform-react-pug and babel-plugin-transform-react-jsx to your babelrc file, then it will behave pretty similarly to this module.

Would you mind having a look and seeing if you could contribute back to the official plugin, rather than starting a separate project?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.