@@ -64,53 +64,84 @@ class SQLJsWasmFixPlugin {
6464 callback ( ) ;
6565 } ) ;
6666
67- // Also run after the compilation is done to process distribution files
68- compiler . hooks . afterEmit . tapAsync ( 'SQLJsWasmFixPlugin' , ( compilation , callback ) => {
69- console . log ( '🔧 SQLJsWasmFixPlugin afterEmit hook running' ) ;
70- const fs = require ( 'fs' ) ;
71- const path = require ( 'path' ) ;
72-
73- // Check if outputPath is defined
74- if ( ! compilation . outputPath ) {
75- console . log ( '🔧 No outputPath defined, skipping distribution file processing' ) ;
76- callback ( ) ;
77- return ;
78- }
79-
80- // Process distribution files if they exist
81- const distPath = path . join ( compilation . outputPath , '..' , 'dist' , 'wasmJs' , 'productionExecutable' ) ;
82- if ( fs . existsSync ( distPath ) ) {
83- console . log ( '🔧 Processing distribution files in' , distPath ) ;
84- const files = fs . readdirSync ( distPath ) ;
85- files . forEach ( file => {
86- if ( file . endsWith ( '.js' ) ) {
87- const filePath = path . join ( distPath , file ) ;
88- let content = fs . readFileSync ( filePath , 'utf8' ) ;
89-
90- if ( content . includes ( 'sql-wasm.wasm' ) ) {
91- console . log ( '🔧 Fixing WASM path in distribution file' , file ) ;
92-
93- // Fix the hardcoded locateFile function - handle different patterns
94- content = content . replace ( / l o c a t e F i l e : t = > " \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
95- content = content . replace ( / l o c a t e F i l e : t = > " \. \. \/ \. \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
96- content = content . replace ( / l o c a t e F i l e : t = > " \. \. \/ \. \/ \. \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
97-
98- // Also fix any other path references
99- content = content . replace ( / \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
100- content = content . replace ( / \. \. \/ \. \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
101- content = content . replace ( / \. \. \/ \. \/ \. \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
102- content = content . replace ( / s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
103-
104- fs . writeFileSync ( filePath , content ) ;
105- console . log ( '🔧 Fixed distribution file' , file ) ;
67+ // Also run after the compilation is done to process distribution files
68+ compiler . hooks . afterEmit . tapAsync ( 'SQLJsWasmFixPlugin' , ( compilation , callback ) => {
69+ console . log ( '🔧 SQLJsWasmFixPlugin afterEmit hook running' ) ;
70+ const fs = require ( 'fs' ) ;
71+ const path = require ( 'path' ) ;
72+
73+ // Check if outputPath is defined
74+ if ( ! compilation . outputPath ) {
75+ console . log ( '🔧 No outputPath defined, skipping distribution file processing' ) ;
76+ callback ( ) ;
77+ return ;
78+ }
79+
80+ // Process distribution files if they exist
81+ const distPath = path . join ( compilation . outputPath , '..' , 'dist' , 'wasmJs' , 'productionExecutable' ) ;
82+ if ( fs . existsSync ( distPath ) ) {
83+ console . log ( '🔧 Processing distribution files in' , distPath ) ;
84+ const files = fs . readdirSync ( distPath ) ;
85+ files . forEach ( file => {
86+ if ( file . endsWith ( '.js' ) ) {
87+ const filePath = path . join ( distPath , file ) ;
88+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
89+
90+ if ( content . includes ( 'sql-wasm.wasm' ) ) {
91+ console . log ( '🔧 Fixing WASM path in distribution file' , file ) ;
92+
93+ // Fix the hardcoded locateFile function - handle different patterns
94+ content = content . replace ( / l o c a t e F i l e : t = > " \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
95+ content = content . replace ( / l o c a t e F i l e : t = > " \. \. \/ \. \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
96+ content = content . replace ( / l o c a t e F i l e : t = > " \. \. \/ \. \/ \. \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
97+
98+ // Also fix any other path references
99+ content = content . replace ( / \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
100+ content = content . replace ( / \. \. \/ \. \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
101+ content = content . replace ( / \. \. \/ \. \/ \. \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
102+ content = content . replace ( / s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
103+
104+ fs . writeFileSync ( filePath , content ) ;
105+ console . log ( '🔧 Fixed distribution file' , file ) ;
106+ }
106107 }
107- }
108- } ) ;
109- } else {
110- console . log ( '🔧 Distribution path does not exist:' , distPath ) ;
111- }
112- callback ( ) ;
113- } ) ;
108+ } ) ;
109+ } else {
110+ console . log ( '🔧 Distribution path does not exist:' , distPath ) ;
111+ }
112+
113+ // Also process the main output directory files
114+ if ( fs . existsSync ( compilation . outputPath ) ) {
115+ console . log ( '🔧 Processing main output files in' , compilation . outputPath ) ;
116+ const files = fs . readdirSync ( compilation . outputPath ) ;
117+ files . forEach ( file => {
118+ if ( file . endsWith ( '.js' ) ) {
119+ const filePath = path . join ( compilation . outputPath , file ) ;
120+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
121+
122+ if ( content . includes ( 'sql-wasm.wasm' ) ) {
123+ console . log ( '🔧 Fixing WASM path in main output file' , file ) ;
124+
125+ // Fix the hardcoded locateFile function - handle different patterns
126+ content = content . replace ( / l o c a t e F i l e : t = > " \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
127+ content = content . replace ( / l o c a t e F i l e : t = > " \. \. \/ \. \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
128+ content = content . replace ( / l o c a t e F i l e : t = > " \. \. \/ \. \/ \. \/ s q l - w a s m \. w a s m " / g, 'locateFile:t=>"./sql-wasm.wasm"' ) ;
129+
130+ // Also fix any other path references
131+ content = content . replace ( / \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
132+ content = content . replace ( / \. \. \/ \. \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
133+ content = content . replace ( / \. \. \/ \. \/ \. \/ s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
134+ content = content . replace ( / s q l - w a s m \. w a s m / g, './sql-wasm.wasm' ) ;
135+
136+ fs . writeFileSync ( filePath , content ) ;
137+ console . log ( '🔧 Fixed main output file' , file ) ;
138+ }
139+ }
140+ } ) ;
141+ }
142+
143+ callback ( ) ;
144+ } ) ;
114145 }
115146}
116147
0 commit comments