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

JSM: Add Canonical Modules White List #15599

Closed
wants to merge 9 commits into from
25 changes: 20 additions & 5 deletions rollup-examples.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
var path = require( 'path' );
var fs = require( 'fs' );
var isModuleCanonical = require( './utils/isModuleCanonical.js' );

// 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/' );
var relativePath = path.relative( './examples/jsm', file ).replace( /^\.[\/\\]/, '' );

var banner = '';
if ( isModuleCanonical( relativePath ) ) {

banner = [
'/**',
` * Generated from original source in "examples/jsm/${ relativePath.replace( /\\/g, '/' ) }".`,
' * Not intended for editing.',
' */'
].join( '\n' );

} else {

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
Expand Down Expand Up @@ -41,10 +59,7 @@ function createOutput( file ) {
paths: p => /three\.module\.js$/.test( p ) ? 'three' : p,
extend: true,

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

}
Expand Down Expand Up @@ -82,4 +97,4 @@ 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 ) );
export default files.map( p => createOutput( p ) ).filter( p => p );
34 changes: 34 additions & 0 deletions utils/isModuleCanonical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// List of strings and regexp tests describing which
// modules should be considered the canonical source
var canonicalModules = [
/controls\/.*/
];

// takes a path relative to the jsm directory
// 'controls/OrbitControls.js' for example
function isModuleCanonical( p ) {

p = p.replace( /\\/g, '/' );

for ( var i in canonicalModules ) {

const name = canonicalModules[ i ];
if ( name instanceof RegExp && name.test( p ) ) {

return true;

}

if ( typeof name === 'string' && p === name ) {

return true;

}

}

return false;

}

module.exports = isModuleCanonical;
21 changes: 19 additions & 2 deletions utils/modularize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var fs = require( 'fs' );
var isModuleCanonical = require( './isModuleCanonical.js' );

var srcFolder = '../examples/js/';
var dstFolder = '../examples/jsm/';
Expand All @@ -27,6 +28,23 @@ for ( var i = 0; i < files.length; i ++ ) {

function convert( path, ignoreList ) {

var banner = '';
if ( ! isModuleCanonical( path ) ) {

banner = [
'/**',
` * Generated from original source in "examples/js/${ path.replace( /\\/g, '/' ) }".`,
' * Not intended for editing.',
' */',
''
].join( '\n' );

} else {

return;

}

var contents = fs.readFileSync( srcFolder + path, 'utf8' );

var className = '';
Expand Down Expand Up @@ -84,8 +102,7 @@ function convert( path, ignoreList ) {
var keys = Object.keys( dependencies ).sort().map( value => '\n\t' + value ).toString();
var imports = `import {${keys}\n} from "../../../build/three.module.js";`;
var exports = `export { ${className} }`;

var output = contents.replace( '_IMPORTS_', imports ) + '\n' + exports;
var output = banner + contents.replace( '_IMPORTS_', imports ) + '\n' + exports;

// console.log( output );

Expand Down