1.0.0 • Published 1 year ago

wd-tracker v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

一、工程初始化步骤

  1. 常见工程目录 wd-tarcker

  2. 创建 src、src/core/index.ts、src/types/index.ts、src/utils/pv.ts

  3. 创建 index.html、README.md、rollup.config.js

  4. 终端执行 npm init -y 生成 package.json

  5. 终端执行 tsc --init 生成 tsconfig.json

    ps:若 tsc 无法执行,全局安装 yarn add typescript -g

二、安装开发依赖

yarn add rollup -D
yarn add rollup-plugin-dts -D
yarn add rollup-plugin-typescript2 -D
yarn add typescript -D

package.json

{
  "name": "wd-tracker",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "module": "dist/index.esm.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "rollup -c",
    "sourcemap": "rollup -c --sourcemap"
  },
  "keywords": [
    "前端",
    "埋点",
    "tracker"
  ],
  "author": "",
  "files": [
    "dist"
  ],
  "license": "ISC",
  "devDependencies": {
    "rollup": "^2.76.0",
    "rollup-plugin-dts": "^4.2.2",
    "rollup-plugin-typescript2": "^0.32.1",
    "typescript": "^4.7.4",
    "@babel/core": "^7.19.1",
    "@babel/preset-env": "^7.19.1",
    "@rollup/plugin-babel": "^5.3.1",
    "@rollup/plugin-node-resolve": "^14.1.0",
    "rollup-plugin-terser": "^7.0.2"
  }
}

三、配置 rollup.config.js

import ts from 'rollup-plugin-typescript2'
import path from 'path'
import dts from 'rollup-plugin-dts';
export default [{
    //入口文件
    input: "./src/core/index.ts",
    output: [
        //打包esModule
        {
            file: path.resolve(__dirname, './dist/index.esm.js'),
            format: "es"
        },
         //打包common js
        {
            file: path.resolve(__dirname, './dist/index.cjs.js'),
            format: "cjs"
        },
       //打包 AMD CMD UMD
        {
            input: "./src/core/index.ts",
            file: path.resolve(__dirname, './dist/index.js'),
            format: "umd",
            name: "tracker"
        }

    ],
    //配置ts
    plugins: [
        ts(),
    ]

}, {
    //打包声明文件
    input: "./src/core/index.ts",
    output:{
        file: path.resolve(__dirname, './dist/index.d.ts'),
        format: "es",
    },
    plugins: [dts()]
}]

https://blog.csdn.net/qq_48896417/article/details/128487180