x0

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 严亮
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

x0

Document & develop React components without breaking a sweat

npm install -g @compositor/x0

Features

  • Zero-config
  • No plugins
  • Components over configuration
  • Use markdown, MDX, or React components
  • Automatic file system based routing
  • Completely customizable
  • Export static sites
  • Works as an isolated development environment

Read more about x0 in our blog post.

Getting Started

x0 renders a directory of React components, automatically handling routing based on filename.Create a docs folder and add an index.js file.

// index.js
import React from 'react'

export default class extends React.Component {
  render () {
    return (
      <h1>Hello</h1>
    )
  }
}

Start a development server by running:

x0 docs --open

To add more pages, add a new component for each route. For example, create an about page:

// about.js
import React from 'react'

export default props =>
  <h1>About</h1>

The about page should now render when navigating to http://localhost:8080/about.

Isolated development environment

x0 docs

Options:

-o --open       Open dev server in default browser
-p --port       Custom port for dev server
-t --template   Path to custom HTML template
--webpack       Path to custom webpack configuration
x0 docs -op 8080

Static Build

Export static HTML and client-side bundle

x0 build docs

Export static HTML without bundle

x0 build docs --static

Options

-d --out-dir    Output directory (default dist)
-s --static     Output static HTML without JS bundle
-t --template   Path to custom HTML template
--webpack       Path to custom webpack configuration

Fetching Data

Use the async getInitialProps static method to fetch data for static rendering.This method was inspired by Next.js.

const Index = props => (
  <h1>Hello {props.data}</h1>
)

Index.getInitialProps = async () => {
  const fetch = require('isomorphic-fetch')
  const res = await fetch('http://example.com/data')
  const data = await res.json()

  return { data }
}

Custom App

A custom App component can be provided by including an _app.js file.The App component uses the render props pattern to provide additional state and props to its child routes.

// example _app.js
import React from 'react'

export default class extends React.Component {
  state = {
    count: 0
  }

  update = fn => this.setState(fn)

  render () {
    const { render, routes } = this.props

    return render({
      ...this.state,
      decrement: () => this.update(s => ({ count: s.count - 1 })),
      increment: () => this.update(s => ({ count: s.count + 1 }))
    })
  }
}

Layouts

The App component can also be used to provide a common layout for all routes.

// example _app.js
import React from 'react'
import Nav from '../components/Nav'
import Header from '../components/Header'
import Footer from '../components/Footer'

export default class extends React.Component {
  render () {
    const {
      location,
      render,
      routes
    } = this.props

    const route = routes.find(route => route.path === location.pathname)

    return (
      <React.Fragment>
        <Nav />
        <Header
          route={route}
        />
        {render()}
        <Footer />
      </React.Fragment>
    )
  }
}

CSS-in-JS

x0 supports server-side rendering for styled-components and emotion with zero configuration.

Styled Components

To enable CSS rendering for static export, ensure that styled-components is installed as a dependency in your package.json

"dependencies": {
  "styled-components": "^3.2.6"
}

Emotion

Ensure emotion is installed as a dependency in your package.json

"dependencies": {
  "emotion": "^9.1.3"
}

Configuration

Default options can be set in the x0 field in package.json.

"x0": {
  "static": true,
  "outDir": "site",
  "title": "Hello",
}

Head content

Head elements such as <title>, <meta>, and <style> can be configured with the x0 field in package.json.

"x0": {
  "title": "My Site",
  "meta": [
    { "name": "twitter:card", "content": "summary" },
    { "name": "twitter:image", "content": "kitten.png" }
  ],
  "links": [
    {
      "rel": "stylesheet",
      "href": "https://fonts.googleapis.com/css?family=Roboto"
    }
  ]
}

Custom HTML Template

A custom HTML template can be passed as the template option.

"x0": {
  "template": "./html.js"
}
// example template
module.exports = ({
  html,
  css,
  scripts,
  title,
  meta = [],
  links = [],
  static: isStatic
}) => `<!DOCTYPE html>
<head>
  <title>{title}</title>
  ${css}
</head>
<div id=root>${html}</div>
${scripts}
`

Routing

x0 creates routes based on the file system, using react-router.To set the base URL for static builds, use the basename option.

"x0": {
  "basename": "/my-site"
}

Links

