Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Revive JSM -> JS Rollup script #20527

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"start": "npm run dev",
"test": "npm run test-lint && npm run test-unit",
"build": "rollup -c utils/build/rollup.config.js",
"build-examples": "rollup -c utils/build/rollup-examples.config.js",
"dev": "concurrently --names \"ROLLUP,HTTP\" -c \"bgBlue.bold,bgGreen.bold\" \"rollup -c utils/build/rollup.config.js -w -m inline\" \"servez -p 8080\"",
"lint-fix": "eslint src --ext js --ext ts --fix && eslint examples/js examples/jsm --ext js --ext ts --ignore-pattern libs --fix",
"lint-docs": "eslint docs --ext html",
Expand Down
107 changes: 107 additions & 0 deletions utils/build/rollup-examples.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const path = require( 'path' );
const fs = require( 'fs' );

// Creates a rollup config object for the given file to
// be converted to umd
function createOutput( file, externalFiles ) {

const jsmRoot = path.resolve( process.cwd(), 'examples/jsm' );
const inputPath = path.resolve( jsmRoot, file );
const dirName = path.dirname( inputPath );
const outputPath = path.resolve( process.cwd(), 'examples/js', file );

if ( ! fs.existsSync( inputPath ) ) {

return null;

}

// 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 => {

// Check if this imported file is being built separately and mark it as external if true
const otherPath = path.resolve( dirName, p ).substring( jsmRoot.length + 1 );
return externalFiles.includes( otherPath );

},

plugins: [ {

generateBundle: function ( options, bundle ) {

for ( var key in bundle ) {

bundle[ key ].code = bundle[ key ].code.replace( /three_module_js/g, 'THREE' );

}

}

} ],

output: {

format: 'umd',
name: 'THREE',
file: outputPath,

globals: () => 'THREE',
paths: p => {

return /three\.module\.js$/.test( p ) ? 'three' : path.relative( inputPath, p );

},
extend: true,

banner:
'/**\n' +
` * Generated from '${ path.relative( '.', inputPath ).replace( /\\/g, '/' ) }'\n` +
' */\n',
esModule: false

}

};

}

// Walk the file structure starting at the given directory and fire
// the callback for every js file.
function walk( dir, cb ) {

const files = fs.readdirSync( dir );
files.forEach( f => {

const p = path.join( dir, f );
const stats = fs.statSync( p );
if ( stats.isDirectory() ) {

walk( p, cb );

} else if ( f.endsWith( '.js' ) ) {

cb( p );

}

} );

}

// Gather up all the files
const files = [];
const jsRoot = path.resolve( process.cwd(), 'examples/js' );
walk( jsRoot, p => {

files.push( p.substring( jsRoot.length + 1 ) );

} );

// Create a rollup config for each module.js file
export default files.map( p => createOutput( p, files ) ).filter( p => ! ! p );