14.3 企业脚手架-- tsup 的配置和使用

78 阅读1分钟

1.安装tsup

  • 执行pnpm i tsup,npm install -g typescript
  • tsc --init分别在根目录和cli目录下执行

根节点创建tsconfig.json文件
子项目的tsconfig.json文件继承根节点的tsconfig.json文件

1. tsup.config.ts配置

import { defineConfig } from "tsup"

export default  defineConfig ({
    "dts": true,
    "entry": ["./index.ts"],
    "format": ["cjs"],
    "outDir": "dist"

})

2.企业级脚手架\wzm-cli\tsconfig.json

{
    "compilerOptions": {
        "target": "ES2015",
        "lib": ["ESNext", "DOM"],
        "module": "ESNext",
        "moduleResolution": "node",
    },
    "include": ["packages/**/*"]
}

3.企业级脚手架\wzm-cli\packages\cli\tsconfig.json

{
    "extends": "../../tsconfig.json",
    "exclude": ["node_modules","dist"],
    "include": ["src","./tsup.config.ts"]
}

生成index.d.ts文件

4.根节点tsconfig.json配置,或者子节点配置

{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
        "declaration": true
    },

    "exclude": ["node_modules","dist"],
    "include": ["src","tsup.config.ts"]
}

tsconfig.json配置

import { defineConfig } from "tsup"

export default  defineConfig ({
    "dts": true, // 生成类型声明文件
    "entry": ["./index.ts"], // 入口文件
    "format": ["cjs"], // 打包格式 cjs esm umd iife commonjs amd system
    "outDir": "dist" // 输出目录 
})

5.cli下的pacckage.json文件

  "scripts": {
    "dev": "tsup --watch",
    "build": "tsup"
},

6.修改bin/wzmtest文件

require('../dist/index.js')
console.log('hello world')

重新执行npm linkpnpm run build 及 "wzm-cli-demo"