We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
monorepo介绍 Monorepo 是管理项目代码的一个方式,指在一个项目仓库(repo)中管理多个模块/包(package)
monorepo
vue3项目结构
reactivity
runtime-core
runtime-dom
runtime-test
server-renderer
compiler-core
compiler-dom
compiler-ssr
compiler-sfc
size-check
template-explorer
shared
vue
+---------------------+ | | | @vue/compiler-sfc | | | +-----+--------+------+ | | v v +---------------------+ +----------------------+ | | | | +-------->| @vue/compiler-dom +--->| @vue/compiler-core | | | | | | +----+----+ +---------------------+ +----------------------+ | | | vue | | | +----+----+ +---------------------+ +----------------------+ +-------------------+ | | | | | | | +-------->| @vue/runtime-dom +--->| @vue/runtime-core +--->| @vue/reactivity | | | | | | | +---------------------+ +----------------------+ +-------------------+
yarn
npm
yarn init -y
修改生成的package.json文件,添加以下内容:
package.json
{ "private": true, // 表示该项目不会发布到npm上,只是用来管理的 "workspaces": [ // 指定管理的包的路径 "packages/*" ] }
yarn add typescript -W
typescript
npx tsc --init
node_modules/.bin/tsc
tsconfig.json
// "module": "commonjs", // rollup不支持打包commonjs "module": "ESNext", // 一般默认都修改为ESNext
yarn add rollup rollup-plugin-typescript2 @rollup/plugin-node-resolve @rollup/plugin-json execa -D -W # rollup 打包工具 # rollup-plugin-typescript2 rollup和ts的桥梁 # @rollup/plugin-node-resolve 解析node第三方模块 # @rollup/plugin-json 支持引入json # execa 开启子进程方便执行命令
. ├── package.json # 项目信息 ├── packages # monorepo方式管理包根路径 │ ├── reactivity # Vue响应式模块 │ │ ├── package.json │ │ └── src │ │ └── index.ts │ └── shared # Vue共享模块 │ ├── package.json │ └── src │ └── index.ts ├── rollup.config.js # rollup配置文件 ├── scripts # 打包脚本 │ └── build.js ├── tsconfig.json # typescript配置文件 └── yarn.lock
// packages/reactivity/package.json { "name": "@vue/reactivity", //@vue表示命名空间 "version": "1.0.0", "main": "index.js", "license": "MIT", "buildOptions": { //自定义打包配置项 "name": "VueReactivity", "formats": [ // 打包的格式 "esm-bundler", "cjs", "global" ] } }
packages
scripts/build.js
const fs = require('fs'); const execa = require('execa'); // 1.解析packages目录,过滤出所有的目录(即需要打包的模块) const dirs = fs.readdirSync('packages').filter(item => { if (!fs.statSync(`packages/${item}`).isDirectory()) { return false; } return true; }); // 2.并行打包所有文件夹 async function build(target) { /* * -c 表示使用配置文件rollup.config.js * --environment 表示rollup执行时传递环境变量,此处传递的环境变量为`TARGET:${target}` * { stdio: 'inherit' } 表示子进程输出打印到父进程标准输出中 */ await execa('rollup', ['-c', '--environment', `TARGET:${target}`], { stdio: 'inherit' }); } async function runParallel(dirs, exec) { let result = []; for (let item of dirs) { result.push(exec(item)); } return Promise.all(result); // 待所有打包操作完毕后,调用成功 } runParallel(dirs, build).then(() => { console.log('打包成功'); })
rollup
rollup.config.js
import path from 'path'; import ts from 'rollup-plugin-typescript2'; // 解析ts插件 import resolvePlugin from '@rollup/plugin-node-resolve'; // 解析第三方模块 // 获取packages目录 let packagesDir = path.resolve(__dirname, 'packages'); // 获取需要打包模块的路径 let pkgDir = path.resolve(packagesDir, process.env.TARGET); const resolvePath = item => path.resolve(pkgDir, item); // 将用户传入的路径与打包模块目录合并 // 获取打包模块的package.json文件内容 let pkg = require(resolvePath('package.json')); // 获取模块自定义参数 let pkgOpts = pkg.buildOptions; // 获取模块的文件夹名称 let pathName = path.basename(pkgDir); // 一个包需要打包成多个格式 esModule commonjs iife const outputConfig = { 'esm-bundler': { file: resolvePath(`dist/${pathName}.esm-bundler.js`), format: 'es' }, 'cjs': { file: resolvePath(`dist/${pathName}.cjs.js`), format: 'cjs' }, 'global': { file: resolvePath(`dist/${pathName}.global.js`), format: 'iife' } } function createConfig(output){ output.name = pkgOpts.name; // 用于 iife 在 window 上挂载的属性 output.sourcemap = true; // 生成sourcemap,便于调试。tsconfig.json中也需要开启 return { input: resolvePath('src/index.ts'), // 打包入口 output, plugins: [ ts({ // ts 编译时配置文件 tsconfig: path.resolve(__dirname, 'tsconfig.json') }), resolvePlugin() ] } } // 根据用户打包模块中提供的formats选项,去outputConfig配置里取值生成配置文件 export default pkgOpts.formats.map(format=>createConfig(outputConfig[format]));
package.json 添加scripts
scripts
"scripts": { "build": "node scripts/build.js" }
保存后,在项目根目录下命令行执行:npm run build。
npm run build
虽然目前可以成功打包整个项目模块,但是在开发过程中每次调用npm run build很浪费性能,我们更希望可以控制某个模块单独进行打包。于是我们添加一个执行脚本"dev": "node scripts/dev.js":
"dev": "node scripts/dev.js"
scripts/dev.js
const execa = require('execa'); async function build(target) { // -cw 表示打包并监视文件文件变化,需要打包的模块变化时自动打包 await execa('rollup', ['-cw', '--environment', `TARGET:${target}`], { stdio: 'inherit' }); } build('reactivity'); // 仅打包响应式模块
扩展:组件库也可以采用monorepo的方式,好处就是每个组件都可以独立发布。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
vue3与vue2的对比
vue3整体架构
monorepo
介绍Monorepo 是管理项目代码的一个方式,指在一个项目仓库(repo)中管理多个模块/包(package)
vue3项目结构
reactivity
:响应式系统runtime-core
:与平台无关的运行时核心 (可以创建针对特定平台的运行时 - 自定义渲染器)runtime-dom
:针对浏览器的运行时。包括DOM API,属性,事件处理等runtime-test
:用于测试server-renderer
:用于服务器端渲染compiler-core
:与平台无关的编译器核心compiler-dom
:针对浏览器的编译模块compiler-ssr
:针对服务端渲染的编译模块compiler-sfc
:针对单文件解析size-check
:用来测试代码体积template-explorer
:用于调试编译器输出的开发工具shared
:多个包之间共享的内容vue
:完整版本,包括运行时和编译器vue3模块管理环境搭建
yarn
初始化项目因为我们希望通过
monorepo
的方式管理项目下的多个模块,npm
安装管理模块无法实现此功能,故使用yarn
来管理模块。修改生成的
package.json
文件,添加以下内容:yarn add typescript -W
typescript
:npx tsc --init
(不带npx表示执行全局下的tsc,加npx执行的是当前目录下node_modules/.bin/tsc
命令去初始化)tsconfig.json
文件:编译打包环境搭建
packages
下模块进行打包rollup
配置保存后,在项目根目录下命令行执行:
npm run build
。编译开发打包环境搭建
虽然目前可以成功打包整个项目模块,但是在开发过程中每次调用
npm run build
很浪费性能,我们更希望可以控制某个模块单独进行打包。于是我们添加一个执行脚本"dev": "node scripts/dev.js"
:扩展:组件库也可以采用monorepo的方式,好处就是每个组件都可以独立发布。
The text was updated successfully, but these errors were encountered: