modularize.js 3.9 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6
/**
 * @author mrdoob / http://mrdoob.com/
 */

var fs = require( 'fs' );

D
Don McCurdy 已提交
7 8
var srcFolder = __dirname + '/../examples/js/';
var dstFolder = __dirname + '/../examples/jsm/';
M
Mr.doob 已提交
9 10

var files = [
11
	{ path: 'controls/DragControls.js', ignoreList: [] },
12
	{ path: 'controls/DeviceOrientationControls.js', ignoreList: [] },
13
	{ path: 'controls/EditorControls.js', ignoreList: [] },
M
Mr.doob 已提交
14 15
	{ path: 'controls/OrbitControls.js', ignoreList: [] },
	{ path: 'controls/MapControls.js', ignoreList: [] },
16
	{ path: 'controls/PointerLockControls.js', ignoreList: [] },
M
Mr.doob 已提交
17
	{ path: 'controls/TrackballControls.js', ignoreList: [] },
18
	// { path: 'controls/TransformControls.js', ignoreList: [] },
D
Don McCurdy 已提交
19

20
	{ path: 'exporters/GLTFExporter.js', ignoreList: [ 'AnimationClip', 'Camera', 'Geometry', 'Material', 'Mesh', 'Object3D', 'RGBFormat', 'Scenes', 'ShaderMaterial', 'VertexColors' ] },
21 22 23 24 25
	{ path: 'exporters/MMDExporter.js', ignoreList: [] },
	{ path: 'exporters/OBJExporter.js', ignoreList: [] },
	{ path: 'exporters/PLYExporter.js', ignoreList: [] },
	{ path: 'exporters/STLExporter.js', ignoreList: [] },
	{ path: 'exporters/TypedGeometryExporter.js', ignoreList: [] },
D
Don McCurdy 已提交
26

M
Mr.doob 已提交
27
	{ path: 'loaders/GLTFLoader.js', ignoreList: [ 'NoSide', 'Matrix2', 'DDSLoader' ] },
28
	{ path: 'loaders/OBJLoader.js', ignoreList: [] },
D
Don McCurdy 已提交
29
	{ path: 'loaders/MTLLoader.js', ignoreList: [] },
H
haafoo 已提交
30
	{ path: 'loaders/STLLoader.js', ignoreList: [] },
D
Don McCurdy 已提交
31

O
oparisy 已提交
32 33
	{ path: 'pmrem/PMREMCubeUVPacker.js', ignoreList: [] },
	{ path: 'pmrem/PMREMGenerator.js', ignoreList: [] },
O
oparisy 已提交
34

D
Don McCurdy 已提交
35 36 37 38 39 40
	{ path: 'utils/BufferGeometryUtils.js', ignoreList: [] },
	{ path: 'utils/GeometryUtils.js', ignoreList: [] },
	{ path: 'utils/MathUtils.js', ignoreList: [] },
	{ path: 'utils/SceneUtils.js', ignoreList: [] },
	{ path: 'utils/ShadowMapViewer.js', ignoreList: [ 'DirectionalLight', 'SpotLight' ] },
	{ path: 'utils/SkeletonUtils.js', ignoreList: [] },
D
Don McCurdy 已提交
41
	{ path: 'utils/TypedArrayUtils.js', ignoreList: [] },
D
Don McCurdy 已提交
42
	{ path: 'utils/UVsDebug.js', ignoreList: [ 'SphereBufferGeometry' ] },
M
Mr.doob 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
];

for ( var i = 0; i < files.length; i ++ ) {

	var file = files[ i ];
	convert( file.path, file.ignoreList );

}

//

function convert( path, ignoreList ) {

	var contents = fs.readFileSync( srcFolder + path, 'utf8' );

	var className = '';
	var dependencies = {};

61 62
	// imports

D
Don McCurdy 已提交
63
	contents = contents.replace( /^\/\*+[^*]*\*+(?:[^/*][^*]*\*+)*\//, function ( match ) {
64 65 66 67 68

		return `${match}\n\n_IMPORTS_`;

	} );

M
Mr.doob 已提交
69 70 71 72 73 74 75 76
	// class name

	contents = contents.replace( /THREE\.([a-zA-Z0-9]+) = /g, function ( match, p1 ) {

		className = p1;

		console.log( className );

77
		return `var ${p1} = `;
M
Mr.doob 已提交
78 79 80

	} );

M
Mr.doob 已提交
81
	contents = contents.replace( /(\'?)THREE\.([a-zA-Z0-9]+)(\.{0,1})/g, function ( match, p1, p2, p3 ) {
M
Mr.doob 已提交
82

M
Mr.doob 已提交
83 84
		if ( p1 === '\'' ) return match; // Inside a string
		if ( p2 === className ) return `${p2}${p3}`;
M
Mr.doob 已提交
85

86 87 88 89 90 91 92 93
		if ( p1 === 'Math' ) {

			dependencies[ '_Math' ] = true;

			return '_Math.';

		}

M
Mr.doob 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		return match;

	} );

	// methods

	contents = contents.replace( /new THREE\.([a-zA-Z0-9]+)\(/g, function ( match, p1 ) {

		if ( ignoreList.includes( p1 ) ) return match;

		dependencies[ p1 ] = true;

		return `new ${p1}(`;

	} );

	// constants

112
	contents = contents.replace( /(\'?)THREE\.([a-zA-Z0-9]+)/g, function ( match, p1, p2 ) {
M
Mr.doob 已提交
113

114 115 116
		if ( ignoreList.includes( p2 ) ) return match;
		if ( p1 === '\'' ) return match; // Inside a string
		if ( p2 === className ) return p2;
117

118
		if ( p2 === 'Math' || p2 === '_Math' ) {
119 120 121 122 123 124

			dependencies[ '_Math' ] = true;

			return '_Math';

		}
M
Mr.doob 已提交
125

126
		dependencies[ p2 ] = true;
M
Mr.doob 已提交
127

128
		// console.log( match, p2 );
M
Mr.doob 已提交
129

130
		return `${p2}`;
M
Mr.doob 已提交
131 132 133 134 135

	} );

	//

136 137 138 139 140 141
	var keys = Object.keys( dependencies )
		.filter( value => value !== className )
		.map( value => value === '_Math' ? 'Math as _Math' : value )
		.map( value => '\n\t' + value )
		.sort()
		.toString();
M
Mr.doob 已提交
142
	var imports = `import {${keys}\n} from "../../../build/three.module.js";`;
G
Garrett Johnson 已提交
143
	var exports = `export { ${className} };\n`;
M
Mr.doob 已提交
144

145
	var output = contents.replace( '_IMPORTS_', keys ? imports : '' ) + '\n' + exports;
M
Mr.doob 已提交
146 147 148 149 150

	// console.log( output );

	fs.writeFileSync( dstFolder + path, output, 'utf-8' );

M
Mugen87 已提交
151
}