rollup.examples.config.js 4.2 KB

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