使用 rollup 打包 ts 模块

初始化一个 npm 项目

npm init

安装相关依赖

# rollup
npm i rollup@^2.79.1 -D
# typescript
npm i typescript -D
# 插件
npm i @rollup/plugin-typescript -D
npm i rollup-plugin-terser -D
npm i tslib -D

注意 rollup 使用的版本

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

rollup 配置文件

// rollup.config.ts
import typescript from "@rollup/plugin-typescript";
import {terser} from "rollup-plugin-terser";
import del from "rollup-plugin-delete";

export default {
    input: "src/index.ts",
    output: [
        {
            file: "./dist/index.es.js",
            format: "es",
        },
        {
            file: "./dist/index.cjs.js",
            format: "cjs",
        },
    ],
    plugins: [del({targets: "dist/*"}), typescript(), terser()],
    external: []
};

package.json

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/index.cjs.js",
  "module": "./dist/index.es.js",
  "typings": "./dist/index.d.ts",
  "files": [
    "dist"
  ],
  "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,   

类似文章