To link between different components, install react-router-dom and use the Link component.

npm i react-router-dom
import React from 'react'
import { Link } from 'react-router-dom'

export default () => (
  <div>
    <h1>Home</h1>
    <nav>
      <Link to='/'>Home</Link>
      <Link to='/about'>About</Link>
    </nav>
  </div>
)

JSX Format

x0 includes support for the Compositor JSX file format.

---
title: Hello
---
import { Box, Heading } from 'rebass'

<Box px={2} py={4}>
  <Heading>
    {frontMatter.title}
  </Heading>
</Box>

MDX Format

x0 includes support for the MDX file format.

import { Box } from 'rebass'

# Hello MDX

<Box p={4} bg='tomato'>
  Beep Boop
</Box>

webpack

Webpack configuration files named webpack.config.js will automatically be merged with the built-in configuration, using webpack-merge.To use a custom filename, pass the file path to the --webpack flag.

// webpack.config.js example
module.exports = {
  module: {
    rules: [
      { test: /\.txt$/, loader: 'raw-loader' }
    ]
  }
}

See the example.

Related


Made by Compositor|MIT License

  • RISC-V有32个通用寄存器,其中第1个寄存器x0硬编码为0,即读出来总是0,写进去总是被丢弃。x0为RISC-V指令集的简化可贡献不少啊。 mingdu.zheng at gmail dot com http://blog.csdn.net/zoomdy/article/details/79343785 搞过Linux的都知道Linux有两个特殊的设备:/dev/zero和/dev/null。

  • vncserver By default a logged in user has a desktop provided by X Server on display 0. A user can share their desktop using the TigerVNC server x0vncserver.

  • 今日查一下nginx日志,发现有数据乱码了?!如下所示: \x222\x22,\x0A 使用python来处理这个乱码 line = '\x222\x22,\x0A' bytes(line, 'UTF-8').decode('unicode_escape'); 输出 "2",\n

 相关资料
  • 问题内容: 我想使用Spring cache @Cacheable来管理器缓存。真正的缓存是redis。 我的代码是这样的: 在我有职位要求后 本地主机:8080 / post?key = key&value = value Redis服务器出现一个奇怪的密钥 spring缓存 如何设置@Cacheable的StringRedisTemplate之类的序列化器默认值: 我的application.

  • 问题内容: 我在for循环中解组json时遇到此错误。第一次通过循环可以很好地进行编组,但是在下一次迭代中,我会收到此错误。 我是golang的新手,并且此错误消息尚不清楚。有人可以解释在什么情况下会发生此错误,以及如何避免该错误。 问题答案: 谢谢你回答我的问题。 此错误是由于错误的json导致的,在此之前没有更多的故事了, 我有一个json.RawMessage类型的字段 详细信息 ,我有一个

  • 问题内容: 我在Python 3.4 shell中使用转义反斜杠进行了一些实验,发现有些奇怪的东西。 如您在上面的代码中看到的,我将变量字符串定义为。但是,当我进入控制台时,它打印而不是打印。为什么会发生这种情况,它的作用是什么? 问题答案: 在Python字符串文字中,字符开始转义序列。转换为换行符,制表符等。十六进制序列使您可以生成具有十六进制值的代码点,生成具有4位十六进制值的代码点,并生成

  • 我有一个概率问题: 在任何一天,埃里克要么是快乐的(C),要么是马马虎虎的(S),要么是闷闷不乐的(G)。 如果他今天很高兴,那么他明天将是C、S或G,相应的概率为0.5、0.3、0.2。 如果他今天感觉一般,那么他明天会是C,S或G,概率是0.3,0.4,0.3。 如果他今天闷闷不乐,那么他明天会是C,S或G,概率是0.2,0.2,0.6。 我在R中生成了50,000个独立的伪随机数(均匀的),

  • 我想使用Spring cache@Cacheable来管理缓存。真正的缓存是redis。 我的代码是这样的: 在我收到发帖请求后 本地主机:8080/帖子?钥匙 redis服务器似乎是一把奇怪的钥匙 Spring缓存 奇怪的redis钥匙和spring数据绝地武士 如何设置@Cacheable的序列化程序,如StringRedisTemplate默认值: 我的application.propert

  • 在尝试向列添加一些内容后,我得到了 问题出在哪里?