A plugin for transpiling pug templates to jsx
npm install babel-plugin-transform-react-pugUse Pug templates to write react components.
  
babel-plugin-transform-react-pug is a plugin for babel which transpiles pug syntax within template literals to jsx.
Write your components this way:
``jsx
export const ReactComponent = props => pug
.wrapper
if props.shouldShowGreeting
p.greeting Hello World!
button(onClick=props.notify) Click Me`
And it will be transpiled into:
` Hello World!jsx`
export const ReactComponent = props => (
{props.shouldShowGreeting ? (
) : null}
)
* Usage
* Syntax
* Eslint integration
* CSS Modules
* Install
* Configuration
* create-react-app
* React Native
* How it works
* Limitations
* FAQ
* Can I import template from other files?
* How to get syntax highlighting in IDE (or text editors)?
* License
Full information of the syntax you can find in official documentation: pugjs.org.
#### Basic example
`jsx //- const Component = props => (
const Component = props => pug
div //-
You can set bigger amount ;)
;
`#### How to pass functions and other primitives
`jsx
const Component = props => pug //- const Component = props => (;
`#### Define local variables and use javascript in attributes
`jsx
const Component = props => pug //- const Component = props => (Variable is {format(props.no)}
;
`#### Interpolation
If you'd prefer to use interpolation, you can. This is possible by using
${} within your template.`jsx
const Component = props => pugli(key=${index}) ${item})};
`$3
Install eslint-plugin-react-pug if you use eslint-plugin-react.
$3
Whether you use babel plugin to turn on CSS Modules specifically for JSX (e.g. babel-plugin-react-css-modules) or use webpack loader for that to transform styles into key-value object, it's possible to use it with pug.
* With babel-plugin-react-css-modules you need to set
classAttribute option to styleName value and that's it.
`rc
{
"plugins": [
["transform-react-pug", {
"classAttribute": "styleName"
}]
]
}
`
`jsx
import './styles.css' // .hello{color:red} const withCorrectStyles = pug
`* With webpack loader or other approaches which transform styles into object
`jsx
import classes from './styles.css' // .hello{color:green} const withCorrectStyles = pug
`classAttribute option to styleName value and adding babel-plugin-transform-jsx-css-modules
`rc
{
"plugins": [
["transform-react-pug", {
"classAttribute": "styleName"
}],
"transform-jsx-css-modules"
]
}
`
`jsx
import './styles.css' // .hello{color:green} const withCorrectStyles = pug
`Install
1. Install via yarn or npm
`
yarn add --dev babel-plugin-transform-react-pug
`
`
npm install --save-dev babel-plugin-transform-react-pug
`2. Add to babel configuration before transpiling jsx (usually in
.babelrc)
`
{
"plugins": [
"transform-react-pug",
"transform-react-jsx"
]
}
`3. Now all your templates written with pug are understood by react and browsers.
$3
classAttribute | String | className | Attribute name which considered by PUG as "class"####
classAttributeDefault:
`
pugp.one=>
`With "styleName" as value:
`
pugp.one=>
`$3
Integrating with [create-react-app][link to cra] is tricky because it does not allow you to modify babel configuration. There are two documented possibilities:
* [eject][link to cra eject]
That is easy, you will get
.babelrc file in your root directory, just add transform-react-pug before transform-react-jsx there.* [react-app-rewired][link to rewired cra]
Go through official instruction to rewire your application. Then modify your
config-overrides.js:
`diff
+ const {injectBabelPlugin} = require('react-app-rewired');
module.exports = function override(config, env) {
- //do stuff with the webpack config...
+ config = injectBabelPlugin('transform-react-pug', config);
return config;
}
`[link to cra]: https://github.com/facebook/create-react-app/
[link to cra eject]: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject
[link to rewired cra]: https://github.com/timarney/react-app-rewired#1-injectbabelplugin
$3
Just add this plugin to the list in
.babelrc file.`diff
{
- "presets": ["react-native"]
+ "presets": ["react-native"],
+ "plugins": ["transform-react-pug"]
}
`_We don't need
transform-react-jsx here because it's coming with react-native preset._How it works
_Coming soon..._
Limitations
* We can't use dots in component names because pugjs treats everything after dot as a className. For example,
React.Fragment becomes , not A nice workaround is made by babel-plugin-transform-jsx-classname-components. Just add it to
.babelrc:
`rc
{
"plugins": [
["transform-jsx-classname-components", {
"objects": ["React"]
}]
]
}
`* We don't support html language in pug templates. This is different than what Pug promises.
However, you can still use tag interpolation:
`pug
p Good #[strong Morning]
`FAQ
$3
The short answer is no and we are not going to implement that in near future. Take a look at initial request with small explanation (#15).
$3
* WebStorm
* Atom
* Visual Studio Code
#### WebStorm
1. Open settings
2. "Editor" -> "Language Injections"
3. Click on Add new "Generic Js" injection
* Name:
Pug In Template Literals (JavaScript)
* ID: Vue (Vue.js template) (current version of pug plugin is created in HTML scope, so we use workaround here)
* Prefix:
* Suffix:
* Places Patterns: + taggedString("pug")4. Click "OK" and "Apply"
#### Atom
1. Install language-babel and language-pug-jade
_I suggest language-pug-jade because it works better for me. But there are more approaches for building pugjs grammar: language-pug and atom-pug, and you can try them too._
2. Open settings of language-babel in atom
3. Find the field under "JavaScript Tagged Template Literal Grammar Extensions"
4. Enter:
pug:source.pug`More details: gandm/language-babel#javascript-tagged-template-literal-grammar-extensions
5. Restart the atom
#### Visual Studio Code
1. Open settings of extensions
2. Search "vscode-react-pug" by the search field
3. Click "Install" and "Reload"
4. If you use any grammar other than default one (e.g. Babel JavaScript which is quite popular), you might need to add supporting of Atom's Grammar (Microsoft/vscode-js-atom-grammar).
Check out the history beyond that: kaminaly/vscode-react-pug#4.
MIT