Browse Source

build-examples: Support namespaced modules (#22270)

* build-examples: Support namespaced modules

* Namespace modules that end with Utils
Marco Fugaro 4 years ago
parent
commit
c2700fbc3e
1 changed files with 33 additions and 2 deletions
  1. 33 2
      utils/build/rollup.examples.config.js

+ 33 - 2
utils/build/rollup.examples.config.js

@@ -41,7 +41,11 @@ function unmodularize() {
 	return {
 
 
-		renderChunk( code ) {
+		renderChunk( code, { fileName } ) {
+
+			// Namespace the modules that end with Utils
+			const fileNameNoExtension = fileName.slice( 0, fileName.indexOf( '.' ) );
+			const namespace = fileNameNoExtension.endsWith( 'Utils' ) ? fileNameNoExtension : undefined;
 
 			// export { Example };
 			// ↓
@@ -49,7 +53,22 @@ function unmodularize() {
 			code = code.replace( /export { ([a-zA-Z0-9_, ]+) };/g, ( match, p1 ) => {
 
 				const exps = p1.split( ', ' );
-				return exps.map( exp => `THREE.${exp} = ${exp};` ).join( EOL );
+
+				let output = '';
+
+				if ( namespace ) {
+
+					output += `THREE.${namespace} = {};${ EOL }`;
+					output += exps.map( exp => `THREE.${namespace}.${exp} = ${exp};` ).join( EOL );
+
+				} else {
+
+					output += exps.map( exp => `THREE.${exp} = ${exp};` ).join( EOL );
+
+				}
+
+
+				return output;
 
 			} );
 
@@ -66,6 +85,18 @@ function unmodularize() {
 
 			} );
 
+			// import * as Example from '...';
+			// but excluding imports importing from the libs/ folder
+			code = code.replace( /import \* as ([a-zA-Z0-9_, ]+) from '((?!libs).)*';/g, ( match, p1 ) => {
+
+				const imp = p1;
+				imports.push( imp );
+
+				return '';
+
+			} );
+
+
 			// new Example()
 			// (Example)
 			// [Example]