rollup.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import babel from '@rollup/plugin-babel';
  2. import { terser } from 'rollup-plugin-terser';
  3. if ( String.prototype.replaceAll === undefined ) {
  4. String.prototype.replaceAll = function ( find, replace ) {
  5. return this.split( find ).join( replace );
  6. };
  7. }
  8. function glconstants() {
  9. var constants = {
  10. POINTS: 0, ZERO: 0,
  11. LINES: 1, ONE: 1,
  12. LINE_LOOP: 2,
  13. LINE_STRIP: 3,
  14. TRIANGLES: 4,
  15. TRIANGLE_STRIP: 5,
  16. TRIANGLE_FAN: 6,
  17. DEPTH_BUFFER_BIT: 256,
  18. LESS: 513,
  19. NEVER: 512,
  20. EQUAL: 514,
  21. LEQUAL: 515,
  22. GREATER: 516,
  23. NOTEQUAL: 517,
  24. GEQUAL: 518,
  25. ALWAYS: 519,
  26. SRC_COLOR: 768,
  27. ONE_MINUS_SRC_COLOR: 769,
  28. SRC_ALPHA: 770,
  29. ONE_MINUS_SRC_ALPHA: 771,
  30. DST_ALPHA: 772,
  31. ONE_MINUS_DST_ALPHA: 773,
  32. DST_COLOR: 774,
  33. ONE_MINUS_DST_COLOR: 775,
  34. SRC_ALPHA_SATURATE: 776,
  35. STENCIL_BUFFER_BIT: 1024,
  36. FRONT: 1028,
  37. BACK: 1029,
  38. FRONT_AND_BACK: 1032,
  39. CW: 2304,
  40. CCW: 2305,
  41. CULL_FACE: 2884,
  42. DEPTH_TEST: 2929,
  43. STENCIL_TEST: 2960,
  44. BLEND: 3042,
  45. SCISSOR_TEST: 3089,
  46. UNPACK_ALIGNMENT: 3317,
  47. MAX_TEXTURE_SIZE: 3379,
  48. DEPTH24_STENCIL8: 35056,
  49. TEXTURE_2D: 3553,
  50. BYTE: 5120,
  51. UNSIGNED_BYTE: 5121,
  52. SHORT: 5122,
  53. UNSIGNED_INT: 5125,
  54. UNSIGNED_SHORT: 5123,
  55. INT: 5124,
  56. FLOAT: 5126,
  57. HALF_FLOAT: 5131,
  58. DEPTH_COMPONENT: 6402,
  59. RED: 6403,
  60. ALPHA: 6406,
  61. RGB: 6407,
  62. RGBA: 6408,
  63. LUMINANCE: 6409,
  64. LUMINANCE_ALPHA: 6410,
  65. RED_INTEGER: 36244,
  66. RG: 33319,
  67. RG_INTEGER: 33320,
  68. RGB_INTEGER: 36248,
  69. RGBA_INTEGER: 36249,
  70. VERSION: 7938,
  71. NEAREST: 9728,
  72. LINEAR: 9729,
  73. NEAREST_MIPMAP_NEAREST: 9984,
  74. LINEAR_MIPMAP_NEAREST: 9985,
  75. NEAREST_MIPMAP_LINEAR: 9986,
  76. LINEAR_MIPMAP_LINEAR: 9987,
  77. TEXTURE_MAG_FILTER: 10240,
  78. TEXTURE_MIN_FILTER: 10241,
  79. TEXTURE_WRAP_S: 10242,
  80. TEXTURE_WRAP_T: 10243,
  81. TEXTURE_WRAP_R: 32882,
  82. REPEAT: 10497,
  83. COLOR_BUFFER_BIT: 16384,
  84. FUNC_ADD: 32774,
  85. MIN: 32775,
  86. MAX: 32776,
  87. FUNC_SUBTRACT: 32778,
  88. FUNC_REVERSE_SUBTRACT: 32779,
  89. UNSIGNED_SHORT_4_4_4_4: 32819,
  90. UNSIGNED_SHORT_5_5_5_1: 32820,
  91. POLYGON_OFFSET_FILL: 32823,
  92. RGB8: 32849,
  93. RGBA4: 32854,
  94. RGBA8: 32856,
  95. TEXTURE_3D: 32879,
  96. CLAMP_TO_EDGE: 33071,
  97. DEPTH_COMPONENT16: 33189,
  98. DEPTH_COMPONENT24: 33190,
  99. DEPTH_COMPONENT32F: 36012,
  100. DEPTH_STENCIL_ATTACHMENT: 33306,
  101. R8: 33321,
  102. R16F: 33325,
  103. R32F: 33326,
  104. UNSIGNED_SHORT_5_6_5: 33635,
  105. MIRRORED_REPEAT: 33648,
  106. TEXTURE0: 33984,
  107. DEPTH_STENCIL: 34041,
  108. UNSIGNED_INT_24_8: 34042,
  109. TEXTURE_CUBE_MAP: 34067,
  110. TEXTURE_CUBE_MAP_POSITIVE_X: 34069,
  111. MAX_CUBE_MAP_TEXTURE_SIZE: 34076,
  112. COMPRESSED_TEXTURE_FORMATS: 34467,
  113. RGBA32F: 34836,
  114. RGB32F: 34837,
  115. RGBA16F: 34842,
  116. RGB16F: 34843,
  117. MAX_VERTEX_ATTRIBS: 34921,
  118. MAX_TEXTURE_IMAGE_UNITS: 34930,
  119. ARRAY_BUFFER: 34962,
  120. ELEMENT_ARRAY_BUFFER: 34963,
  121. STATIC_DRAW: 35044,
  122. DYNAMIC_DRAW: 35048,
  123. VERTEX_SHADER: 35633,
  124. FRAGMENT_SHADER: 35632,
  125. MAX_VERTEX_TEXTURE_IMAGE_UNITS: 35660,
  126. MAX_COMBINED_TEXTURE_IMAGE_UNITS: 35661,
  127. COMPILE_STATUS: 35713,
  128. LINK_STATUS: 35714,
  129. VALIDATE_STATUS: 35715,
  130. ACTIVE_UNIFORMS: 35718,
  131. ACTIVE_ATTRIBUTES: 35721,
  132. IMPLEMENTATION_COLOR_READ_TYPE: 35738,
  133. IMPLEMENTATION_COLOR_READ_FORMAT: 35739,
  134. TEXTURE_2D_ARRAY: 35866,
  135. COLOR_ATTACHMENT0: 36064,
  136. FRAMEBUFFER_COMPLETE: 36053,
  137. DEPTH_ATTACHMENT: 36096,
  138. FRAMEBUFFER: 36160,
  139. RENDERBUFFER: 36161,
  140. LOW_FLOAT: 36336,
  141. MEDIUM_FLOAT: 36337,
  142. HIGH_FLOAT: 36338,
  143. MAX_VERTEX_UNIFORM_VECTORS: 36347,
  144. MAX_VARYING_VECTORS: 36348,
  145. MAX_FRAGMENT_UNIFORM_VECTORS: 36349,
  146. UNPACK_FLIP_Y_WEBGL: 37440,
  147. UNPACK_PREMULTIPLY_ALPHA_WEBGL: 37441,
  148. MAX_SAMPLES: 36183,
  149. READ_FRAMEBUFFER: 36008,
  150. DRAW_FRAMEBUFFER: 36009
  151. };
  152. return {
  153. transform( code ) {
  154. code = code.replace( /_?gl\.([A-Z0-9_]+)/g, function ( match, p1 ) {
  155. if ( p1 in constants ) return constants[ p1 ];
  156. console.log( '* Unhandled GL Constant:', p1 );
  157. return match;
  158. } );
  159. return {
  160. code: code,
  161. map: null
  162. };
  163. }
  164. };
  165. }
  166. function addons() {
  167. return {
  168. transform( code, id ) {
  169. if ( /\/examples\/jsm\//.test( id ) === false ) return;
  170. code = code.replace( 'build/three.module.js', 'src/Three.js' );
  171. return {
  172. code: code,
  173. map: null
  174. };
  175. }
  176. };
  177. }
  178. function glsl() {
  179. return {
  180. transform( code, id ) {
  181. if ( /\.glsl.js$/.test( id ) === false ) return;
  182. code = code.replace( /\/\* glsl \*\/\`((.|\r|\n)*)\`/, function ( match, p1 ) {
  183. return JSON.stringify(
  184. p1
  185. .trim()
  186. .replace( /\r/g, '' )
  187. .replace( /[ \t]*\/\/.*\n/g, '' ) // remove //
  188. .replace( /[ \t]*\/\*[\s\S]*?\*\//g, '' ) // remove /* */
  189. .replace( /\n{2,}/g, '\n' ) // # \n+ to \n
  190. );
  191. } );
  192. return {
  193. code: code,
  194. map: null
  195. };
  196. }
  197. };
  198. }
  199. function babelCleanup() {
  200. const doubleSpaces = / {2}/g;
  201. return {
  202. transform( code ) {
  203. code = code.replace( doubleSpaces, '\t' );
  204. return {
  205. code: code,
  206. map: null
  207. };
  208. }
  209. };
  210. }
  211. function header() {
  212. return {
  213. renderChunk( code ) {
  214. return '// threejs.org/license\n' + code;
  215. }
  216. };
  217. }
  218. function polyfills() {
  219. return {
  220. transform( code, filePath ) {
  221. if ( filePath.endsWith( 'src/Three.js' ) || filePath.endsWith( 'src\\Three.js' ) ) {
  222. code = 'import \'./polyfills\';\n' + code;
  223. }
  224. return {
  225. code: code,
  226. map: null
  227. };
  228. }
  229. };
  230. }
  231. const babelrc = {
  232. presets: [
  233. [
  234. '@babel/preset-env',
  235. {
  236. modules: false,
  237. // the supported browsers of the three.js browser bundle
  238. // https://browsersl.ist/?q=%3E0.3%25%2C+not+dead
  239. targets: '>0.3%, not dead',
  240. loose: true,
  241. bugfixes: true,
  242. },
  243. ],
  244. ],
  245. plugins: [
  246. [
  247. '@babel/plugin-proposal-class-properties',
  248. {
  249. loose: true
  250. }
  251. ]
  252. ]
  253. };
  254. export default [
  255. {
  256. input: 'src/Three.js',
  257. plugins: [
  258. polyfills(),
  259. addons(),
  260. glconstants(),
  261. glsl(),
  262. babel( {
  263. babelHelpers: 'bundled',
  264. compact: false,
  265. babelrc: false,
  266. ...babelrc
  267. } ),
  268. babelCleanup(),
  269. header()
  270. ],
  271. output: [
  272. {
  273. format: 'umd',
  274. name: 'THREE',
  275. file: 'build/three.js',
  276. indent: '\t'
  277. }
  278. ]
  279. },
  280. {
  281. input: 'src/Three.js',
  282. plugins: [
  283. polyfills(),
  284. addons(),
  285. glconstants(),
  286. glsl(),
  287. babel( {
  288. babelHelpers: 'bundled',
  289. babelrc: false,
  290. ...babelrc
  291. } ),
  292. babelCleanup(),
  293. terser(),
  294. header()
  295. ],
  296. output: [
  297. {
  298. format: 'umd',
  299. name: 'THREE',
  300. file: 'build/three.min.js'
  301. }
  302. ]
  303. },
  304. {
  305. input: 'src/Three.js',
  306. plugins: [
  307. addons(),
  308. glconstants(),
  309. glsl(),
  310. header()
  311. ],
  312. output: [
  313. {
  314. format: 'esm',
  315. file: 'build/three.module.js'
  316. }
  317. ]
  318. }
  319. ];