rollup.config.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 \*\/\`((.|\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 wrappedClass = /(var\s*(\w+) = \/\*#__PURE__\*\/function \((\w+)?\) {\s*).*(return \2;\s*}\((\w+)?\);)/gs;
  201. const inheritsLoose = /_inheritsLoose\((\w+), (\w+)\);\n/;
  202. const doubleSpaces = / {2}/g;
  203. const danglingTabs = /(^\t+$\n)|(\n^\t+$)/gm;
  204. const commentOutside = /function (\w+)?\(\)\s*\/\*(.*)\*\/\s*{/g;
  205. function unwrap( match, wrapperStart, klass, _parentClass, wrapperEnd, parentClass ) {
  206. return match
  207. .replace( wrapperStart, '' )
  208. .replace( inheritsLoose, '' )
  209. .replace( wrapperEnd, '' )
  210. .replaceAll( _parentClass, parentClass );
  211. }
  212. function commentInside( match, functionName = '', comment ) {
  213. return `function ${functionName}(/*${comment}*/) {`;
  214. }
  215. return {
  216. transform( code ) {
  217. while ( wrappedClass.test( code ) ) {
  218. code = code
  219. .replace( wrappedClass, unwrap );
  220. }
  221. code = code
  222. .replace( commentOutside, commentInside )
  223. .replace( doubleSpaces, '\t' )
  224. .replace( danglingTabs, '' );
  225. return {
  226. code: code,
  227. map: null
  228. };
  229. }
  230. };
  231. }
  232. function header() {
  233. return {
  234. renderChunk( code ) {
  235. return "// threejs.org/license\n" + code;
  236. }
  237. };
  238. }
  239. function polyfills() {
  240. return {
  241. transform( code, filePath ) {
  242. if ( filePath.endsWith( 'src/Three.js' ) ) {
  243. code = "import './polyfills';\n" + code;
  244. }
  245. return {
  246. code: code,
  247. map: null
  248. };
  249. }
  250. };
  251. }
  252. const babelrc = {
  253. presets: [
  254. [
  255. '@babel/preset-env',
  256. {
  257. modules: false,
  258. // the supported browsers of the three.js browser bundle
  259. // https://browsersl.ist/?q=%3E0.3%25%2C+not+dead
  260. targets: '>0.3%, not dead',
  261. loose: true,
  262. bugfixes: true,
  263. },
  264. ],
  265. ],
  266. plugins: [
  267. [
  268. '@babel/plugin-proposal-class-properties',
  269. {
  270. loose: true
  271. }
  272. ]
  273. ]
  274. };
  275. export default [
  276. {
  277. input: 'src/Three.js',
  278. plugins: [
  279. polyfills(),
  280. addons(),
  281. glconstants(),
  282. glsl(),
  283. babel( {
  284. babelHelpers: 'bundled',
  285. compact: false,
  286. babelrc: false,
  287. ...babelrc
  288. } ),
  289. babelCleanup(),
  290. header()
  291. ],
  292. output: [
  293. {
  294. format: 'umd',
  295. name: 'THREE',
  296. file: 'build/three.js',
  297. indent: '\t'
  298. }
  299. ]
  300. },
  301. {
  302. input: 'src/Three.js',
  303. plugins: [
  304. polyfills(),
  305. addons(),
  306. glconstants(),
  307. glsl(),
  308. babel( {
  309. babelHelpers: 'bundled',
  310. babelrc: false,
  311. ...babelrc
  312. } ),
  313. babelCleanup(),
  314. terser(),
  315. header()
  316. ],
  317. output: [
  318. {
  319. format: 'umd',
  320. name: 'THREE',
  321. file: 'build/three.min.js'
  322. }
  323. ]
  324. },
  325. {
  326. input: 'src/Three.js',
  327. plugins: [
  328. addons(),
  329. glconstants(),
  330. glsl(),
  331. header()
  332. ],
  333. output: [
  334. {
  335. format: 'esm',
  336. file: 'build/three.module.js'
  337. }
  338. ]
  339. }
  340. ];