rollup.examples.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import babel from '@rollup/plugin-babel';
  2. import path from 'path';
  3. import os from 'os';
  4. import glob from 'glob';
  5. import babelrc from './.babelrc.json';
  6. const EOL = os.EOL;
  7. function babelCleanup() {
  8. return {
  9. transform( code ) {
  10. // remove comments messed up by babel that break eslint
  11. // example:
  12. // setSize: function ()
  13. // /* width, height */
  14. // {
  15. // ↓
  16. // setSize: function () {
  17. code = code.replace( new RegExp( `\\(\\)${EOL}\\s*\\/\\*([a-zA-Z0-9_, ]+)\\*\\/${EOL}\\s*{`, 'g' ), '( ) {' );
  18. return {
  19. code: code,
  20. map: null
  21. };
  22. }
  23. };
  24. }
  25. function unmodularize() {
  26. return {
  27. renderChunk( code ) {
  28. // export { Example };
  29. // ↓
  30. // THREE.Example = Example;
  31. code = code.replace( /export { ([a-zA-Z0-9_, ]+) };/g, ( match, p1 ) => {
  32. const exps = p1.split( ', ' );
  33. return exps.map( exp => `THREE.${exp} = ${exp};` ).join( EOL );
  34. } );
  35. // import { Example } from '...';
  36. // but excluding imports importing from the libs/ folder
  37. const imports = [];
  38. code = code.replace( /import { ([a-zA-Z0-9_, ]+) } from '((?!libs).)*';/g, ( match, p1 ) => {
  39. const imps = p1.split( ', ' );
  40. imps.reverse();
  41. imports.push( ...imps );
  42. return '';
  43. } );
  44. // new Example()
  45. // (Example)
  46. // [Example]
  47. // Example2
  48. // ↓
  49. // new THREE.Example()
  50. // (THREE.Example)
  51. // [THREE.Example]
  52. // Example2
  53. function prefixThree( word ) {
  54. code = code.replace( new RegExp( `([\\s([!])${word}([^a-zA-Z0-9_])`, 'g' ), ( match, p1, p2 ) => {
  55. return `${p1}THREE.${word}${p2}`;
  56. } );
  57. }
  58. imports.forEach( prefixThree );
  59. // Do it again for this particular example
  60. // new Example(Example)
  61. // ↓
  62. // new THREE.Example(THREE.Example)
  63. imports.forEach( prefixThree );
  64. // fix for BasisTextureLoader.js
  65. imports.forEach( imp => {
  66. code = code.replace( new RegExp( `${EOL}(\\s)*THREE\\.${imp}:`, 'g' ), ( match, p1 ) => {
  67. return `${EOL}${p1}${imp}:`;
  68. } );
  69. } );
  70. // import * as THREE from '...';
  71. code = code.replace( /import \* as THREE from '(.*)';/g, '' );
  72. // Remove library imports that are exposed as
  73. // global variables in the non-module world
  74. code = code.replace( 'import * as fflate from \'../libs/fflate.module.js\';', '' );
  75. code = code.replace( 'import { MMDParser } from \'../libs/mmdparser.module.js\';', '' );
  76. code = code.replace( 'import { potpack } from \'../libs/potpack.module.js\';', '' );
  77. code = code.replace( 'import { opentype } from \'../libs/opentype.module.min.js\';', '' );
  78. code = code.replace( 'import chevrotain from \'../libs/chevrotain.module.min.js\';', '' );
  79. code = code.replace( 'import { ZSTDDecoder } from \'../libs/zstddec.module.js\';', '' );
  80. // remove newline at the start of file
  81. code = code.trimStart();
  82. code = `( function () {${EOL}${code}${EOL}} )();`;
  83. return {
  84. code: code,
  85. map: null
  86. };
  87. }
  88. };
  89. }
  90. const jsFolder = path.resolve( __dirname, '../../examples/js' );
  91. const jsmFolder = path.resolve( __dirname, '../../examples/jsm' );
  92. // list of all .js file nested in the examples/jsm folder
  93. const files = glob.sync( '**/*.js', { cwd: jsmFolder, ignore: [
  94. // don't convert libs
  95. 'libs/**/*',
  96. 'loaders/ifc/**/*',
  97. // no non-module library
  98. // https://unpkg.com/browse/@webxr-input-profiles/[email protected]/dist/
  99. 'webxr/**/*',
  100. // no non-module library
  101. // https://unpkg.com/browse/[email protected]/
  102. 'loaders/IFCLoader.js',
  103. // no non-module library
  104. // https://unpkg.com/browse/[email protected]/dist/
  105. 'loaders/KTX2Loader.js',
  106. 'renderers/webgl/**/*',
  107. 'renderers/webgpu/**/*',
  108. 'renderers/nodes/**/*',
  109. 'nodes/**/*',
  110. 'loaders/NodeMaterialLoader.js',
  111. 'offscreen/**/*',
  112. ] } );
  113. // Create a rollup config for each .js file
  114. export default files.map( file => {
  115. const inputPath = path.join( 'examples/jsm', file );
  116. const outputPath = path.resolve( jsFolder, file );
  117. return {
  118. input: inputPath,
  119. treeshake: false,
  120. external: () => true, // don't bundle anything
  121. plugins: [
  122. babel( {
  123. babelHelpers: 'bundled',
  124. babelrc: false,
  125. ...babelrc
  126. } ),
  127. babelCleanup(),
  128. unmodularize(),
  129. ],
  130. output: {
  131. format: 'esm',
  132. file: outputPath,
  133. }
  134. };
  135. } );