使用 rollup 打包 ts 模块

初始化一个 npm 项目

npm init

安装相关依赖

# rollup
npm i rollup -D
# typescript
npm i typescript -D
# 插件
npm i @rollup/plugin-typescript -D
npm i @rollup/plugin-json -D
npm i rollup-plugin-terser -D
npm i ts-lib -D

关于插件详情,可以从插件列表检索信息。

rollup 配置文件

// rollup.config.js
import typescript from '@rollup/plugin-typescript';
import { terser } from "rollup-plugin-terser";
export default {
    input: 'src/index.ts',
    output: [
        {
            file: './dist/index.ejs',
            format: 'es'
        },
        {
            file: './dist/index.js',
            format: 'cjs'
        },
    ],
    plugins:[typescript(),terser()]
  };

package.json

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/index.js",
  "module": "./dist/index.ejs",
  "typings": "./dist/index.d.ts",
  "scripts": {
    "build": "rollup -c"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@rollup/plugin-typescript": "^8.4.0",
    "rollup": "^2.79.0",
    "rollup-plugin-terser": "^7.0.2",
    "typescript": "^4.8.2"
  },
  "dependencies": {
    "tslib": "^2.4.0"
  }
}

打包申明文件

初始化 配置文件

npx tsc –init

修改tsconfig.ts

    "outDir": "./dist",   
    "declaration": true,