瀏覽代碼

Add build-examples script (#21584)

* Ignore ifc folder when linting

* Add build-examples script

* Normalize fflate imports

* Wrap the output in an IIFE to avoid polluting the global scope

* Add missing dep

* Remove eslint comment not necessary anymore
Marco Fugaro 4 年之前
父節點
當前提交
157fd1c265

+ 3 - 3
examples/jsm/exporters/USDZExporter.js

@@ -1,4 +1,4 @@
-import { zipSync, strToU8 } from '../libs/fflate.module.min.js';
+import * as fflate from '../libs/fflate.module.min.js';
 
 class USDZExporter {
 
@@ -34,7 +34,7 @@ class USDZExporter {
 		output += buildMaterials( materials );
 		output += buildTextures( textures );
 
-		const files = { 'model.usda': strToU8( output ) };
+		const files = { 'model.usda': fflate.strToU8( output ) };
 
 		for ( const uuid in textures ) {
 
@@ -70,7 +70,7 @@ class USDZExporter {
 
 		}
 
-		return zipSync( files, { level: 0 } );
+		return fflate.zipSync( files, { level: 0 } );
 
 	}
 

+ 0 - 1
examples/jsm/loaders/LottieLoader.js

@@ -36,7 +36,6 @@ class LottieLoader extends Loader {
 			container.style.height = data.h + 'px';
 			document.body.appendChild( container );
 
-			// eslint-disable-next-line no-undef
 			const animation = bodymovin.loadAnimation( {
 				container: container,
 				animType: 'canvas',

+ 3 - 3
examples/jsm/loaders/TiltLoader.js

@@ -12,7 +12,7 @@ import {
 	Quaternion,
 	Vector3
 } from '../../../build/three.module.js';
-import { unzipSync, strFromU8 } from '../libs/fflate.module.min.js';
+import * as fflate from '../libs/fflate.module.min.js';
 
 class TiltLoader extends Loader {
 
@@ -56,7 +56,7 @@ class TiltLoader extends Loader {
 		const group = new Group();
 		// https://docs.google.com/document/d/11ZsHozYn9FnWG7y3s3WAyKIACfbfwb4PbaS8cZ_xjvo/edit#
 
-		const zip = unzipSync( new Uint8Array( buffer.slice( 16 ) ) );
+		const zip = fflate.unzipSync( new Uint8Array( buffer.slice( 16 ) ) );
 
 		/*
 		const thumbnail = zip[ 'thumbnail.png' ].buffer;
@@ -65,7 +65,7 @@ class TiltLoader extends Loader {
 		document.body.appendChild( img );
 		*/
 
-		const metadata = JSON.parse( strFromU8( zip[ 'metadata.json' ] ) );
+		const metadata = JSON.parse( fflate.strFromU8( zip[ 'metadata.json' ] ) );
 
 		/*
 		const blob = new Blob( [ zip[ 'data.sketch' ].buffer ], { type: 'application/octet-stream' } );

File diff suppressed because it is too large
+ 1 - 6326
package-lock.json


+ 10 - 2
package.json

@@ -32,6 +32,12 @@
     "plugins": [
       "html"
     ],
+    "globals": {
+      "potpack": true,
+      "fflate": true,
+      "ZSTDDecoder": true,
+      "bodymovin": true
+    },
     "rules": {
       "quotes": [
         "error",
@@ -50,10 +56,11 @@
     "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 && echo '\nFormatting...' && eslint examples/js --ext js --ignore-pattern libs --ignore-pattern ifc --fix",
     "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 --fix && eslint examples/js examples/jsm --ext js --ignore-pattern libs --fix",
+    "lint-fix": "eslint src --ext js --fix && eslint examples/js examples/jsm --ext js --ignore-pattern libs --ignore-pattern ifc --fix",
     "lint-docs": "eslint docs --ext html",
-    "lint-examples": "eslint examples/js examples/jsm --ext js --ignore-pattern libs",
+    "lint-examples": "eslint examples/js examples/jsm --ext js --ignore-pattern libs --ignore-pattern ifc",
     "test-lint": "eslint src --ext js",
     "test-unit": "npm run unit --prefix test",
     "test-unit-examples": "npm run unit-examples --prefix test",
@@ -93,6 +100,7 @@
     "eslint": "^7.23.0",
     "eslint-config-mdcs": "^5.0.0",
     "eslint-plugin-html": "^6.1.2",
+    "glob": "^7.1.6",
     "rollup": "^2.44.0",
     "rollup-plugin-filesize": "^9.1.1",
     "rollup-plugin-terser": "^7.0.2",

+ 13 - 0
utils/build/.babelrc.json

@@ -0,0 +1,13 @@
+{
+  "presets": [
+    [
+      "@babel/preset-env",
+      {
+        "modules": false,
+        "targets": ">1%",
+        "loose": true,
+        "bugfixes": true
+      }
+    ]
+  ]
+}

+ 1 - 14
utils/build/rollup.config.js

@@ -1,5 +1,6 @@
 import babel from '@rollup/plugin-babel';
 import { terser } from 'rollup-plugin-terser';
+import babelrc from './.babelrc.json';
 
 function glconstants() {
 
@@ -271,20 +272,6 @@ ${ code }`;
 
 }
 
-const babelrc = {
-	presets: [
-		[
-			'@babel/preset-env',
-			{
-				modules: false,
-				targets: '>1%',
-				loose: true,
-				bugfixes: true,
-			}
-		]
-	]
-};
-
 export default [
 	{
 		input: 'src/Three.js',

+ 200 - 0
utils/build/rollup.examples.config.js

@@ -0,0 +1,200 @@
+import babel from '@rollup/plugin-babel';
+import path from 'path';
+import glob from 'glob';
+import babelrc from './.babelrc.json';
+
+function babelCleanup() {
+
+	const doubleSpaces = / {2}/g;
+
+	return {
+
+		transform( code ) {
+
+			code = code.replace( doubleSpaces, '\t' );
+
+
+			// remove comments messed up by babel that break eslint
+			// example:
+			// 	  setSize: function ()
+			//    /* width, height */
+			//    {
+			//             ↓
+			// 	  setSize: function () {
+			code = code.replace( /\(\)\n\s*\/\*([a-zA-Z0-9_, ]+)\*\/\n\s*{/g, '( ) {' );
+
+
+			return {
+				code: code,
+				map: null
+			};
+
+		}
+
+	};
+
+}
+
+
+
+
+
+function unmodularize() {
+
+	return {
+
+
+		renderChunk( code ) {
+
+			// export { Example };
+			// ↓
+			// THREE.Example = Example;
+			code = code.replace( /export { ([a-zA-Z0-9_, ]+) };/g, ( match, p1 ) => {
+
+				const exps = p1.split( ', ' );
+				return exps.map( exp => `THREE.${exp} = ${exp};` ).join( '\n' );
+
+			} );
+
+			// import { Example } from '...';
+			// but excluding imports importing from the libs/ folder
+			const imports = [];
+			code = code.replace( /import { ([a-zA-Z0-9_, ]+) } from '((?!libs).)*';\n/g, ( match, p1 ) => {
+
+				const imps = p1.split( ', ' );
+				imps.reverse();
+				imports.push( ...imps );
+
+				return '';
+
+			} );
+
+			// new Example()
+			// (Example)
+			// [Example]
+			// Example2
+			// ↓
+			// new THREE.Example()
+			// (THREE.Example)
+			// [THREE.Example]
+			// Example2
+			function prefixThree( word ) {
+
+				code = code.replace( new RegExp( `([\\s([!])${word}([^a-zA-Z0-9_])`, 'g' ), ( match, p1, p2 ) => {
+
+					return `${p1}THREE.${word}${p2}`;
+
+				} );
+
+			}
+
+			imports.forEach( prefixThree );
+
+
+			// Do it again for this particular example
+			// new Example(Example)
+			// ↓
+			// new THREE.Example(THREE.Example)
+			imports.forEach( prefixThree );
+
+			// fix for BasisTextureLoader.js
+			imports.forEach( imp => {
+
+				code = code.replace( new RegExp( `\n(\\s)THREE\.${imp}:`, 'g' ), ( match, p1 ) => {
+
+					return `\n${p1}${imp}:`;
+
+				} );
+
+			} );
+
+			// import * as THREE from '...';
+			code = code.replace( /import \* as THREE from '(.*)';\n/g, '' );
+
+			// Remove library imports that are exposed as
+			// global variables in the non-module world
+			code = code.replace( 'import * as fflate from \'../libs/fflate.module.min.js\';\n', '' );
+			code = code.replace( 'import { MMDParser } from \'../libs/mmdparser.module.js\';\n', '' );
+			code = code.replace( 'import { potpack } from \'../libs/potpack.module.js\';\n', '' );
+			code = code.replace( 'import { opentype } from \'../libs/opentype.module.min.js\';\n', '' );
+			code = code.replace( 'import { chevrotain } from \'../libs/chevrotain.module.min.js\';\n', '' );
+			code = code.replace( 'import { ZSTDDecoder } from \'../libs/zstddec.module.js\';\n', '' );
+
+			// remove newline at the start of file
+			code = code.trimStart();
+
+			code = `( function () {\n${code}\n} )();`;
+
+			return {
+				code: code,
+				map: null
+			};
+
+		}
+
+	};
+
+}
+
+
+const jsFolder = path.resolve( __dirname, '../../examples/js' );
+const jsmFolder = path.resolve( __dirname, '../../examples/jsm' );
+
+// list of all .js file nested in the examples/jsm folder
+const files = glob.sync( '**/*.js', { cwd: jsmFolder, ignore: [
+	// don't convert libs
+	'libs/**/*',
+	'loaders/ifc/**/*',
+
+	// no non-module library
+	// https://unpkg.com/browse/@webxr-input-profiles/[email protected]/dist/
+	'webxr/**/*',
+
+	// no non-module library
+	// https://unpkg.com/browse/[email protected]/
+	'loaders/IFCLoader.js',
+
+	// no non-module library
+	// https://unpkg.com/browse/[email protected]/dist/
+	'loaders/KTX2Loader.js',
+
+	'renderers/webgpu/**/*',
+	'renderers/nodes/**/*',
+	'nodes/**/*',
+	'loaders/NodeMaterialLoader.js',
+	'offscreen/**/*',
+] } );
+
+
+// Create a rollup config for each .js file
+export default files.map( file => {
+
+	const inputPath = path.join( 'examples/jsm', file );
+	const outputPath = path.resolve( jsFolder, file );
+
+
+	return {
+
+		input: inputPath,
+		treeshake: false,
+		external: () => true, // don't bundle anything
+		plugins: [
+			babel( {
+				babelHelpers: 'bundled',
+				babelrc: false,
+				...babelrc
+			} ),
+			babelCleanup(),
+			unmodularize(),
+		],
+
+		output: {
+
+			format: 'esm',
+			file: outputPath,
+
+		}
+
+	};
+
+} );

Some files were not shown because too many files changed in this diff