diff --git a/package.json b/package.json index 16fe216e1b958e..f8d18fd2fcfb8c 100644 --- a/package.json +++ b/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", diff --git a/rollup-examples.config.js b/rollup-examples.config.js new file mode 100644 index 00000000000000..8b4d1e25860743 --- /dev/null +++ b/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 ) );