├── test ├── Root.jsx └── index.js ├── docs ├── card.png ├── Container.jsx ├── Root.jsx ├── Install.jsx ├── Pre.jsx ├── App.jsx ├── Flex.jsx ├── CarbonAd.jsx ├── GithubButton.jsx ├── Footer.jsx ├── TweetButton.jsx ├── data.json ├── GA.jsx ├── Header.jsx ├── card.svg ├── Head.jsx ├── Usage.jsx ├── Style.jsx └── index.html ├── index.js ├── lib ├── render.js └── get-components.js ├── package.json ├── LICENSE.md ├── bin └── ejsx.js └── README.md /test/Root.jsx: -------------------------------------------------------------------------------- 1 |
3 | Install
4 | {`npm install -g ejsx`}
5 |
9 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | const path = require('path') 3 | const build = require('..') 4 | 5 | const dir = path.join(__dirname, '.') 6 | 7 | const html = build(dir) 8 | 9 | console.log(html) 10 | 11 | -------------------------------------------------------------------------------- /docs/App.jsx: -------------------------------------------------------------------------------- 1 |
11 | -------------------------------------------------------------------------------- /docs/Flex.jsx: -------------------------------------------------------------------------------- 1 |
10 | -------------------------------------------------------------------------------- /docs/CarbonAd.jsx: -------------------------------------------------------------------------------- 1 |
8 | -------------------------------------------------------------------------------- /docs/GithubButton.jsx: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /docs/Footer.jsx: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | const getComponents = require('./lib/get-components') 3 | const render = require('./lib/render') 4 | 5 | const build = (dir, opts) => { 6 | const components = getComponents(dir, opts) 7 | 8 | const args = Object.assign({ 9 | components 10 | }, opts) 11 | 12 | const html = render(args) 13 | 14 | return html 15 | } 16 | 17 | module.exports = build 18 | 19 | -------------------------------------------------------------------------------- /docs/TweetButton.jsx: -------------------------------------------------------------------------------- 1 |
18 |
--------------------------------------------------------------------------------
/docs/data.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": " {props.description}{props.title}
3 |
2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /lib/render.js: -------------------------------------------------------------------------------- 1 | 2 | const vm = require('vm') 3 | const React = require('react') 4 | const { renderToStaticMarkup } = require('react-dom/server') 5 | 6 | module.exports = ({ 7 | components = '', 8 | rootComponentName = 'Root', 9 | entry, 10 | props, 11 | globals 12 | } = {}) => { 13 | 14 | const ctxObj = Object.assign({ 15 | React, 16 | renderToStaticMarkup, 17 | html: null, 18 | }, globals) 19 | 20 | entry = entry || (` 21 | const props = ${JSON.stringify(props)} 22 | 23 | html = renderToStaticMarkup( 24 | React.createElement(${rootComponentName}, props) 25 | )`) 26 | 27 | const src = [ 28 | components, 29 | entry 30 | ].join('\n') 31 | 32 | const script = new vm.Script(src) 33 | const ctx = new vm.createContext(ctxObj) 34 | 35 | script.runInContext(ctx) 36 | 37 | return '' + ctxObj.html 38 | } 39 | 40 | -------------------------------------------------------------------------------- /lib/get-components.js: -------------------------------------------------------------------------------- 1 | 2 | const fs = require('fs') 3 | const path = require('path') 4 | const babel = require('babel-core') 5 | 6 | module.exports = (dir, { 7 | extension = 'jsx' 8 | } = {}) => { 9 | 10 | const extRegex = new RegExp('\.' + extension + '$') 11 | 12 | const files = fs.readdirSync(dir) 13 | .filter(f => extRegex.test(f)) 14 | .map(f => { 15 | const contents = fs.readFileSync(path.join(dir, f), 'utf8') 16 | const [ name ] = f.split('.') 17 | 18 | return { 19 | name, 20 | contents 21 | } 22 | }) 23 | 24 | const comps = files.map(({ name, contents }) => { 25 | const src = `const ${name} = props => (${contents})` 26 | const { code } = babel.transform(src, { 27 | presets: [ 28 | 'react' 29 | ] 30 | }) 31 | 32 | return code 33 | }).join('\n') 34 | 35 | return comps 36 | } 37 | 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ejsx", 3 | "version": "1.0.0-b2", 4 | "description": "Experimental Node.js module and CLI to use JSX as a templating language for static HTML", 5 | "main": "index.js", 6 | "bin": { 7 | "ejsx": "./bin/ejsx.js" 8 | }, 9 | "scripts": { 10 | "build": "node ./bin/ejsx.js docs -p docs/data.json > docs/index.html", 11 | "start": "nodemon --exec 'npm run build' -e js,jsx", 12 | "help": "node ./bin/ejsx.js --help", 13 | "test": "node test" 14 | }, 15 | "keywords": [ 16 | "jsx", 17 | "static", 18 | "html", 19 | "template", 20 | "templating" 21 | ], 22 | "author": "Brent Jackson", 23 | "license": "MIT", 24 | "dependencies": { 25 | "babel-core": "^6.17.0", 26 | "babel-preset-react": "^6.16.0", 27 | "meow": "^3.7.0", 28 | "react": "^15.3.2", 29 | "react-dom": "^15.3.2" 30 | }, 31 | "devDependencies": { 32 | "nodemon": "^1.11.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | # The MIT License (MIT) 3 | Copyright (c) 2016 Brent Jackson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | 11 | -------------------------------------------------------------------------------- /bin/ejsx.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // const fs = require('fs') 4 | const path = require('path') 5 | const meow = require('meow') 6 | const build = require('..') 7 | 8 | // Options to add: 9 | // - entry/renderer 10 | // - globals 11 | // - file extension 12 | 13 | const cli = meow(` 14 | Usage 15 | $ ejsx
5 | Create a Root.jsx template. 6 | This file should not include any wrapping JavaScript. 7 |
8 |{`
9 | Hello
10 | `}
11 | 12 | Run the ejsx command on the folder that contains the Root.jsx file, and write the output to an HTML file. 13 |
14 |{`ejsx components > index.html`}
15 |
17 | To create reusable components, add files to the folder that begin with a capital letter and have the .jsx file extension.
18 | Components have access to the props passed in from their parent.
19 |
21 |{props.title}
22 | `} /> 23 |{` 24 |26 |25 | `} Passing in Data Props
27 |28 | To pass props to the Root component, create a JSON file and use the --props flag. 29 |
30 | 33 |ejsx components --props data.json > index.html34 |Loops
35 |36 | {props.items.map((item, i) => ( 37 |
44 | {props.foo % 2 ? Odd : Even}
45 | `} />
46 |
47 | {props.foo && Hidden when props.foo is falsy}
48 | `} />
49 | Pure JSX templates for rendering static HTML
npm install -g ejsx
Create a Root.jsx template. This file should not include any wrapping JavaScript.
<html> 61 | <h1>Hello</h1> 62 | </html>
Run the ejsx command on the folder that contains the Root.jsx file, and write the output to an HTML file.
ejsx components > index.html
To create reusable components, add files to the folder that begin with a capital letter and have the .jsx file extension. Components have access to the props passed in from their parent.
<header>
63 | <h1>{props.title}</h1>
64 | </header><html> 65 | <Header title='Hello' /> 66 | </html>
To pass props to the Root component, create a JSON file and use the --props flag.
{
67 | "title": "Hello"
68 | }ejsx components --props data.json > index.html
<ul>
69 | {props.items.map((item, i) => (
70 | <li key={i}>
71 | {item}
72 | </li>
73 | ))}
74 | </ul><div>
75 | {props.foo % 2 ? <span>Odd</span> : <span>Even</span>}
76 | </div><div>
77 | {props.foo && <div>Hidden when props.foo is falsy</div>}
78 | </div>