modularize.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/DragControls.js', ignoreList: [] },
  9. { path: 'controls/DeviceOrientationControls.js', ignoreList: [] },
  10. { path: 'controls/EditorControls.js', ignoreList: [] },
  11. { path: 'controls/FirstPersonControls.js', ignoreList: [] },
  12. { path: 'controls/FlyControls.js', ignoreList: [] },
  13. { path: 'controls/OrbitControls.js', ignoreList: [] },
  14. { path: 'controls/MapControls.js', ignoreList: [] },
  15. { path: 'controls/OrthographicTrackballControls.js', ignoreList: [] },
  16. { path: 'controls/PointerLockControls.js', ignoreList: [] },
  17. { path: 'controls/TrackballControls.js', ignoreList: [] },
  18. { path: 'controls/TransformControls.js', ignoreList: [] },
  19. { path: 'exporters/GLTFExporter.js', ignoreList: [ 'AnimationClip', 'Camera', 'Geometry', 'Material', 'Mesh', 'Object3D', 'RGBFormat', 'Scenes', 'ShaderMaterial', 'VertexColors' ] },
  20. { path: 'exporters/MMDExporter.js', ignoreList: [] },
  21. { path: 'exporters/OBJExporter.js', ignoreList: [] },
  22. { path: 'exporters/PLYExporter.js', ignoreList: [] },
  23. { path: 'exporters/STLExporter.js', ignoreList: [] },
  24. { path: 'exporters/TypedGeometryExporter.js', ignoreList: [] },
  25. { path: 'loaders/BVHLoader.js', ignoreList: [ 'Bones' ] },
  26. { path: 'loaders/PCDLoader.js', ignoreList: [] },
  27. { path: 'loaders/GLTFLoader.js', ignoreList: [ 'NoSide', 'Matrix2', 'DDSLoader' ] },
  28. { path: 'loaders/OBJLoader.js', ignoreList: [] },
  29. { path: 'loaders/MTLLoader.js', ignoreList: [ 'BackSide', 'DoubleSide', 'ClampToEdgeWrapping', 'MirroredRepeatWrapping' ] },
  30. { path: 'loaders/PLYLoader.js', ignoreList: [ 'Mesh' ] },
  31. { path: 'loaders/STLLoader.js', ignoreList: [ 'Mesh', 'MeshPhongMaterial', 'VertexColors' ] },
  32. { path: 'loaders/SVGLoader.js', ignoreList: [] },
  33. { path: 'loaders/TGALoader.js', ignoreList: [] },
  34. { path: 'loaders/VRMLLoader.js', ignoreList: [] },
  35. { path: 'pmrem/PMREMCubeUVPacker.js', ignoreList: [] },
  36. { path: 'pmrem/PMREMGenerator.js', ignoreList: [] },
  37. { path: 'renderers/CSS2DRenderer.js', ignoreList: [] },
  38. { path: 'renderers/CSS3DRenderer.js', ignoreList: [] },
  39. { path: 'utils/BufferGeometryUtils.js', ignoreList: [] },
  40. { path: 'utils/GeometryUtils.js', ignoreList: [] },
  41. { path: 'utils/MathUtils.js', ignoreList: [] },
  42. { path: 'utils/SceneUtils.js', ignoreList: [] },
  43. { path: 'utils/ShadowMapViewer.js', ignoreList: [ 'DirectionalLight', 'SpotLight' ] },
  44. { path: 'utils/SkeletonUtils.js', ignoreList: [] },
  45. { path: 'utils/TypedArrayUtils.js', ignoreList: [] },
  46. { path: 'utils/UVsDebug.js', ignoreList: [ 'SphereBufferGeometry' ] },
  47. ];
  48. for ( var i = 0; i < files.length; i ++ ) {
  49. var file = files[ i ];
  50. convert( file.path, file.ignoreList );
  51. }
  52. //
  53. function convert( path, ignoreList ) {
  54. var contents = fs.readFileSync( srcFolder + path, 'utf8' );
  55. var classNames = [];
  56. var dependencies = {};
  57. // imports
  58. contents = contents.replace( /^\/\*+[^*]*\*+(?:[^/*][^*]*\*+)*\//, function ( match ) {
  59. return `${match}\n\n_IMPORTS_`;
  60. } );
  61. // class name
  62. contents = contents.replace( /THREE\.([a-zA-Z0-9]+) = /g, function ( match, p1 ) {
  63. classNames.push( p1 );
  64. console.log( p1 );
  65. return `var ${p1} = `;
  66. } );
  67. contents = contents.replace( /(\'?)THREE\.([a-zA-Z0-9]+)(\.{0,1})/g, function ( match, p1, p2, p3 ) {
  68. if ( p1 === '\'' ) return match; // Inside a string
  69. if ( classNames.includes( p2 ) ) return `${p2}${p3}`;
  70. if ( p1 === 'Math' ) {
  71. dependencies[ '_Math' ] = true;
  72. return '_Math.';
  73. }
  74. return match;
  75. } );
  76. // methods
  77. contents = contents.replace( /new THREE\.([a-zA-Z0-9]+)\(/g, function ( match, p1 ) {
  78. if ( ignoreList.includes( p1 ) ) return match;
  79. dependencies[ p1 ] = true;
  80. return `new ${p1}(`;
  81. } );
  82. // constants
  83. contents = contents.replace( /(\'?)THREE\.([a-zA-Z0-9]+)/g, function ( match, p1, p2 ) {
  84. if ( ignoreList.includes( p2 ) ) return match;
  85. if ( p1 === '\'' ) return match; // Inside a string
  86. if ( classNames.includes( p2 ) ) return p2;
  87. if ( p2 === 'Math' || p2 === '_Math' ) {
  88. dependencies[ '_Math' ] = true;
  89. return '_Math';
  90. }
  91. dependencies[ p2 ] = true;
  92. // console.log( match, p2 );
  93. return `${p2}`;
  94. } );
  95. //
  96. var keys = Object.keys( dependencies )
  97. .filter( value => ! classNames.includes( value ) )
  98. .map( value => value === '_Math' ? 'Math as _Math' : value )
  99. .map( value => '\n\t' + value )
  100. .sort()
  101. .toString();
  102. var imports = `import {${keys}\n} from "../../../build/three.module.js";`;
  103. var exports = `export { ${classNames.join( ", " )} };\n`;
  104. var output = contents.replace( '_IMPORTS_', keys ? imports : '' ) + '\n' + exports;
  105. // console.log( output );
  106. fs.writeFileSync( dstFolder + path, output, 'utf-8' );
  107. }