Răsfoiți Sursa

Rollup: replace bubleCleanup with babelCleanup

Marco Fugaro 4 ani în urmă
părinte
comite
9b8692964a
2 a modificat fișierele cu 40 adăugiri și 14 ștergeri
  1. 2 1
      package.json
  2. 38 13
      utils/build/rollup.config.js

+ 2 - 1
package.json

@@ -54,10 +54,11 @@
   "scripts": {
     "start": "npm run dev",
     "test": "npm run test-lint && npm run test-unit",
-    "build": "rollup -c utils/build/rollup.config.js",
+    "build": "rollup -c utils/build/rollup.config.js && npm run lint-fix-build",
     "dev": "concurrently --names \"ROLLUP,HTTP\" -c \"bgBlue.bold,bgGreen.bold\" \"rollup -c utils/build/rollup.config.js -w -m inline\" \"http-server -c-1 -p 8080\"",
     "dev-test": "concurrently --names \"ROLLUP,ROLLUPTEST,HTTP\" -c \"bgBlue.bold,bgRed.bold,bgGreen.bold\" \"rollup -c utils/build/rollup.config.js -w -m inline\" \"npm run dev --prefix test\" \"http-server -p 8080\"",
     "lint-fix": "eslint src --ext js --ext ts --fix && eslint examples/js/ --ext js --ext ts --ignore-pattern libs --fix",
+    "lint-fix-build": "eslint build/three.js --fix --quiet",
     "lint-docs": "eslint docs --ext html",
     "lint-examples": "eslint examples/jsm --ext js --ext ts --ignore-pattern libs && tsc -p utils/build/tsconfig-examples.lint.json",
     "test-lint": "eslint src --ext js --ext ts && tsc -p utils/build/tsconfig.lint.json",

+ 38 - 13
utils/build/rollup.config.js

@@ -1,6 +1,16 @@
 import babel from "@rollup/plugin-babel";
 import { terser } from "rollup-plugin-terser";
 
+if ( ! String.prototype.replaceAll ) {
+
+	String.prototype.replaceAll = function ( find, replace ) {
+
+		return this.split( find ).join( replace );
+
+	};
+
+}
+
 function glconstants() {
 
 	var constants = {
@@ -202,23 +212,30 @@ function glsl() {
 
 }
 
-function bubleCleanup() {
+function babelCleanup() {
 
+	const wrappedClass = /(var\s*(\w+) = \/\*#__PURE__\*\/function \((\w+)?\) {\s*).*(return \2;\s*}\((\w+)?\);)/gs;
+	const inheritsLoose = /_inheritsLoose\((\w+), (\w+)\);\n/;
+
+	const doubleSpaces = / {2}/g;
 	const danglingTabs = /(^\t+$\n)|(\n^\t+$)/gm;
-	const wrappedClass = /(var (\w+) = \/\*@__PURE__*\*\/\(function \((\w+)\) {\n).*(return \2;\s+}\(\3\)\);\n)/s;
-	const unwrap = function ( match, wrapperStart, klass, parentClass, wrapperEnd ) {
+	const commentOutside = /function (\w+)?\(\)\s*\/\*(.*)\*\/\s*{/g;
+
+	function unwrap( match, wrapperStart, klass, _parentClass, wrapperEnd, parentClass ) {
 
 		return match
 			.replace( wrapperStart, '' )
-			.replace( `if ( ${parentClass} ) ${klass}.__proto__ = ${parentClass};`, '' )
-			.replace(
-				`${klass}.prototype = Object.create( ${parentClass} && ${parentClass}.prototype );`,
-				`${klass}.prototype = Object.create( ${parentClass}.prototype );`
-			)
+			.replace( inheritsLoose, '' )
 			.replace( wrapperEnd, '' )
-			.replace( danglingTabs, '' );
+			.replaceAll( _parentClass, parentClass );
 
-	};
+	}
+
+	function commentInside( match, functionName = '', comment ) {
+
+		return `function ${functionName}(/*${comment}*/) {`;
+
+	}
 
 	return {
 
@@ -226,10 +243,18 @@ function bubleCleanup() {
 
 			while ( wrappedClass.test( code ) ) {
 
-				code = code.replace( wrappedClass, unwrap );
+				code = code
+					.replace( wrappedClass, unwrap );
 
 			}
 
+
+			code = code
+				.replace( commentOutside, commentInside )
+				.replace( doubleSpaces, '\t' )
+				.replace( danglingTabs, '' );
+
+
 			return {
 				code: code,
 				map: null
@@ -291,7 +316,7 @@ export default [
 				babelrc: false,
 				...babelrc
 			} ),
-			bubleCleanup(),
+			babelCleanup(),
 			header()
 		],
 		output: [
@@ -313,7 +338,7 @@ export default [
 				babelrc: false,
 				...babelrc
 			} ),
-			bubleCleanup(),
+			babelCleanup(),
 			terser(),
 			header()
 		],