From 9756da956f85576d2ae17c6546f3d589e60f9987 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Wed, 16 Jan 2019 18:27:36 -0800 Subject: [PATCH 1/6] Add check to modularize and rollup config to determine whether or not a module should be considered the canonical source --- rollup-examples.config.js | 25 ++++++++++++++++++++----- utils/isModuleCanonical.js | 34 ++++++++++++++++++++++++++++++++++ utils/modularize.js | 21 +++++++++++++++++++-- 3 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 utils/isModuleCanonical.js diff --git a/rollup-examples.config.js b/rollup-examples.config.js index 9bbadeb4fec4dd..02b14d8ce2910f 100644 --- a/rollup-examples.config.js +++ b/rollup-examples.config.js @@ -1,5 +1,6 @@ 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 @@ -7,6 +8,23 @@ 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/js/${ 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 @@ -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 } @@ -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 ); diff --git a/utils/isModuleCanonical.js b/utils/isModuleCanonical.js new file mode 100644 index 00000000000000..bad10d40285906 --- /dev/null +++ b/utils/isModuleCanonical.js @@ -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; diff --git a/utils/modularize.js b/utils/modularize.js index abf7cb5919c3f5..3d230c16ebcaec 100644 --- a/utils/modularize.js +++ b/utils/modularize.js @@ -3,6 +3,7 @@ */ var fs = require( 'fs' ); +var isModuleCanonical = require( './isModuleCanonical.js' ); var srcFolder = '../examples/js/'; var dstFolder = '../examples/jsm/'; @@ -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/jsm/${ path.replace( /\\/g, '/' ) }".`, + ' * Not intended for editing.', + ' */', + '' + ].join( '\n' ); + + } else { + + return; + + } + var contents = fs.readFileSync( srcFolder + path, 'utf8' ); var className = ''; @@ -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 ); From f502fa81f60bc637ca570f2a88d095211ad8c3b5 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Thu, 17 Jan 2019 10:04:58 -0800 Subject: [PATCH 2/6] Fix banner text --- rollup-examples.config.js | 2 +- utils/modularize.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rollup-examples.config.js b/rollup-examples.config.js index 02b14d8ce2910f..2f09c3d4645d4a 100644 --- a/rollup-examples.config.js +++ b/rollup-examples.config.js @@ -15,7 +15,7 @@ function createOutput( file ) { banner = [ '/**', - ` * Generated from original source in "examples/js/${ relativePath.replace( /\\/g, '/' ) }".`, + ` * Generated from original source in "examples/jsm/${ relativePath.replace( /\\/g, '/' ) }".`, ' * Not intended for editing.', ' */' ].join( '\n' ); diff --git a/utils/modularize.js b/utils/modularize.js index 3d230c16ebcaec..1f7ca148d7bfbb 100644 --- a/utils/modularize.js +++ b/utils/modularize.js @@ -33,7 +33,7 @@ function convert( path, ignoreList ) { banner = [ '/**', - ` * Generated from original source in "examples/jsm/${ path.replace( /\\/g, '/' ) }".`, + ` * Generated from original source in "examples/js/${ path.replace( /\\/g, '/' ) }".`, ' * Not intended for editing.', ' */', '' From c90f7301ea825b665434517542d58ea1f5a8f065 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Tue, 26 Mar 2019 21:03:12 -0700 Subject: [PATCH 3/6] Simplify rollup config, make it more like modularize.js --- rollup-examples.config.js | 73 ++++++++++++++++++++++++--------------- utils/modularize.js | 26 +++++--------- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/rollup-examples.config.js b/rollup-examples.config.js index 2f09c3d4645d4a..d0bef860101122 100644 --- a/rollup-examples.config.js +++ b/rollup-examples.config.js @@ -1,30 +1,31 @@ +/** + * @author Garrett Johnson / http://gkjohnson.github.io/ + */ + 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 srcFolder = __dirname + '/examples/jsm/'; +var dstFolder = __dirname + '/examples/js/'; - var banner = ''; - if ( isModuleCanonical( relativePath ) ) { +var files = [ + // { path: /postprocessing\/.+\.js$/ } + { path: /controls[\/\\].+\.js$/ } +]; - banner = [ - '/**', - ` * Generated from original source in "examples/jsm/${ relativePath.replace( /\\/g, '/' ) }".`, - ' * Not intended for editing.', - ' */' - ].join( '\n' ); - - } else { +// Creates a rollup config object for the given file to +// be converted to umd +function createOutput( inputPath ) { - return null; + 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 @@ -33,7 +34,7 @@ function createOutput( file ) { input: inputPath, treeshake: false, - external: p => p !== inputPath, + external: p => p !== p, plugins: [ { @@ -69,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 ); @@ -82,7 +83,7 @@ function walk( dir, cb ) { walk( p, cb ); - } else if ( f.endsWith( '.js' ) ) { + } else { cb( p ); @@ -93,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 ) ).filter( p => p ); +export default configs; diff --git a/utils/modularize.js b/utils/modularize.js index 295f639a670e77..364cc81efe1fab 100644 --- a/utils/modularize.js +++ b/utils/modularize.js @@ -3,7 +3,6 @@ */ var fs = require( 'fs' ); -var isModuleCanonical = require( './isModuleCanonical.js' ); var srcFolder = __dirname + '/../examples/js/'; var dstFolder = __dirname + '/../examples/jsm/'; @@ -46,22 +45,13 @@ 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 banner = [ + '/**', + ` * Generated from original source in "examples/js/${ path.replace( /\\/g, '/' ) }".`, + ' * Not intended for editing.', + ' */', + '' + ].join( '\n' ); var contents = fs.readFileSync( srcFolder + path, 'utf8' ); @@ -150,7 +140,7 @@ function convert( path, ignoreList ) { .sort() .toString(); var imports = `import {${keys}\n} from "../../../build/three.module.js";`; - var exports = `export { ${className} }`; + var exports = `export { ${className} };\n`; var output = banner + contents.replace( '_IMPORTS_', keys ? imports : '' ) + '\n' + exports; // console.log( output ); From 44098bedae984dc506919b45df03472217bf8753 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Tue, 26 Mar 2019 21:03:23 -0700 Subject: [PATCH 4/6] Remove isModuleCanonical --- utils/isModuleCanonical.js | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 utils/isModuleCanonical.js diff --git a/utils/isModuleCanonical.js b/utils/isModuleCanonical.js deleted file mode 100644 index bad10d40285906..00000000000000 --- a/utils/isModuleCanonical.js +++ /dev/null @@ -1,34 +0,0 @@ -// 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; From 31d26f5dbae1862e7a790aee023739e7ee4eb33f Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Tue, 26 Mar 2019 21:05:41 -0700 Subject: [PATCH 5/6] comment out list of files --- rollup-examples.config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rollup-examples.config.js b/rollup-examples.config.js index d0bef860101122..220106b5cd52b3 100644 --- a/rollup-examples.config.js +++ b/rollup-examples.config.js @@ -9,8 +9,8 @@ var srcFolder = __dirname + '/examples/jsm/'; var dstFolder = __dirname + '/examples/js/'; var files = [ - // { path: /postprocessing\/.+\.js$/ } - { path: /controls[\/\\].+\.js$/ } + // { path: /postprocessing[\/\\].+\.js$/ }, + // { path: /controls[\/\\].+\.js$/ } ]; // Creates a rollup config object for the given file to From 15dcbf9d6ed90dd45ae9a3e90480d45b3e52ffae Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Tue, 26 Mar 2019 21:19:06 -0700 Subject: [PATCH 6/6] fix external references --- rollup-examples.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rollup-examples.config.js b/rollup-examples.config.js index 220106b5cd52b3..55043e3b4b0573 100644 --- a/rollup-examples.config.js +++ b/rollup-examples.config.js @@ -34,7 +34,7 @@ function createOutput( inputPath ) { input: inputPath, treeshake: false, - external: p => p !== p, + external: p => inputPath !== p, plugins: [ {