天天看点

(RN) 开发并发布三方库-npm

项目中,封装的一些RN组件希望传到npmjs上供其它人下载,下面介绍个大致步骤:

1.安装react-native-create-library

npm install -g react-native-create-library

2.创建RN项目(如果你的项目名想显示为react-native-xxx-yyy-zz,取名应为xxx-yyy-zz,原因见步骤3;命令见react-native-create-library -h)

react-native-create-library --package-identifier com.chenzhe.rnimageplaceholder --platforms ios,android --author-name <作者名字> --author-email <作者邮箱> --license MIT cz-image-placeholder

3.修改项目名称(因为react-native-create-library初始化的项目,会默认加上react-native或者RN前缀)

mv cz-image-placeholder react-native-cz-image-placeholder

4.根据你的项目实际情况修改package.json文件,下面是笔者某个项目刚init完修改后(仅供参考):

{

  "name": "react-native-cz-pack-element",

  "version": "0.0.1",

  "description": "",

  "main": "index.js",

  "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1"

  },

  "keywords": [

    "react-native"

  ],

  "author": "",

  "license": ""

}

5.如果涉及到调用原生一些Module方法,由于笔者只做过iOS的,下面贴出笔者某个项目刚init完修改后(仅供参考):

Pod::Spec.new do |s|

  s.name         = "RNCzPackElement"

  s.version      = "0.0.1"

  s.summary      = "RNCzPackElement"

  s.description  = "基于AppRegister包装一些自定义组件"

  s.homepage     = "https://github.com/chenzhe555/react-native-cz-pack-element"

  s.license      = { :type => "MIT", :file => "LICENSE" }

  s.author       = { "author" => "[email protected]" }

  s.platform     = :ios, "9.0"

  s.source       = { :git => "https://github.com/chenzhe555/react-native-cz-pack-element.git", :tag => s.version }

  s.source_files = "*.{h,m}"

  s.requires_arc = true

  s.dependency "React"

  #s.dependency "others"

end

6.登录npm

如果没有账号,注册一个 npm adduser

如果有账号,直接登录 npm login

7.发布项目到npm:https://www.npmjs.com(如果出现不能下载,请确认使用到npm源是否有拉取最新的代码)

如果是第一次发布: npm publish

如果不是第一次发布:

先执行:npm version <type>,版本号会自动增加,然后npm publish 发布成功~

patch: 1.0.1->1.0.2

minor: 1.0.1->1.1.1

major: 1.0.1->2.0.1