lerna開發流程(入坑和出坑)

lerna常用api總結:

lerna bootstrap 安裝依賴
lerna clean 刪除各個包下的node_modules
lerna init 創建新的lerna庫
lerna list 顯示package列表
lerna changed 顯示自上次relase tag以來有修改的包, 選項通 list
lerna diff 顯示自上次relase tag以來有修改的包的差異, 執行 git diff
lerna exec

在每個包目錄下執行任意命令

lerna run 執行每個包package.json中的腳本命令

lerna add

添加一個包的版本爲各個包的依賴
lerna import 引入package
lerna link 鏈接互相引用的庫
lerna create 新建package
lerna publish 發佈

備註:可以使用lerna [command] -h 查找單個命令的使用方法和參數

lerna bootstrap 

執行該命令會做下面四件事情:

  1. 爲每個包安裝依賴
  2. 鏈接相互依賴的庫到具體的目錄
  3. 執行 npm run prepublish
  4. 執行 npm run prepare

參數有以下幾個

-- --production --no-optional 指定npm client的參數  

--hoist

把依賴安裝到根目錄的node_modules  

--ignore

忽略的包 --ignore test-* 忽略名稱以test開頭的包
--scope  指定的包 參數的含義是指包的名稱

--ignore-scripts

不執行聲明週期腳本命令, 比如 prepare  

--registry <url>

指定registry  

--npm-client

指定安裝用的npm client lerna bootstrap --npm-client=yarn

--use-workspace

使用yarn workspace, 沒用過  

--no-ci

默認調用 npm ci 替換 npm install , 使用選項修改設置 npm ci 類似於 npm-install ,但它旨在用於自動化環境,如測試平臺,持續集成和部署。
--skip-git 將不會創建git commit或tag  
--skip-npm 將不會把包publish到npm上  
--canary 可以用來獨立發佈每個commit,不打tag lerna publish --canary

備註:npm ci 會刪除node_modules文件夾,並且要有package-lock.json文件

——————————————————————————————————————————————————

lerna list 列舉當前lerna 庫包含的包

 

--json

顯示爲json格式  

--all

顯示包含private的包  

--long

顯示更多的擴展信息  

——————————————————————————————

lerna changed

顯示自上次relase tag以來有修改的包, 選項通 list

——————————————————————————————————————————————————

lerna diff

顯示自上次relase tag以來有修改的包的差異, 執行 git diff

——————————————————————————————————————————————————

lerna exec

在每個包目錄下執行任意命令

--concurrency

默認命令時並行執行的, 我們可以設置併發量爲1(全局參數) lerna exec --concurrency 1 -- ls -la

--scope

設置包含的package lerna exec --scope my-component -- ls -la

--stream

交叉並行輸出結果 lerna exec --stream -- babel src -d lib

--parallel

 Execute command with unlimited concurrency, streaming prefixed output.  

--no-bail

Continue executing command despite non-zero exit in a given package.  

lerna run 選項同lerna exec

執行每個包package.json中的腳本命令

——————————————————————————————————————————————————

lerna init  

創建一個新的lerna庫或者是更新lerna版本

默認lerna有兩種管理模式, 固定模式和獨立模式

  • 固定模式  --exact

固定模式,通過lerna.json的版本進行版本管理。當你執行lerna publish命令時, 如果距離上次發佈只修改了一個模塊,將會更新對應模塊的版本到新的版本號,然後你可以只發布修改的庫。

這種模式也是Babel使用的方式。如果你希望所有的版本一起變更, 可以更新minor版本號,這樣會導致所有的模塊都更新版本。

  • 獨立模式 --independent

獨立模式,init的時候需要設置選項 --independent. 獨立模式允許管理者對每個庫單獨改變版本號,每次發佈的時候,你需要爲每個改動的庫指定版本號。這種情況下, lerna.json的版本號不會變化了, 默認爲independent

——————————————————————————————————————————————————

lerna clean

刪除各個包下的node_modules

——————————————————————————————————————————————————

lerna import

導入指定git倉庫的包作爲lerna管理的包

--flatten

如果有merge衝突, 用戶可以使用這個選項所謂單獨的commit lerna import ~/Product --flatten

--dest

可以指定導入的目錄(lerna.json中設定的目錄) lerna import ~/Product --dest=utilities

lerna add

添加一個包的版本爲各個包的依賴

lerna add <package>[@version] [--dev] [--exact]

lerna link

鏈接互相引用的庫

——————————————————————————————————————————————————

lerna create

新建包

——————————————————————————————————————————————————

lerna.json解析

{
  "version": "1.1.3",
  "npmClient": "npm",
  "command": {
    "publish": {
      "ignoreChanges": [
        "ignored-file",
        "*.md"
      ],
      "allowBranch": ["master", "feature/*"]
    },
    "bootstrap": {
      "ignore": "component-*",
      "npmClientArgs": ["--no-package-lock"]      
    }
  },
  "packages": ["packages/*"]
}

 

lerna發佈相關

1,lerna publish做哪些事情

  • 運行lerna updated來決定哪一個包需要被publish
  • 如果有必要,將會更新lerna.json中的version
  • 將所有更新過的的包中的package.json的version字段更新
  • 將所有更新過的包中的依賴更新
  • 爲新版本創建一個git commit或tag
  • 將包publish到npm上

2, lerna publish發佈失敗後怎樣操作,如下:採用 from-package

Positionals

bump from-git

In addition to the semver keywords supported by lerna versionlerna publish also supports the from-git keyword. This will identify packages tagged by lerna version and publish them to npm. This is useful in CI scenarios where you wish to manually increment versions, but have the package contents themselves consistently published by an automated process.

bump from-package

Similar to the from-git keyword except the list of packages to publish is determined by inspecting each package.json and determining if any package version is not present in the registry. Any versions not present in the registry will be published. This is useful when a previous lerna publish failed to publish all packages to the registry.

具體參見:https://github.com/lerna/lerna/tree/master/commands/publish#readme

3,  symlink 的問題

如果我們的package中有webpack,那麼其中的loader很有可能會出問題

假設 package 下面有一個包 pkg1 ,依賴 package 下面的另一個包 pkg2 。
運行 lerna bootstrap 之後, pkg1/node_modules 下就會出現 pkg2 的 symlink 。

我們遇到的問題是在pkg2中有一個TS文件,export出去。pkg1中去引入,但是發現總是沒有命中真實的loader。

如果使用 webpack 系列工具來編譯運行 pkg1 ,由於 webpack loader 判斷路徑默認是按照真實路徑來的,所以 pkg2 對應到的路徑是 [project root]/package/pkg2 ,而不是 [project root]/package/pkg1/node_modules/pkg2

這樣一來,如果需要 pkg2 中的源碼過 pkg1 的 loader (比如 pkg2 中的 TS 通過 pkg1 的 ts-loader),就需要特殊配置。這和不涉及 symlink 的真實場景存在較大差異。
同時,很多配置(比如 postcssrc 、 babelrc 、 eslintrc 等)是以 resolve 到的文件去解析的。

所以此時其實很希望 webpack loader 基於 symlink 的路徑去解析判斷 include / exclude 等配置,而不是按照真實文件的路徑。

所以需要配置webpack 的 resolve.symlinks 來解決這個問題,具體參見官方文檔

4,指定cnpm源無效

gitlab issue publish時不接受參數,只能使用npm倉庫。
但是舊版本 2.x 的支持,本人測試,截止到3.3.2,3.x的lerna指定cnpm源publish均無效。

 


參考:https://www.jianshu.com/p/2f9c05b119c9

https://www.jianshu.com/p/8b7e6025354b

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章