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
62 changes: 47 additions & 15 deletions rollup-examples.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
/**
* @author Garrett Johnson / http://gkjohnson.github.io/
*/

var path = require( 'path' );
var fs = require( 'fs' );

var srcFolder = __dirname + '/examples/jsm/';
var dstFolder = __dirname + '/examples/js/';

var files = [
// { path: /postprocessing[\/\\].+\.js$/ },
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No files are converted at the moment (so rollup will throw an error) but they can be added in future PRs. You can use rollup to convert the files in the jsm/controls folder by changing this to:

var files = [
	{ path: /controls[\/\\].+\.js$/ }
];

// { path: /controls[\/\\].+\.js$/ }
];

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

var inputPath = path.resolve( file );
var outputPath = inputPath.replace( /[\\\/]examples[\\\/]jsm[\\\/]/, '/examples/js/' );
var relativePath = path.relative( srcFolder, inputPath );
var outputPath = path.join( dstFolder, relativePath );

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

// 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 All @@ -15,7 +34,7 @@ function createOutput( file ) {

input: inputPath,
treeshake: false,
external: p => p !== inputPath,
external: p => inputPath !== p,

plugins: [ {

Expand All @@ -41,10 +60,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 All @@ -54,11 +70,11 @@ function createOutput( file ) {
}

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

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

var p = path.join( dir, f );
var stats = fs.statSync( p );
Expand All @@ -67,7 +83,7 @@ function walk( dir, cb ) {

walk( p, cb );

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

cb( p );

Expand All @@ -78,8 +94,24 @@ function walk( dir, cb ) {
}

// Gather up all the files
var files = [];
walk( 'examples/jsm/', p => files.push( p ) );
var configs = [];
walk( srcFolder, function ( p ) {

for ( var i = 0; i < files.length; i ++ ) {

var file = files[ i ];
if ( file.path.test( p ) ) {

// create a rollup config if the file matches one
// of the path regex
configs.push( createOutput( p ) );
break;

}

}

} );

// Create a rollup config for each module.js file
export default files.map( p => createOutput( p ) );
export default configs;
11 changes: 9 additions & 2 deletions utils/modularize.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ for ( var i = 0; i < files.length; i ++ ) {

function convert( path, ignoreList ) {

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

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

var className = '';
Expand Down Expand Up @@ -133,8 +141,7 @@ function convert( path, ignoreList ) {
.toString();
var imports = `import {${keys}\n} from "../../../build/three.module.js";`;
var exports = `export { ${className} };\n`;

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

// console.log( output );

Expand Down