Skip to content

Commit

Permalink
Merge pull request #15526 from gkjohnson/rollup-jsm-examples
Browse files Browse the repository at this point in the history
Add Rollup Config to module examples to UMD
  • Loading branch information
mrdoob committed Jan 7, 2019
2 parents 6622641 + 406bde0 commit 63ffe8d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
69 changes: 69 additions & 0 deletions rollup-examples.config.js
Original file line number Diff line number Diff line change
@@ -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 ) );

0 comments on commit 63ffe8d

Please sign in to comment.