rollup.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-2024 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. /**
  42. * @type {Array<import('rollup').RollupOptions>}
  43. */
  44. const builds = [
  45. {
  46. input: {
  47. 'three.core.js': 'src/Three.core.js',
  48. 'three.webgpu.nodes.js': 'src/Three.WebGPU.Nodes.js',
  49. },
  50. plugins: [
  51. glsl(),
  52. header()
  53. ],
  54. preserveEntrySignatures: 'allow-extension',
  55. output: [
  56. {
  57. format: 'esm',
  58. dir: 'build',
  59. minifyInternalExports: false,
  60. entryFileNames: '[name]',
  61. }
  62. ]
  63. },
  64. {
  65. input: {
  66. 'three.core.js': 'src/Three.core.js',
  67. 'three.module.js': 'src/Three.js',
  68. 'three.webgpu.js': 'src/Three.WebGPU.js',
  69. },
  70. plugins: [
  71. glsl(),
  72. header()
  73. ],
  74. preserveEntrySignatures: 'allow-extension',
  75. output: [
  76. {
  77. format: 'esm',
  78. dir: 'build',
  79. minifyInternalExports: false,
  80. entryFileNames: '[name]',
  81. }
  82. ]
  83. },
  84. {
  85. input: {
  86. 'three.core.min.js': 'src/Three.core.js',
  87. 'three.webgpu.nodes.min.js': 'src/Three.WebGPU.Nodes.js',
  88. },
  89. plugins: [
  90. glsl(),
  91. header(),
  92. terser()
  93. ],
  94. preserveEntrySignatures: 'allow-extension',
  95. output: [
  96. {
  97. format: 'esm',
  98. dir: 'build',
  99. minifyInternalExports: false,
  100. entryFileNames: '[name]',
  101. }
  102. ]
  103. },
  104. {
  105. input: {
  106. 'three.core.min.js': 'src/Three.core.js',
  107. 'three.module.min.js': 'src/Three.js',
  108. 'three.webgpu.min.js': 'src/Three.WebGPU.js',
  109. },
  110. plugins: [
  111. glsl(),
  112. header(),
  113. terser()
  114. ],
  115. preserveEntrySignatures: 'allow-extension',
  116. output: [
  117. {
  118. format: 'esm',
  119. dir: 'build',
  120. minifyInternalExports: false,
  121. entryFileNames: '[name]',
  122. }
  123. ]
  124. },
  125. {
  126. input: 'src/Three.js',
  127. plugins: [
  128. glsl(),
  129. header()
  130. ],
  131. output: [
  132. {
  133. format: 'cjs',
  134. name: 'THREE',
  135. file: 'build/three.cjs',
  136. indent: '\t'
  137. }
  138. ]
  139. }
  140. ];
  141. export default ( args ) => args.configOnlyModule ? builds.slice( 0, 4 ) : builds;
粤ICP备19079148号