1- const { copySync , removeSync , readFileSync, writeFileSync } = require ( "fs-extra " ) ;
2- const klaw = require ( "klaw " ) ;
1+ const { cpSync , rmSync , readFileSync, writeFileSync, readdirSync } = require ( "node:fs " ) ;
2+ const { join } = require ( "node:path " ) ;
33
44const BUILD_DIR = "dist" ;
55const OUT_DIR = "dist_injected" ;
@@ -15,35 +15,32 @@ const REPLACEMENTS = {
1515 __VITE_HCAPTCHA_SITEKEY__ : process . env . VITE_HCAPTCHA_SITEKEY || "" ,
1616} ;
1717
18- ( async ( ) => {
19- console . log ( "Preparing injected build..." ) ;
20-
21- try {
22- removeSync ( OUT_DIR ) ;
23- } catch ( _ ) { }
24-
25- copySync ( BUILD_DIR , OUT_DIR ) ;
26-
27- console . log ( "Injecting environment variables..." ) ;
28- for await ( const file of klaw ( OUT_DIR ) ) {
29- const path = file . path ;
30- if ( path . endsWith ( ".js" ) || path . endsWith ( ".html" ) ) {
31- let data = readFileSync ( path , "utf-8" ) ;
32- let modified = false ;
33-
34- for ( const [ placeholder , value ] of Object . entries ( REPLACEMENTS ) ) {
35- if ( data . includes ( placeholder ) ) {
36- data = data . replaceAll ( placeholder , value ) ;
37- modified = true ;
38- }
39- }
40-
41- if ( modified ) {
42- console . log ( "Injected:" , path ) ;
43- writeFileSync ( path , data ) ;
44- }
18+ console . log ( "Preparing injected build..." ) ;
19+
20+ rmSync ( OUT_DIR , { recursive : true , force : true } ) ;
21+ cpSync ( BUILD_DIR , OUT_DIR , { recursive : true } ) ;
22+
23+ console . log ( "Injecting environment variables..." ) ;
24+ const files = readdirSync ( OUT_DIR , { recursive : true } ) ;
25+
26+ for ( const file of files ) {
27+ const path = join ( OUT_DIR , file ) ;
28+ if ( ! path . endsWith ( ".js" ) && ! path . endsWith ( ".html" ) ) continue ;
29+
30+ let data = readFileSync ( path , "utf-8" ) ;
31+ let modified = false ;
32+
33+ for ( const [ placeholder , value ] of Object . entries ( REPLACEMENTS ) ) {
34+ if ( data . includes ( placeholder ) ) {
35+ data = data . replaceAll ( placeholder , value ) ;
36+ modified = true ;
4537 }
4638 }
4739
48- console . log ( "Injection complete." ) ;
49- } ) ( ) ;
40+ if ( modified ) {
41+ console . log ( "Injected:" , path ) ;
42+ writeFileSync ( path , data ) ;
43+ }
44+ }
45+
46+ console . log ( "Injection complete." ) ;
0 commit comments