rollup.examples.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 (.*) from '(.*)\/libs\/(.*)';/g, '' );
  75. // remove newline at the start of file
  76. code = code.trimStart();
  77. code = `( function () {${EOL}${code}${EOL}} )();`;
  78. return {
  79. code: code,
  80. map: null
  81. };
  82. }
  83. };
  84. }
  85. const jsFolder = path.resolve( __dirname, '../../examples/js' );
  86. const jsmFolder = path.resolve( __dirname, '../../examples/jsm' );
  87. // list of all .js file nested in the examples/jsm folder
  88. const files = glob.sync( '**/*.js', { cwd: jsmFolder, ignore: [
  89. // don't convert libs
  90. 'libs/**/*',
  91. 'loaders/ifc/**/*',
  92. // no non-module library
  93. // https://unpkg.com/browse/@webxr-input-profiles/[email protected]/dist/
  94. 'webxr/**/*',
  95. // no non-module library
  96. // https://unpkg.com/browse/[email protected]/
  97. 'loaders/IFCLoader.js',
  98. // no non-module library
  99. // https://unpkg.com/browse/[email protected]/dist/
  100. 'loaders/KTX2Loader.js',
  101. 'renderers/webgl/**/*',
  102. 'renderers/webgpu/**/*',
  103. 'renderers/nodes/**/*',
  104. 'nodes/**/*',
  105. 'loaders/NodeMaterialLoader.js',
  106. 'offscreen/**/*',
  107. ] } );
  108. // Create a rollup config for each .js file
  109. export default files.map( file => {
  110. const inputPath = path.join( 'examples/jsm', file );
  111. const outputPath = path.resolve( jsFolder, file );
  112. return {
  113. input: inputPath,
  114. treeshake: false,
  115. external: () => true, // don't bundle anything
  116. plugins: [
  117. babel( {
  118. babelHelpers: 'bundled',
  119. babelrc: false,
  120. ...babelrc
  121. } ),
  122. babelCleanup(),
  123. unmodularize(),
  124. ],
  125. output: {
  126. format: 'esm',
  127. file: outputPath,
  128. }
  129. };
  130. } );