modularize.js 4.2 KB

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