modularize.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var fs = require( 'fs' );
  5. var srcFolder = __dirname + '/../examples/js/';
  6. var dstFolder = __dirname + '/../examples/jsm/';
  7. var files = [
  8. { path: 'controls/OrbitControls.js', ignoreList: [] },
  9. { path: 'controls/MapControls.js', ignoreList: [] },
  10. { path: 'controls/TrackballControls.js', ignoreList: [] },
  11. // { path: 'controls/TransformControls.js', ignoreList: [] },
  12. { path: 'exporters/GLTFExporter.js', ignoreList: [ 'AnimationClip', 'Camera', 'Geometry', 'Material', 'Mesh', 'Object3D', 'RGBFormat', 'Scenes', 'ShaderMaterial', 'VertexColors' ] },
  13. { path: 'exporters/MMDExporter.js', ignoreList: [] },
  14. { path: 'exporters/OBJExporter.js', ignoreList: [] },
  15. { path: 'exporters/PLYExporter.js', ignoreList: [] },
  16. { path: 'exporters/STLExporter.js', ignoreList: [] },
  17. { path: 'exporters/TypedGeometryExporter.js', ignoreList: [] },
  18. { path: 'loaders/GLTFLoader.js', ignoreList: [ 'NoSide', 'Matrix2', 'DDSLoader' ] },
  19. { path: 'loaders/OBJLoader.js', ignoreList: [] },
  20. { path: 'loaders/MTLLoader.js', ignoreList: [] },
  21. { path: 'pmrem/PMREMCubeUVPacker.js', ignoreList: [] },
  22. { path: 'pmrem/PMREMGenerator.js', ignoreList: [] },
  23. { path: 'utils/BufferGeometryUtils.js', ignoreList: [] },
  24. { path: 'utils/GeometryUtils.js', ignoreList: [] },
  25. { path: 'utils/MathUtils.js', ignoreList: [] },
  26. { path: 'utils/SceneUtils.js', ignoreList: [] },
  27. { path: 'utils/ShadowMapViewer.js', ignoreList: [ 'DirectionalLight', 'SpotLight' ] },
  28. { path: 'utils/SkeletonUtils.js', ignoreList: [] },
  29. { path: 'utils/TypedArrayUtils.js', ignoreList: [] },
  30. { path: 'utils/UVsDebug.js', ignoreList: [ 'SphereBufferGeometry' ] },
  31. ];
  32. for ( var i = 0; i < files.length; i ++ ) {
  33. var file = files[ i ];
  34. convert( file.path, file.ignoreList );
  35. }
  36. //
  37. function convert( path, ignoreList ) {
  38. var contents = fs.readFileSync( srcFolder + path, 'utf8' );
  39. var className = '';
  40. var dependencies = {};
  41. // imports
  42. contents = contents.replace( /^\/\*+[^*]*\*+(?:[^/*][^*]*\*+)*\//, function ( match ) {
  43. return `${match}\n\n_IMPORTS_`;
  44. } );
  45. // class name
  46. contents = contents.replace( /THREE\.([a-zA-Z0-9]+) = /g, function ( match, p1 ) {
  47. className = p1;
  48. console.log( className );
  49. return `var ${p1} = `;
  50. } );
  51. contents = contents.replace( /(\'?)THREE\.([a-zA-Z0-9]+)(\.{0,1})/g, function ( match, p1, p2, p3 ) {
  52. if ( p1 === '\'' ) return match; // Inside a string
  53. if ( p2 === className ) return `${p2}${p3}`;
  54. if ( p1 === 'Math' ) {
  55. dependencies[ '_Math' ] = true;
  56. return '_Math.';
  57. }
  58. return match;
  59. } );
  60. // methods
  61. contents = contents.replace( /new THREE\.([a-zA-Z0-9]+)\(/g, function ( match, p1 ) {
  62. if ( ignoreList.includes( p1 ) ) return match;
  63. dependencies[ p1 ] = true;
  64. return `new ${p1}(`;
  65. } );
  66. // constants
  67. contents = contents.replace( /(\'?)THREE\.([a-zA-Z0-9]+)/g, function ( match, p1, p2 ) {
  68. if ( ignoreList.includes( p2 ) ) return match;
  69. if ( p1 === '\'' ) return match; // Inside a string
  70. if ( p2 === className ) return p2;
  71. if ( p2 === 'Math' || p2 === '_Math' ) {
  72. dependencies[ '_Math' ] = true;
  73. return '_Math';
  74. }
  75. dependencies[ p2 ] = true;
  76. // console.log( match, p2 );
  77. return `${p2}`;
  78. } );
  79. //
  80. var keys = Object.keys( dependencies )
  81. .filter( value => value !== className )
  82. .map( value => value === '_Math' ? 'Math as _Math' : value )
  83. .map( value => '\n\t' + value )
  84. .sort()
  85. .toString();
  86. var imports = `import {${keys}\n} from "../../../build/three.module.js";`;
  87. var exports = `export { ${className} };\n`;
  88. var output = contents.replace( '_IMPORTS_', keys ? imports : '' ) + '\n' + exports;
  89. // console.log( output );
  90. fs.writeFileSync( dstFolder + path, output, 'utf-8' );
  91. };