rollup-examples.config.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var path = require( 'path' );
  2. var fs = require( 'fs' );
  3. // Creates a rollup config object for the given file to
  4. // be converted to umd
  5. function createOutput( file ) {
  6. var inputPath = path.resolve( file );
  7. var outputPath = inputPath.replace( /[\\\/]examples[\\\/]jsm[\\\/]/, '/examples/js/' );
  8. // Every import is marked as external so the output is 1-to-1. We
  9. // assume that that global object should be the THREE object so we
  10. // replicate the existing behavior.
  11. return {
  12. input: inputPath,
  13. treeshake: false,
  14. external: p => p !== inputPath,
  15. plugins: [ {
  16. generateBundle: function ( options, bundle ) {
  17. for ( var key in bundle ) {
  18. bundle[ key ].code = bundle[ key ].code.replace( /three_module_js/g, 'THREE' );
  19. }
  20. }
  21. } ],
  22. output: {
  23. format: 'umd',
  24. name: 'THREE',
  25. file: outputPath,
  26. globals: () => 'THREE',
  27. paths: p => /three\.module\.js$/.test( p ) ? 'three' : p,
  28. extend: true,
  29. banner:
  30. '/**\n' +
  31. ` * Generated from '${ path.relative( '.', inputPath ).replace( /\\/g, '/' ) }'\n` +
  32. ' */\n',
  33. esModule: false
  34. }
  35. };
  36. }
  37. // Walk the file structure starting at the given directory and fire
  38. // the callback for every js file.
  39. function walk( dir, cb ) {
  40. var files = fs.readdirSync( dir );
  41. files.forEach( f => {
  42. var p = path.join( dir, f );
  43. var stats = fs.statSync( p );
  44. if ( stats.isDirectory() ) {
  45. walk( p, cb );
  46. } else if ( f.endsWith( '.js' ) ) {
  47. cb( p );
  48. }
  49. } );
  50. }
  51. // Gather up all the files
  52. var files = [];
  53. walk( 'examples/jsm/', p => files.push( p ) );
  54. // Create a rollup config for each module.js file
  55. export default files.map( p => createOutput( p ) );