rollup.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { nodeResolve } from '@rollup/plugin-node-resolve';
  2. import { terser } from 'rollup-plugin-terser';
  3. import babel from '@rollup/plugin-babel';
  4. import {version} from './package.json'
  5. const injectVersion = (options = {}) => {
  6. return {
  7. name: 'inject-version',
  8. renderChunk: (code) => {
  9. return code.replace('[[BM_VERSION]]', version)
  10. },
  11. }
  12. }
  13. const addNavigatorValidation = (options = {}) => {
  14. return {
  15. name: 'inject-version',
  16. renderChunk: (code) => {
  17. return '(typeof navigator !== "undefined") && ' + code
  18. },
  19. }
  20. }
  21. const noTreeShakingForStandalonePlugin = () => {
  22. return {
  23. name: 'no-treeshaking-for-standalone',
  24. transform(code) {
  25. // This is very fast but can produce lots of false positives.
  26. // Use a good regular expression or parse an AST and analyze scoping to improve as needed.
  27. if (code.indexOf('__[STANDALONE]__') >= 0) return {moduleSideEffects: 'no-treeshake'};
  28. }
  29. }
  30. }
  31. const destinationBuildFolder = 'build/player/';
  32. const builds = [
  33. {
  34. input: 'player/js/modules/full.js',
  35. dest: `${destinationBuildFolder}`,
  36. file: 'lottie.min.js',
  37. esm: true,
  38. },
  39. {
  40. input: 'player/js/modules/full.js',
  41. dest: `${destinationBuildFolder}`,
  42. file: 'lottie.js',
  43. esm: false,
  44. skipTerser: true,
  45. },
  46. {
  47. input: 'player/js/modules/svg_light.js',
  48. dest: `${destinationBuildFolder}`,
  49. file: 'lottie_light.min.js',
  50. esm: true,
  51. },
  52. {
  53. input: 'player/js/modules/svg_light.js',
  54. dest: `${destinationBuildFolder}`,
  55. file: 'lottie_light.js',
  56. esm: false,
  57. skipTerser: true,
  58. },
  59. {
  60. input: 'player/js/modules/svg.js',
  61. dest: `${destinationBuildFolder}`,
  62. file: 'lottie_svg.min.js',
  63. esm: true,
  64. },
  65. {
  66. input: 'player/js/modules/svg.js',
  67. dest: `${destinationBuildFolder}`,
  68. file: 'lottie_svg.js',
  69. esm: false,
  70. skipTerser: true,
  71. },
  72. {
  73. input: 'player/js/modules/canvas.js',
  74. dest: `${destinationBuildFolder}`,
  75. file: 'lottie_canvas.min.js',
  76. esm: true,
  77. },
  78. {
  79. input: 'player/js/modules/canvas_light.js',
  80. dest: `${destinationBuildFolder}`,
  81. file: 'lottie_light_canvas.js',
  82. esm: false,
  83. skipTerser: true,
  84. },
  85. {
  86. input: 'player/js/modules/canvas_light.js',
  87. dest: `${destinationBuildFolder}`,
  88. file: 'lottie_light_canvas.min.js',
  89. esm: true,
  90. },
  91. {
  92. input: 'player/js/modules/canvas.js',
  93. dest: `${destinationBuildFolder}`,
  94. file: 'lottie_canvas.js',
  95. esm: false,
  96. skipTerser: true,
  97. },
  98. {
  99. input: 'player/js/modules/html_light.js',
  100. dest: `${destinationBuildFolder}`,
  101. file: 'lottie_light_html.min.js',
  102. esm: true,
  103. },
  104. {
  105. input: 'player/js/modules/html_light.js',
  106. dest: `${destinationBuildFolder}`,
  107. file: 'lottie_light_html.js',
  108. esm: false,
  109. skipTerser: true,
  110. },
  111. {
  112. input: 'player/js/modules/html.js',
  113. dest: `${destinationBuildFolder}`,
  114. file: 'lottie_html.min.js',
  115. esm: true,
  116. },
  117. {
  118. input: 'player/js/modules/html.js',
  119. dest: `${destinationBuildFolder}`,
  120. file: 'lottie_html.js',
  121. esm: false,
  122. skipTerser: true,
  123. },
  124. ];
  125. const plugins = [
  126. nodeResolve(),
  127. babel({
  128. babelHelpers: 'runtime',
  129. skipPreflightCheck: true,
  130. }),
  131. // noTreeShakingForStandalonePlugin(),
  132. injectVersion(),
  133. addNavigatorValidation(),
  134. ]
  135. const pluginsWithTerser = [
  136. ...plugins,
  137. terser(),
  138. ]
  139. const UMDModule = {
  140. output: {
  141. format: 'umd',
  142. name: 'lottie', // this is the name of the global object
  143. esModule: false,
  144. exports: 'default',
  145. sourcemap: false,
  146. compact: false,
  147. },
  148. treeshake: false,
  149. };
  150. const ESMModule = {
  151. plugins: [nodeResolve()],
  152. treeshake: false,
  153. output: [
  154. {
  155. format: 'esm',
  156. exports: 'named',
  157. },
  158. {
  159. format: 'cjs',
  160. exports: 'named',
  161. },
  162. ],
  163. };
  164. const exports = builds.reduce((acc, build) => {
  165. const builds = [];
  166. builds.push({
  167. ...UMDModule,
  168. plugins: !build.skipTerser ? pluginsWithTerser : plugins,
  169. input: build.input,
  170. output: {
  171. ...UMDModule.output,
  172. file: `${build.dest}${build.file}`,
  173. }
  174. });
  175. if (build.esm) {
  176. builds.push({
  177. ...ESMModule,
  178. input: build.input,
  179. output: [
  180. {
  181. ...ESMModule.output[0],
  182. file: 'dist/esm/' + build.file,
  183. file: `${destinationBuildFolder}esm/${build.file}`,
  184. },
  185. {
  186. ...ESMModule.output[1],
  187. file: `${destinationBuildFolder}cjs/${build.file}`,
  188. }
  189. ]
  190. });
  191. }
  192. acc = acc.concat(builds);
  193. return acc;
  194. }, []);
  195. export default exports;