4141 */
4242export const optiCompress = ( code , options ) => {
4343 const builtIns = options . project . analysis . builtInExts ;
44+ const BLANK_PACK = 'function(A,e,t){},' ;
4445
4546 /* remove some lingering vm comments/spacing */
4647 const vmCommentSec = code . indexOf ( '+="let stuckCounter = 0;' ) ;
@@ -66,42 +67,60 @@ export const optiCompress = (code, options) => {
6667 /* remove threeJS Library if not used */
6768 if ( ! builtIns . includes ( 'fr3d' ) || ! builtIns . includes ( 'jg3d' ) ) {
6869 const threeInd = code . indexOf ( 'function(A,e,t){"use strict";t.r(e),t.d(e,"ACESFilmicToneMapping"' ) ;
69- const nextWorkerInd = code . indexOf ( 'window.__THREE__=n)},' ) ;
70- code = code . substring ( 0 , threeInd ) + code . substring ( nextWorkerInd + 20 , code . length ) ;
70+ const nextWorkerInd = code . indexOf ( 'window.__THREE__=' ) ;
71+ if ( threeInd > 0 && nextWorkerInd > 0 ) {
72+ code = code . substring ( 0 , threeInd ) + code . substring ( nextWorkerInd + 20 , code . length ) ;
73+ }
7174
7275 const threeUtilsStart = code . indexOf ( '"deepCloneAttribute",(function(){' ) ;
7376 const threeUtilsEnd = code . indexOf ( 'THREE.BufferGeometryUtils: mergeBufferAttributes() has been renamed to mergeAttributes()' ) ;
74- code = code . substring ( 0 , threeUtilsStart - 42 ) + code . substring ( threeUtilsEnd + 98 , code . length ) ;
77+ if ( threeUtilsStart > 0 && threeUtilsEnd > 0 ) {
78+ code = code . substring ( 0 , threeUtilsStart - 42 ) + code . substring ( threeUtilsEnd + 98 , code . length ) ;
79+ }
7580
7681 const objLoaderInd = code . indexOf ( 't.r(e),t.d(e,"OBJLoader",(function(){' ) - 29 ;
7782 const objLoaderEnd = code . indexOf ( 't.r(e),t.d(e,"GLTFLoader",(function(){' ) - 29 ;
78- code = code . replace ( code . substring ( objLoaderInd , objLoaderEnd ) , 'function(A,e,t){},' ) ;
83+ if ( objLoaderInd > 0 && objLoaderEnd > 0 ) {
84+ code = code . replace ( code . substring ( objLoaderInd , objLoaderEnd ) , BLANK_PACK ) ;
85+ }
7986
8087 const gltfLoaderInd = code . indexOf ( 't.r(e),t.d(e,"GLTFLoader",(function(){' ) - 29 ;
8188 const gltfLoaderEnd = code . indexOf ( 'A.morphTargetsRelative=!0,A}))}' ) + 54 ;
82- code = code . replace ( code . substring ( gltfLoaderInd , gltfLoaderEnd ) , 'function(A,e,t){},' ) ;
89+ if ( gltfLoaderInd > 0 && gltfLoaderEnd > 0 ) {
90+ code = code . replace ( code . substring ( gltfLoaderInd , gltfLoaderEnd ) , BLANK_PACK ) ;
91+ }
8392
8493 const fbxLoaderInd = code . indexOf ( 't.r(e),t.d(e,"FBXLoader",(function(){' ) - 29 ;
8594 const fbxLoaderEnd = code . indexOf ( 'console.warn("THREE.FBXLoader: unsupported Euler Order: Spherical XYZ' ) + 394 ;
86- code = code . replace ( code . substring ( fbxLoaderInd , fbxLoaderEnd ) , 'function(A,e,t){},' ) ;
95+ if ( fbxLoaderInd > 0 && fbxLoaderEnd > 0 ) {
96+ code = code . replace ( code . substring ( fbxLoaderInd , fbxLoaderEnd ) , BLANK_PACK ) ;
97+ }
8798
8899 const convLoaderInd = code . indexOf ( 't.r(e),t.d(e,"ConvexGeometry",(function(){' ) - 29 ;
89- const convLoaderEnd = code . indexOf ( 'this.setAttribute("position",new n.Float32BufferAttribute(e,3)),' ) + 128 ;
90- code = code . replace ( code . substring ( convLoaderInd , convLoaderEnd ) , 'function(A,e,t){},' ) ;
100+ const convLoaderRegex = / t h i s \. s e t A t t r i b u t e \( " ( [ ^ " ] + ) " , \s * n e w \s + ( [ a - z A - Z _ $ ] [ \w $ ] * ) \. F l o a t 3 2 B u f f e r A t t r i b u t e \( \s * ( [ a - z A - Z _ $ ] [ \w $ ] * ) , \s * 3 \s * \) \) / ;
101+ const convLoaderEnd = ( convLoaderRegex . exec ( code ) ?. index ?? - 999 ) + 128 ;
102+ if ( convLoaderInd > 0 && convLoaderEnd > 0 ) {
103+ code = code . replace ( code . substring ( convLoaderInd , convLoaderEnd ) , BLANK_PACK ) ;
104+ }
91105 }
92106
93107 /* remove matter-js library if not used */
94108 if ( ! builtIns . includes ( 'jwPsychic' ) ) {
95109 const matterInd = code . indexOf ( '* matter-js' ) - 35 ;
96- const matterEnd = code . indexOf ( '}])},A.exports=t()}).call(this,t(14))}' ) + 38 ;
97- code = code . replace ( code . substring ( matterInd , matterEnd ) , 'function(A,e,t){}' ) ;
110+ const matterEnd = code . indexOf ( '.addConstraint}])},A.exports=t()}).call(this,t(' ) + 52 ;
111+ if ( matterInd > 0 && matterEnd > 0 ) {
112+ code = code . replace ( code . substring ( matterInd , matterEnd ) , 'function(A,e,t){}' ) ;
113+ }
98114 }
99115
100116 /* remove expanta-num library if not used */
101117 if ( ! builtIns . includes ( 'jwNum' ) ) {
102118 const expNumInd = code . indexOf ( 'serializeMode:0,debug:0},o="[ExpantaNumError]' ) - 66 ;
103- const expNumEnd = code . indexOf ( '.call(e,t,e,A))||(A.exports=n)}()},' ) + 35 ;
104- code = code . replace ( code . substring ( expNumInd , expNumEnd ) , 'function(A,e,t){},' ) ;
119+ const expRegex = / f o r \( v a r \s + ( [ a - z A - Z _ $ ] [ \w $ ] * ) \s + i n \s + ( [ a - z A - Z _ $ ] [ \w $ ] * ) \. p r o t o t y p e = ( [ a - z A - Z _ $ ] [ \w $ ] * ) , \2\. J S O N = 0 , \2\. S T R I N G = 1 , \2\. N O N E = 0 , \2\. N O R M A L = 1 , \2\. A L L = 2 , \2\. c l o n e = ( [ a - z A - Z _ $ ] [ \w $ ] * ) , \2\. c o n f i g = \2\. s e t = / ;
120+ const expNumEnd = ( expRegex . exec ( code ) ?. index ?? - 999 ) + 378 ;
121+ if ( expNumInd > 0 && expNumEnd > 0 ) {
122+ code = code . replace ( code . substring ( expNumInd , expNumEnd ) , BLANK_PACK ) ;
123+ }
105124 }
106125
107126 /* remove unused built-in extensions */
@@ -172,8 +191,8 @@ export const optiCompress = (code, options) => {
172191 if ( ! ext ) continue ;
173192 if ( ! builtIns . includes ( id ) ) {
174193 const wrap = getExtWrapper ( ext , id ) ;
175- if ( wrap . startsWith ( 'function(A,e,t){},' ) ) continue ;
176- code = code . replace ( wrap , 'function(A,e,t){},' ) ;
194+ if ( wrap . startsWith ( BLANK_PACK ) ) continue ;
195+ code = code . replace ( wrap , BLANK_PACK ) ;
177196 }
178197 }
179198
0 commit comments