Browse Source

Merge pull request #15526 from gkjohnson/rollup-jsm-examples

Add Rollup Config to module examples to UMD
Mr.doob 6 years ago
parent
commit
63ffe8d734
2 changed files with 70 additions and 0 deletions
  1. 1 0
      package.json
  2. 69 0
      rollup-examples.config.js

+ 1 - 0
package.json

@@ -32,6 +32,7 @@
     "build": "rollup -c",
     "build-test": "rollup -c test/rollup.unit.config.js",
     "build-closure": "rollup -c && java -jar node_modules/google-closure-compiler-java/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --jscomp_off=checkTypes --externs utils/build/externs.js --language_in=ECMASCRIPT5_STRICT --js build/three.js --js_output_file build/three.min.js",
+    "build-examples": "rollup -c rollup-examples.config.js",
     "dev": "concurrently --names \"ROLLUP,HTTP\" -c \"bgBlue.bold,bgGreen.bold\" \"rollup -c -w -m inline\" \"http-server -c-1 -p 8080\"",
     "dev-test": "concurrently --names \"ROLLUP,ROLLUPTEST,HTTP\" -c \"bgBlue.bold,bgRed.bold,bgGreen.bold\" \"rollup -c -w -m inline\" \"rollup -c test/rollup.unit.config.js -w -m inline\" \"http-server -p 8080\"",
     "start": "npm run dev",

+ 69 - 0
rollup-examples.config.js

@@ -0,0 +1,69 @@
+var path = require( 'path' );
+var fs = require( 'fs' );
+
+// Creates a rollup config object for the given file to
+// be converted to umd
+function createOutput( file ) {
+
+	var inputPath = path.resolve( file );
+	var outputPath = inputPath.replace( /[\\\/]examples[\\\/]jsm[\\\/]/, '/examples/js/' );
+
+	// Every import is marked as external so the output is 1-to-1. We
+	// assume that that global object should be the THREE object so we
+	// replicate the existing behavior.
+	return {
+
+		input: inputPath,
+		treeshake: false,
+		external: p => p !== inputPath,
+
+		output: {
+
+			format: 'umd',
+			name: 'THREE',
+			file: outputPath,
+
+            globals: () => 'THREE',
+            paths: p => /three\.module\.js$/.test( p ) ? 'three' : p,
+			extend: true,
+
+			banner:
+				'/**\n' +
+				` * Generated from '${ path.relative( '.', inputPath.replace( /\\/, '/' ) ) }'\n` +
+				' */\n'
+
+		}
+
+	};
+
+}
+
+// Walk the file structure starting at the given directory and fire
+// the callback for every file.
+function walk( dir, cb ) {
+
+	var files = fs.readdirSync( dir );
+	files.forEach( f => {
+
+		var p = path.join( dir, f );
+		var stats = fs.statSync( p );
+		if ( stats.isDirectory() ) {
+
+			walk( p, cb );
+
+		} else {
+
+			cb( p );
+
+		}
+
+	} );
+
+}
+
+// Gather up all the files
+var files = [];
+walk( 'examples/jsm/', p => files.push( p ) );
+
+// Create a rollup config for each module.js file
+export default files.map( p => createOutput( p ) );