rollup.config.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import terser from '@rollup/plugin-terser';
  2. function addons() {
  3. return {
  4. transform( code, id ) {
  5. if ( /\/examples\/jsm\//.test( id ) === false ) return;
  6. code = code.replace( 'build/three.module.js', 'src/Three.js' );
  7. return {
  8. code: code,
  9. map: null
  10. };
  11. }
  12. };
  13. }
  14. export function glsl() {
  15. return {
  16. transform( code, id ) {
  17. if ( /\.glsl.js$/.test( id ) === false ) return;
  18. code = code.replace( /\/\* glsl \*\/\`(.*?)\`/sg, function ( match, p1 ) {
  19. return JSON.stringify(
  20. p1
  21. .trim()
  22. .replace( /\r/g, '' )
  23. .replace( /[ \t]*\/\/.*\n/g, '' ) // remove //
  24. .replace( /[ \t]*\/\*[\s\S]*?\*\//g, '' ) // remove /* */
  25. .replace( /\n{2,}/g, '\n' ) // # \n+ to \n
  26. );
  27. } );
  28. return {
  29. code: code,
  30. map: null
  31. };
  32. }
  33. };
  34. }
  35. function header() {
  36. return {
  37. renderChunk( code ) {
  38. return `/**
  39. * @license
  40. * Copyright 2010-2023 Three.js Authors
  41. * SPDX-License-Identifier: MIT
  42. */
  43. ${ code }`;
  44. }
  45. };
  46. }
  47. function deprecationWarning() {
  48. return {
  49. renderChunk( code ) {
  50. return `console.warn( 'Scripts "build/three.js" and "build/three.min.js" are deprecated with r150+, and will be removed with r160. Please use ES Modules or alternatives: https://threejs.org/docs/index.html#manual/en/introduction/Installation' );
  51. ${ code }`;
  52. }
  53. };
  54. }
  55. const builds = [
  56. {
  57. input: 'src/Three.js',
  58. plugins: [
  59. addons(),
  60. glsl(),
  61. header()
  62. ],
  63. output: [
  64. {
  65. format: 'esm',
  66. file: 'build/three.module.js'
  67. }
  68. ]
  69. },
  70. {
  71. input: 'src/Three.js',
  72. plugins: [
  73. addons(),
  74. glsl(),
  75. terser(),
  76. header()
  77. ],
  78. output: [
  79. {
  80. format: 'esm',
  81. file: 'build/three.module.min.js'
  82. }
  83. ]
  84. },
  85. {
  86. input: 'src/Three.js',
  87. plugins: [
  88. addons(),
  89. glsl(),
  90. header()
  91. ],
  92. output: [
  93. {
  94. format: 'cjs',
  95. name: 'THREE',
  96. file: 'build/three.cjs',
  97. indent: '\t'
  98. }
  99. ]
  100. },
  101. { // @deprecated, r150
  102. input: 'src/Three.js',
  103. plugins: [
  104. addons(),
  105. glsl(),
  106. header(),
  107. deprecationWarning()
  108. ],
  109. output: [
  110. {
  111. format: 'umd',
  112. name: 'THREE',
  113. file: 'build/three.js',
  114. indent: '\t'
  115. }
  116. ]
  117. },
  118. { // @deprecated, r150
  119. input: 'src/Three.js',
  120. plugins: [
  121. addons(),
  122. glsl(),
  123. terser(),
  124. header(),
  125. deprecationWarning()
  126. ],
  127. output: [
  128. {
  129. format: 'umd',
  130. name: 'THREE',
  131. file: 'build/three.min.js'
  132. }
  133. ]
  134. }
  135. ];
  136. export default ( args ) => args.configOnlyModule ? builds[ 0 ] : builds;