rollup.config.js 2.4 KB

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