rollup.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import serve from 'rollup-plugin-serve'
  2. import typescript from '@rollup/plugin-typescript'
  3. import resolve from '@rollup/plugin-node-resolve'
  4. import commonjs from '@rollup/plugin-commonjs'
  5. import autoExternal from 'rollup-plugin-auto-external'
  6. import terser from '@rollup/plugin-terser'
  7. import babel from '@rollup/plugin-babel'
  8. import dts from 'rollup-plugin-dts'
  9. import json from './package.json' assert { type: 'json' }
  10. import { execSync } from 'child_process'
  11. // 需要 build 的目录
  12. const model = process.env.model
  13. function executeCommand(command) {
  14. return execSync(command, { encoding: 'utf-8' }).trim()
  15. }
  16. // sdk 的版本号
  17. const version = json.version
  18. // sdk build 时间戳
  19. const newDate = new Date()
  20. // build 时的 git 信息
  21. const currentBranch = executeCommand('git rev-parse --abbrev-ref HEAD')
  22. const latestCommitID = executeCommand('git rev-parse HEAD')
  23. const latestCommitMessage = executeCommand('git log -1 --pretty=%B')
  24. // sdk 的头部信息
  25. const getBannerText = (module, standard) => {
  26. return `/*!
  27. - Name ${module.toUpperCase()}_CTI
  28. - FileName ${module}-cti
  29. - Version ${version}
  30. - JS Standard ${standard}
  31. - Author ${json.author}
  32. - Built on ${newDate.toLocaleDateString()} ${newDate.toLocaleTimeString()}
  33. - GitHub ${json.repository.url}
  34. - Branch ${currentBranch}
  35. - CommitID ${latestCommitID}
  36. - CommitMessage ${latestCommitMessage}
  37. */`
  38. }
  39. // 获取输出配置
  40. const getCommonOutput = (module, format, standard, terser) => {
  41. return {
  42. format,
  43. name: `${module.toUpperCase()}_CTI`,
  44. globals: {
  45. 'socket.io-client': 'io',
  46. 'sip.js': 'SIP'
  47. },
  48. exports: 'named',
  49. file: `dist/${module}-cti.${standard}.${format}.${terser ? 'prod.' : ''}js`,
  50. banner: getBannerText(module, standard),
  51. sourcemap: !!process.env.sourcemap
  52. }
  53. }
  54. // 获取插件配置
  55. const getCommonPlugin = standard => {
  56. return [
  57. resolve(),
  58. commonjs(),
  59. autoExternal(),
  60. typescript({
  61. tsconfig: `tsconfig.${standard}.json`
  62. }),
  63. babel({
  64. extensions: ['.js', '.ts'],
  65. babelHelpers: 'bundled',
  66. presets: ['@babel/preset-env']
  67. })
  68. ]
  69. }
  70. // 需要生成的格式配置
  71. const configArr = [
  72. { format: 'umd', standard: 'es5' },
  73. { format: 'umd', standard: 'es6' },
  74. { format: 'esm', standard: 'es5' },
  75. { format: 'esm', standard: 'es6' }
  76. ]
  77. // 后续增加其他的需手动增加
  78. const buildModuleList = ['hs']
  79. const generateExport = () => {
  80. // 如果指定了需要打包的模块,则单独打包
  81. if (model) {
  82. return buildModel(model)
  83. } else {
  84. const exportAll = []
  85. // 若未指定,则打包所有的模块
  86. buildModuleList.forEach(module => {
  87. exportAll.push(...buildModel(module))
  88. })
  89. return exportAll
  90. }
  91. }
  92. function buildModel(module) {
  93. const exportArr = []
  94. // 类型声明文件
  95. exportArr.push({
  96. input: `src/${module}-cti/index.dts.ts`,
  97. plugins: [dts()],
  98. output: {
  99. banner: getBannerText(module, 'any'),
  100. file: `dist/${module}-cti.d.ts`,
  101. format: 'es'
  102. }
  103. })
  104. // 统一输出 js 模块的入口文件
  105. const input = `src/${module}-cti/index.ts`
  106. // 分别生成不混淆和混淆两版
  107. configArr.forEach(item => {
  108. exportArr.push(
  109. {
  110. input,
  111. output: getCommonOutput(module, item.format, item.standard, false),
  112. plugins: getCommonPlugin(item.standard)
  113. },
  114. {
  115. input,
  116. output: getCommonOutput(module, item.format, item.standard, true),
  117. plugins: [...getCommonPlugin(item.standard), terser()]
  118. }
  119. )
  120. })
  121. return exportArr
  122. }
  123. // export default generateExport()
  124. export default [
  125. ...generateExport(),
  126. {
  127. input: 'src/cs-cti/index.ts', // 指定一个任意入口,防止报错,可以是一个空的文件
  128. output: {
  129. dir: 'dist', // 输出目录,避免报错
  130. format: 'esm', // 输出格式为 ES 模块
  131. sourcemap: true // 开启 sourcemap 以便调试
  132. },
  133. plugins: [
  134. resolve(), // 解析 node_modules 中的依赖
  135. commonjs(), // 允许加载 CommonJS 模块
  136. typescript(), // 处理 TypeScript 文件
  137. serve({
  138. open: true, // 自动打开浏览器
  139. contentBase: ['dist', 'src'], // 包含 dist 和 src 作为静态资源的根目录
  140. host: '0.0.0.0', // 可以通过局域网访问
  141. port: 8099, // 服务器运行端口
  142. openPage: '/sdk.html' // 直接打开的页面路径,src/sdk.html
  143. })
  144. ],
  145. watch: {
  146. include: 'src/**' // 监视 src 文件夹中的文件变动,但不自动打包
  147. }
  148. }
  149. ]