what’s the lerna ?
A tool for managing JavaScript projects with multiple packages. Tools for managing multi-module JS projects
What can Lerna do?
The two primary commands in Lerna are lerna bootstrap and lerna publish. bootstrap will link dependencies in the repo together. publish will help publish any updated packages. The first two commands are lerna Bootstrap and LERna publish Bootstrap, which associate dependencies within the repository. Publish publishes all updated packages
practice
1. Install and initialize the directory
NPM I lerna -g // create the practice directory mkdir lerna-democd lerna-demo
lerna init
Copy the code
Once initialized, git is automatically hooked up, although there is no repository yet
After installation, the directory is as follows:
- Packages - Lerna. json - package.json(project description file)Copy the code
2. Create a package
mkdir module-1
cd module-1
npm init -y
Copy the code
The NPM init -y command can skip the input panel and directly generate the default package.json. Do not use this in everyday projects.
This is the current file directory
➜ lerna - demo git tree. (master) ✗ ├ ─ ─ lerna. Json ├ ─ ─ package. The json └ ─ ─ packages └ ─ ─ the module 1 └ ─ ─ package. The json 2 directories, 3 filesCopy the code
3. Add dependencies to the project
{
"name": "module-1"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": []."author": ""."license": "ISC"// This is the added dependency"dependencies": {
"lodash": "^ 4.17.4"}}Copy the code
Install dependencies
lerna bootstrap
Copy the code
4. Upload projects
You need to create a repository on Git now and follow the instructions to link it
echo "# lerna-demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/daly-young/lerna-demo.git
git push -u origin master
Copy the code
The warehouse is connected. Let’s start publishing
lerna publish
Copy the code
Select the version number
I chose Major and went down
npm set init-license ISC
Copy the code
And then release it again
No problem. Successful launch
The process of publish is:
Git tag v1.0.0
2. Example of automatically updating dependency versions
3. Then publish each package to NPM
4. Finally, add the tag and corresponding commit to push
This is the normal creation and publication process for LERNA. The default mode is Fixed/Locked mode.
lerna mode
Lerna provides Fixed/Locked mode and Independent mode. What is the difference between these two modes?
Fixed/Locked mode
Fixed mode Lerna projects operate on a single version line. The version is kept in the lerna.json file at the root of your project under the version key. When you run lerna publish, if a module has been updated since the last time a release was made, it will be updated to the new version you’re releasing. This means that you only publish a new version of a package when you need to. This is the mode that Babel is currently using. Use this if you want to automatically tie all package versions together. One issue with this approach is that a major change in any package will result in all packages having a new major version. Fixed mode Lerna projects run on a single release line. This version is versioned in the lerna.json file in the root directory. When lerna publish is performed, if a module has been updated since the last release, it will be updated to the new version you want to publish. This means you only need to release a new version of the package when you need it. This is the model Babel currently uses. Use this option if you want to bind all package versions together automatically. One problem with this approach is that major changes in any package will cause all packages to be upgraded to the new version.
Run the code directly to try it ~~
mkdir module-2
cd module-2
npm init
lerna bootstrap
Copy the code
Although I only added one package, it shows two dependencies, indicating that all dependencies, including module-1, have been re-associated
And then I published that package and then I modified module-2
touch index.js
vim index.js
Copy the code
index.js
exports.demo = (param) => {
console.log('hello ' + param)
}
Copy the code
I also added it to the dependency file
"vuex": "^ 2.5.0"
Copy the code
In this case, I have made a major change. Did I change the version number manually? Or is it changed by default? It’s kind of silly for me to change it manually, so I just push it up, and lerna publishes and sure enough, it lets me select the version number again
And then we’ll find out
The innocent module-1 has been updated, and lerna.json has been updated with its version number. This is the fixed mode, we know, one update, all updates, so we don’t have to worry about dependencies between packages and version incompatibility. However, if there are many packages and the version number is superimposed on one change, it always feels a bit brutal, so let’s see if there are any new ideas for the second mode
Independent mode
This mode initialization needs to be specified
lerna init --independent
Copy the code
Independent mode Lerna projects allows maintainers to increment package versions independently of each other. Each time you publish, you will get a prompt for each package that has changed to specify if it’s a patch, minor, major or custom change. Independent mode allows you to more specifically update versions for each package and makes sense for a group of components. Combining this mode with something like semantic-release would make it less painful. (There is work on this already at atlassian/lerna-semantic-release). The Lerna project in standalone mode allows maintainers to increase package versions relatively independently. With each release, you will be prompted for each package that has changed to specify whether it is a patch, minor, major, or custom change. Standalone mode allows you to update versions of each package more specifically and only makes sense for a set of components. Free this pattern and semantics (???) A combination of things like that can reduce pain. (There’s already work on atlassian/Lerna-Seman-release).
// Set the version key in lerna.json to be independent to run in independent mode. Set the version keyin lerna.json to independent to run in independent mode.
Copy the code
It looks like this is independent, not affecting each other, so let’s try a wave
Restart a Lerna project, which will not be repeated here. Don’t forget to specify the mode. We have created packages for module-1 and module-2, and they have been published successfully. OK, next we will modify module-2, still adding index.js, and then publish
I changed module-2, but ask me to change module-1?
Then ask Module-2? Am I doing something wrong?
Immediately and not to check the document, found their own understanding is flawed, the relatively independent and not to say that I changed a package just send this one bag, but I changed a package, you can specify other package version, rather than like a fixed pattern, a large package upgrade version, all packages to upgrade version, it can be independent to each package customized version.. That’s what it means
This is just a simple operation, other API (official) you can try again ~~ remember to give someone else the official star ~~
Reference article:
Lerna manages best practices for front-end Packages official Github