Git Product home page Git Product logo

biti's Introduction

repo banner

npm i biti -g


⚠️ this repo is deprecated in favor of @rola/static and the rola project.


Features

  • easy static page generation
  • intelligent file watching
  • convenient CLI
  • easy Node API

Usage

biti watch pages/ static/

Getting started

biti is pretty simple. It operates on a single directory of pages, which each define and export properties and methods that biti uses to render the page.

For the examples here, we'll use /pages as our pages directory, but you could call it anything.

Configuring a page

Each page requires the following exports:

  • pathname - string - the path where you'd like the page to appear
  • view - function - a function that returns a React component

The pathname property will be passed to the view component.

An simple page might look like this:

import React from 'react'

export const pathname = '/about'

export function view ({ pathname }) {
  return (
    <>
      <h1>About Us</h1>
      <p>...</p>
    </>
  )
}

Static data

Pages can also export a state object, which will also be passed to the view function when rendering.

import React from 'react'

export const pathname = '/about'

export const state = {
  title: 'About Us',
  description: '...'
}

export function view ({ state, pathname }) {
  return (
    <>
      <h1>{state.title}</h1>
      <p>{state.description}</p>
    </>
  )
}

Loading data

Routes that require data or those that need dynamic properties can define a config function that returns a page config containing the same properties listed above.

These values will be deeply merged with whatever static exports were provided.

import React from 'react'
import { getAboutPage } from './lib/api.js'

export const pathname = '/about'

export const state = {
  title: 'About Us',
  team: [
    'Eric'
  ]
}

export function config () {
  return getAboutPage()
    .then(res => {
      return {
        state: {
          team: res.team
        }
      }
    })
}

export function view ({ state, pathname }) {
  return (
    <>
      <h1>{state.title}</h1>

      <h2>Team</h2>

      {state.team.map(name => (
        <p key={name}>{name}</p>
      ))}
    </>
  )
}

Generating pages from loaded data

For generative pages and pagination, the config function can also return an array of page configs. Each of these configs should define its own pathname, so that each page is rendered separately.

The following example will generate a page for each post returned from getBlogPosts:

import React from 'react'
import { getBlogPosts } from './lib/api.js'

export function config () {
  return getBlogPosts()
    .then(posts => {
      return posts.map(post => ({
        pathname: `/posts/${post.slug}`,
        state: {
          title: post.title,
          content: post.content
        }
      }))
    })
}

export function view ({ state, pathname }) {
  return (
    <>
      <h1>{state.title}</h1>

      <article>
        {state.content}
      </article>
    </>
  )
}

Configuration

biti supports minimal configuration, and otherwise falls back to smart defaults. To define a config for all rendering tasks, you can create a biti.config.js file.

biti supports the following properties on the config file:

  • env - object - properties on this object will be attached to process.env, as well as defined globally within the compilation.
  • alias - object - module import aliases

Example:

module.exports = {
  env: {
    API_KEY: 'abcdefg'
  },
  alias: {
    components: './src/components'
  }
}

Default config

By default, biti defines a single alias @ that points to process.cwd(). You can use it throughout your templates like this:

import Component from '@/src/components/Component.js'

CLI

biti only has two commands: render and watch.

Both follow the same pattern:

biti <command> <src> <dest>

For example:

biti render /pages /static

These commands also accept globs as the src property, allowing you to specify individual pages or directories.

biti render /pages/about-us.js /static
biti render /pages/*.js /static
biti render /pages/marketing-site/*.js /static
biti render /pages/**/*.js /static

API

Using biti programmatically is virtually the same as using the CLI, only you'll need to pass your configuration object manually.

const biti = require('biti')

const config = {
  env: { ... },
  alias: { ... }
}

const app = biti(config)

Both render and watch have the following signature:

app.render(src, dest)

render

Renders all pages from src dest.

app.render('/src', '/static')

watch

Watches all pages in src and renders to dest on file change.

app.watch('/src', '/static')

API Events

A biti instance emits a few helpful events as well.

render

After rendering a single page.

app.on('render', page => {})

rendered

After rendering all pages. On watch this is called after every change has been compiled and rendered.

app.on('rendered', pages => {})

error

app.on('error', error => {})

License

MIT License © Eric Bailey

biti's People

Contributors

estrattonbailey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

biti's Issues

plugins

Will need to integrate with spitball in order to support things like CSS-in-JS.

named 404.html support

At the moment if you export a pathname /404 it'll generate /static/404/index.html, but many static hosts – like Netlify – expect a 404.html.

Also tracked in hypr.

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.