@@ -400,6 +400,58 @@ function listAllJsFilesRecursively(dirPath) {
400400 return allFiles ;
401401}
402402
403+ function extractRequireTextFragments ( fileContent ) {
404+ // Regular expression to match "require('text!...')" patterns with optional spaces
405+ const regex = / r e q u i r e \s * \( \s * " t e x t ! ( [ ^ " ] + ) " \s * \) / g;
406+
407+ const result = [ ] ;
408+ let match ;
409+
410+ // Loop through all matches in the fileContent
411+ while ( ( match = regex . exec ( fileContent ) ) !== null ) {
412+ // Add the captured fragment to the fragments array
413+ result . push ( {
414+ requirePath : match [ 1 ] ,
415+ requireStatement : match [ 0 ]
416+ } ) ;
417+ }
418+
419+ return result ;
420+ }
421+
422+ const textContentMap = { } ;
423+ function inlineTextRequire ( file , content , srcDir ) {
424+ if ( content . includes ( `'text!` ) || content . includes ( "`text!" ) ) {
425+ throw new Error ( `in file ${ file } require("text!...") should always use a double quote "text! instead of " or \`` ) ;
426+ }
427+ if ( content . includes ( `"text!` ) ) {
428+ const requireFragments = extractRequireTextFragments ( content ) ;
429+ for ( const { requirePath, requireStatement} of requireFragments ) {
430+ let textContent = textContentMap [ requirePath ] ;
431+ if ( ! textContent ) {
432+ let filePath = srcDir + requirePath ;
433+ if ( requirePath . startsWith ( "./" ) ) {
434+ filePath = path . join ( path . dirname ( file ) , requirePath ) ;
435+ }
436+ console . log ( "reading file at path: " , filePath ) ;
437+ const fileContent = fs . readFileSync ( filePath , "utf8" ) ;
438+ textContentMap [ requirePath ] = fileContent ;
439+ textContent = fileContent ;
440+ }
441+ if ( textContent . includes ( "`" ) ) {
442+ console . log ( "Not inlining file as it contains a backquote(`) :" , requirePath ) ;
443+ } else if ( requirePath . endsWith ( ".js" ) || requirePath . endsWith ( ".json" ) ) {
444+ console . log ( "Not inlining JS/JSON file:" , requirePath ) ;
445+ } else {
446+ console . log ( "Inlining" , requireStatement ) ;
447+ content = content . replaceAll ( requireStatement , "`" + textContent + "`" ) ;
448+ }
449+ }
450+
451+ }
452+ return content ;
453+ }
454+
403455function makeBracketsConcatJS ( ) {
404456 return new Promise ( ( resolve ) => {
405457 const srcDir = "src/" ;
@@ -434,6 +486,7 @@ function makeBracketsConcatJS() {
434486 console . log ( "Merging: " , requirePath ) ;
435487 mergeCount ++ ;
436488 content = content . replace ( "define(" , `define("${ requirePath } ", ` ) ;
489+ content = inlineTextRequire ( file , content , srcDir ) ;
437490 concatenatedFile = concatenatedFile + "\n" + content ;
438491 }
439492 }
0 commit comments