The skeleton of monorepo project with lerna and yarn workspace.
Yarn is needed for managing the dependencies of this monorepo project. So prepare to install yarn before getting start. Refer to this Installation Document.
# or use npm
npm install yarn -g
- Clone this project as skeleton repo.
git clone [email protected]:Sara2009/start-monorepo-with-lerna-yarnworkspace.git
- Install Lerna globally (recommend).
npm install -g lerna
- Install dependencies. The dependencies of the project will be installed at the repo root. So they're available to all packages.
# use npm
npm run bootstrap
# or use yarn
yarn install
Then the project is initialized successfully.
Use lerna create <package-name>
to create a new package. This is our project structure:
- src: source code.
- lib: the commonjs2 format code.
- es: the esm format code.
- dist: the umd format code.
.
├── node_modules
├── packages
│ ├── module1
│ │ ├── __tests__
│ │ ├── src
│ │ ├── lib
│ │ ├── es
│ │ ├── README.md
│ │ └── package.json
│ ├── module2
│ │ ├── __tests__
│ │ ├── src
│ │ ├── dist
│ │ ├── es
│ │ ├── README.md
│ │ └── package.json
│ └── ...
├── scripts
├── .eslintrc.js
├── .gitignore
├── .prettier
├── commitlint.config.js
├── lerna.json
├── LICENSE
└── package.json
Run yarn add <package-name> -W
to install external depenencies at the repo root.
If packages/module2 depends on the packages/module1, a symlink is needed. Lerna analyzes the packages and automatically creates the required npm links between them.
# use lerna
# Install module1 to module2
lerna add module1 --scope=module2
We can also use yarn to install local dependencies. But refer to this issue, yarn workspace <workspace-name> add <package-name>
is failed unless specifying the exact version of the local package.
yarn workspace module2 add [email protected]
Or just write the dependency in the package.json of module2.
// packages/module2/package.json
{
"dependencies": {
"module1": "^0.0.0"
}
}
Then run yarn install
at the repo root.
Use git-cz
to replace git commit
. Execute npm run commit
to submit. We use commitlint to check if your commit messages meet the conventional commit format.
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
npm run version
# or use lerna
lerna version
lerna version
supports semver keyword.
npm run publish
Lerna will automatically run prepublish
script before it. So we can configure the build command with npm run prepubish
.
Execute npm run version
locally and then a git tag will be pushed. The CI will execute npm run publish
after a git tag hook.