rollup.config.js 4.7 KB

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