工程步骤
初始化lerna项目,修改配置文件,加入快捷命令
lerna init
# \lerna.json
{
"packages": [
"apps/app",
"modules/module"
],
"version": "1.0.0"
}
# \package.json
{
"name": "lernatest",
"version": "1.0.0",
"description": "",
"scripts": {
"prebuild": "npm install && lerna bootstrap",
"build:dev": "npm run build",
"build": "lerna run build --stream",
"clean": "lerna run clean --stream",
"clean-all": "npm run remove-package-lock && npm run remove-maven-folders && npm run remove-node-modules",
"remove-node-modules": "npx lerna exec -- rm -rf node_modules && rm -rf node_modules",
"remove-package-lock": "npx lerna exec -- rm -rf package-lock.json && rm -rf package-lock.json",
},
"author": "",
}
在\modules\module\目录执行
npm init -y
加入moment依赖后得到的文件
# \modules\module\package.json
{
"name": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"moment": "^2.27.0"
}
}
# modules\module\index.js
const moment= require('moment')
function fmtTime(ymd){
console.log(moment(ymd).format("YYYY-MM-DD"))
}
const API = 'http://test.com/mock/api/test';
module.exports = {
API,
fmtTime
}
在\apps\app\目录执行
npm init -y
# apps\app\package.json
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "babel-node index.js "
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"module": "^1.0.0"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0"
}
}
上面的package.json中对module的依赖可以通过在\目录下执行以下命令加入, 外部库依赖也可以使用该命令
lerna add module --scope=app
# apps\app\index.js
import M from 'module'
import axios from 'axios'
const api=M.API
const fmtTime=M.fmtTime
const request = () => axios.get(api)
request().then((res) => console.log(res.data.data[0]));
fmtTime('20200628')
# apps\app\.babelrc
{
"presets": [
"env"
],
"plugins": []
}
在\目录进行编译(如果有编译步骤)
lerna run build
在\apps\app目录执行
npm run start
每次依赖改动后要执行
lerna bootstrap