123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import serve from 'rollup-plugin-serve'
- import typescript from '@rollup/plugin-typescript'
- import resolve from '@rollup/plugin-node-resolve'
- import commonjs from '@rollup/plugin-commonjs'
- import autoExternal from 'rollup-plugin-auto-external'
- import terser from '@rollup/plugin-terser'
- import babel from '@rollup/plugin-babel'
- import dts from 'rollup-plugin-dts'
- import json from './package.json' assert { type: 'json' }
- import { execSync } from 'child_process'
- // 需要 build 的目录
- const model = process.env.model
- function executeCommand(command) {
- return execSync(command, { encoding: 'utf-8' }).trim()
- }
- // sdk 的版本号
- const version = json.version
- // sdk build 时间戳
- const newDate = new Date()
- // build 时的 git 信息
- const currentBranch = executeCommand('git rev-parse --abbrev-ref HEAD')
- const latestCommitID = executeCommand('git rev-parse HEAD')
- const latestCommitMessage = executeCommand('git log -1 --pretty=%B')
- // sdk 的头部信息
- const getBannerText = (module, standard) => {
- return `/*!
- - Name ${module.toUpperCase()}_CTI
- - FileName ${module}-cti
- - Version ${version}
- - JS Standard ${standard}
- - Author ${json.author}
- - Built on ${newDate.toLocaleDateString()} ${newDate.toLocaleTimeString()}
- - GitHub ${json.repository.url}
- - Branch ${currentBranch}
- - CommitID ${latestCommitID}
- - CommitMessage ${latestCommitMessage}
- */`
- }
- // 获取输出配置
- const getCommonOutput = (module, format, standard, terser) => {
- return {
- format,
- name: `${module.toUpperCase()}_CTI`,
- globals: {
- 'socket.io-client': 'io',
- 'sip.js': 'SIP'
- },
- exports: 'named',
- file: `dist/${module}-cti.${standard}.${format}.${terser ? 'prod.' : ''}js`,
- banner: getBannerText(module, standard),
- sourcemap: !!process.env.sourcemap
- }
- }
- // 获取插件配置
- const getCommonPlugin = standard => {
- return [
- resolve(),
- commonjs(),
- autoExternal(),
- typescript({
- tsconfig: `tsconfig.${standard}.json`
- }),
- babel({
- extensions: ['.js', '.ts'],
- babelHelpers: 'bundled',
- presets: ['@babel/preset-env']
- })
- ]
- }
- // 需要生成的格式配置
- const configArr = [
- { format: 'umd', standard: 'es5' },
- { format: 'umd', standard: 'es6' },
- { format: 'esm', standard: 'es5' },
- { format: 'esm', standard: 'es6' }
- ]
- // 后续增加其他的需手动增加
- const buildModuleList = ['hs']
- const generateExport = () => {
- // 如果指定了需要打包的模块,则单独打包
- if (model) {
- return buildModel(model)
- } else {
- const exportAll = []
- // 若未指定,则打包所有的模块
- buildModuleList.forEach(module => {
- exportAll.push(...buildModel(module))
- })
- return exportAll
- }
- }
- function buildModel(module) {
- const exportArr = []
- // 类型声明文件
- exportArr.push({
- input: `src/${module}-cti/index.dts.ts`,
- plugins: [dts()],
- output: {
- banner: getBannerText(module, 'any'),
- file: `dist/${module}-cti.d.ts`,
- format: 'es'
- }
- })
- // 统一输出 js 模块的入口文件
- const input = `src/${module}-cti/index.ts`
- // 分别生成不混淆和混淆两版
- configArr.forEach(item => {
- exportArr.push(
- {
- input,
- output: getCommonOutput(module, item.format, item.standard, false),
- plugins: getCommonPlugin(item.standard)
- },
- {
- input,
- output: getCommonOutput(module, item.format, item.standard, true),
- plugins: [...getCommonPlugin(item.standard), terser()]
- }
- )
- })
- return exportArr
- }
- // export default generateExport()
- export default [
- ...generateExport(),
- {
- input: 'src/cs-cti/index.ts', // 指定一个任意入口,防止报错,可以是一个空的文件
- output: {
- dir: 'dist', // 输出目录,避免报错
- format: 'esm', // 输出格式为 ES 模块
- sourcemap: true // 开启 sourcemap 以便调试
- },
- plugins: [
- resolve(), // 解析 node_modules 中的依赖
- commonjs(), // 允许加载 CommonJS 模块
- typescript(), // 处理 TypeScript 文件
- serve({
- open: true, // 自动打开浏览器
- contentBase: ['dist', 'src'], // 包含 dist 和 src 作为静态资源的根目录
- host: '0.0.0.0', // 可以通过局域网访问
- port: 8099, // 服务器运行端口
- openPage: '/sdk.html' // 直接打开的页面路径,src/sdk.html
- })
- ],
- watch: {
- include: 'src/**' // 监视 src 文件夹中的文件变动,但不自动打包
- }
- }
- ]
|