@@ -493,6 +493,25 @@ function extractRequireTextFragments(fileContent) {
493493 return result ;
494494}
495495
496+ function containsRegExpExcludingEmpty ( str ) {
497+ // This pattern attempts to match a RegExp literal, starting and ending with slashes,
498+ // containing at least one character that's not a slash or a space in between,
499+ // and possibly followed by RegExp flags. This excludes simple "//".
500+ let lines = str . split ( ( "\n" ) ) ;
501+ const regExpPatternEq = / = \s * \/ (? ! \/ \^ ) (?: [ ^ \/ \s ] | \\ \/ ) + \/ [ g i m u y ] * / ; // matched x=/reg/i
502+ const regExpPatternProp = / : \s * \/ (? ! \/ \^ ) (?: [ ^ \/ \s ] | \\ \/ ) + \/ [ g i m u y ] * / ; // matched x: /reg/i
503+ const regExpPatternCond = / \( \s * \/ (? ! \/ \^ ) (?: [ ^ \/ \s ] | \\ \/ ) + \/ [ g i m u y ] * / ; // matched if(/reg/i
504+
505+ for ( let line of lines ) {
506+ if ( regExpPatternEq . test ( line ) || regExpPatternProp . test ( line ) || regExpPatternCond . test ( line ) ) {
507+ console . error ( "detected regular expression in line: " , line ) ;
508+ return line ;
509+ }
510+ }
511+ return false ;
512+ }
513+
514+
496515const textContentMap = { } ;
497516function inlineTextRequire ( file , content , srcDir ) {
498517 if ( content . includes ( `'text!` ) || content . includes ( "`text!" ) ) {
@@ -518,6 +537,13 @@ function inlineTextRequire(file, content, srcDir) {
518537 console . log ( "Not inlining JS/JSON file:" , requirePath ) ;
519538 } else {
520539 console . log ( "Inlining" , requireStatement ) ;
540+ if ( ( requireStatement . includes ( ".html" ) || requireStatement . includes ( ".js" ) )
541+ && containsRegExpExcludingEmpty ( textContent ) ) {
542+ console . log ( textContent ) ;
543+ const detectedRegEx = containsRegExpExcludingEmpty ( textContent ) ;
544+ throw `Error inlining ${ requireStatement } in ${ file } : Regex: ${ detectedRegEx } ` +
545+ "\nRegular expression of the form /*/ is not allowed for minification please use RegEx constructor" ;
546+ }
521547 content = content . replaceAll ( requireStatement , "`" + textContent + "`" ) ;
522548 }
523549 }
0 commit comments