|
@@ -0,0 +1,65 @@
|
|
|
|
|
+## npm私服搭建
|
|
|
|
|
+### qa
|
|
|
|
|
+verdaccio + pm2
|
|
|
|
|
+
|
|
|
|
|
+内网地址:http://192.168.1.90:4873
|
|
|
|
|
+
|
|
|
|
|
+内网穿透:http://smartcost.in.8866.org:26904/
|
|
|
|
|
+## 客户端使用
|
|
|
|
|
+在指定了私服地址后执行npm install,package.json中特定前缀的包(比如"@sc/**")会从私服中下载,其他前缀的包从taobao下载。(verdaccio config.yml中配置)
|
|
|
|
|
+### npm设置
|
|
|
|
|
+将npm registry设置为私服地址``http://192.168.1.90:4873``,设置了npm registry后才能安装、发布、取消发布、更新私服的包。
|
|
|
|
|
+
|
|
|
|
|
+`npm config set registry http://192.168.1.90:4873 `
|
|
|
|
|
+
|
|
|
|
|
+## nrm
|
|
|
|
|
+由于npm的私服包的安装或发布都需要指定registry,为了方便,可以使用nrm来管理registry。
|
|
|
|
|
+#### 安装
|
|
|
|
|
+npm install -g nrm
|
|
|
|
|
+#### 使用
|
|
|
|
|
+- 将registry地址用别名来取代: ``nrm add smartcost http://192.168.1.90:4873 ``
|
|
|
|
|
+- 切换npm registry: ``nrm use smartcost``
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+使用nrm use命令切换registry后,npm install等命令不需要带上--registry选项了。
|
|
|
|
|
+
|
|
|
|
|
+使用`nrm ls`可以查看当前npm registry地址
|
|
|
|
|
+
|
|
|
|
|
+- 添加用户 npm adduser
|
|
|
|
|
+
|
|
|
|
|
+## npm包开发、发布流程
|
|
|
|
|
+### 开发
|
|
|
|
|
+1. 克隆SCCommon项目 http://192.168.1.41:3000/SmartCost/SCCommon ,用于各库的git管理,所有库作为SCCommon项目的子目录,如test库:`SCCommon/test/...`
|
|
|
|
|
+2. 基于JavaScript编写库或TypeScript编写库,从git拷贝相应工程目录到SCCommon项目的对应库目录下。
|
|
|
|
|
+
|
|
|
|
|
+ js-template:
|
|
|
|
|
+
|
|
|
|
|
+ http://192.168.1.41:3000/SmartCost/javascript-package-template
|
|
|
|
|
+
|
|
|
|
|
+ ts-template:
|
|
|
|
|
+
|
|
|
|
|
+ http://192.168.1.41:3000/SmartCost/typescript-package-template
|
|
|
|
|
+
|
|
|
|
|
+3. 在对应库目录下(如SCCommon/test/)安装依赖`npm install`。
|
|
|
|
|
+4. 修改package.json文件,将name字段修改为包名称。必须以“@sc/”开头。
|
|
|
|
|
+5. 在对应库目录下的src文件夹下开始开发(注意src文件夹必须要有index入口文件(.js、.ts))。
|
|
|
|
|
+6. 在对应库目录下执行打包命令`npm run build`,打包后的文件会被输出到dist文件夹。
|
|
|
|
|
+7. 如果库基于JavaScript开发,且库需要被TypeScript项目引入。由于TypeScript是强类型语言,必须要有类型声明。因此,必须要在dist文件夹下,编写index.d.ts声明文件。参考: https://www.tslang.cn/docs/handbook/declaration-files/introduction.html
|
|
|
|
|
+
|
|
|
|
|
+注意:rollup打包模块基于ESModule,因此在开发包的时候,模块规范为ESModule(import... export...)
|
|
|
|
|
+### 发布
|
|
|
|
|
+在npm注册了私服地址后,在对应库目录下(如SCCommon/test/)下执行命令:
|
|
|
|
|
+
|
|
|
|
|
+`npm publish`
|
|
|
|
|
+
|
|
|
|
|
+若报错提示缺少权限,则执行`npm adduser`添加新用户或登陆`npm login`。
|
|
|
|
|
+### 更新
|
|
|
|
|
+#### 本地代码
|
|
|
|
|
+本地代码通过git方式提交至SCCommon仓库(每个库都为SCCommon的一个子目录)
|
|
|
|
|
+#### npm包
|
|
|
|
|
+1. 更新npm包时,应对npm包版本号(package.json version字段)进行迭代:`npm version x.x.x`
|
|
|
|
|
+2. 重新发布`npm publish`(当前版本号与远程包版本号一样的情况下无法更新)
|
|
|
|
|
+#### 使用了相关包的项目
|
|
|
|
|
+执行`npm update libName`
|
|
|
|
|
+
|
|
|
|
|
+若更新失败,可删除package-lock.json后重新执行安装命令。
|