modularize.js 3.9 KB

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