搭建monorepo工程--工具包

28 阅读1分钟

项目地址

创建项目
pnpm init 
新建 pnpm-workspace.yaml
创建/utils/tools
pnpm init 

全局安转ts支持 pnpm add typescript -D -w
全局安转测试框架 pnpm add vitest -D -w
tests下的测试文件报错 新建project01/utils/tools/global.d.ts 文件 内容为 /// <reference types="vitest/globals" />,告诉文件类型这里拿

执行 test 命令报错,ReferenceError: test is not defined
"scripts": {
    "test": "vitest"
  },

添加配置文件 /project01/utils/tools/vitest.config.ts
添加配置 /project01/utils/tools/package.json    "type": "module",
pnpm  run test   成功通过
新建 /project01/utils/tools/tsconfig.json
开始打包
安装全局依赖    pnpm add @babel/preset-env @rollup/plugin-babel @rollup/plugin-commonjs @rollup/plugin-json @rollup/plugin-node-resolve rollup  rollup-plugin-typescript2 -D -w
新建/utils/tools/rollup.config.js
添加配置 /project01/utils/tools/package.json 
    暴露的位置信息很重要 main module types exports
    {
    "name": "tools",
    "version": "1.0.0",
    "description": "",
    "main": "dist/index.cjs",
    "module": "dist/index.js",
    "type": "module",
    "types": "dist/types/index.d.ts",
    "exports": {
        ".": {
        "import": "./dist/index.js",
        "require": "./dist/index.cjs"
        }
    },
    "scripts": {
        "test": "vitest",
        "build": "rollup -c"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
    }

pnpm  run  build  打包成功


同理新增一个项目 /project01/projects/tools-test-project
pnpm init 
在 /project01/projects/tools-test-project执行  pnpm add tools --workspace --filter tools-test-project ,将包安转进来
新建 tsconfig.json rollup.config.js

package.json
{
  "name": "tools-test-project",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.cjs",
  "module": "dist/index.js",
  "type": "module",
  "types": "dist/types/index.d.ts",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs"
    }
  },
  "scripts": {
    "test": "tsc && node ./dist/index.js",
    "build": "rollup -c"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "packageManager": "pnpm@10.13.0",
  "dependencies": {
    "tools": "workspace:*"
  }
}


子包要打完包,后面的才可以打包,不然找不到文件的哦