Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Created August 27, 2018 04:42
Show Gist options
  • Save ThaddeusJiang/f4cca7ea96ff4911cd300d3c973205d6 to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/f4cca7ea96ff4911cd300d3c973205d6 to your computer and use it in GitHub Desktop.
[完整] 发布 React Component 到 npm
  1. 新建项目
mkdir component-name && cd $_ 

npx create-react-library

touch README.md
  1. 开发
# run example to start developing your new component against
npm link # the link commands are important for local development
npm install # disregard any warnings about missing peer dependencies
npm start # runs rollup with watch flag

# (in another tab, run the example create-react-app)
cd example
npm link react-poop-emoji
npm install
npm start # runs create-react-app hot-reload dev server
  1. 编写 Example 使用 create-react-app
// example/src
import React, { Component } from 'react'
import PropTypes from 'prop-types'

// example of built-in support for importing css styles (optional)
import './styles.css'

export default class ExampleComponent extends Component {
  static propTypes = {
    text: PropTypes.string
  }

  render() {
    const {
      text
    } = this.props

    return (
      <div>
        Example Component: {text}
      </div>
    )
  }
}
  1. Git Stuffs
# be sure to update docs
vim README.md
vim package.json

# init and push git repo
git init
git add *
git commit -am "init"
# add git remote and push to remote github repo

  1. NPM Stuffs
# update dependencies, devDependencies, and peerDependencies
vim package.json
vim rollup.config.js

# build dist and publish to npm
npm publish
  1. Github Pages
npm run deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment