@@ -4,6 +4,21 @@ import vue from "@vitejs/plugin-vue2";
44import path from "node:path" ;
55import { readFileSync } from "node:fs" ;
66
7+ function serveFileFromDirectory ( directory ) {
8+ return ( req , res , next ) => {
9+ const filePath = req . url . replace ( new RegExp ( `^/${ directory } /` ) , "" ) ;
10+ const absolutePath = path . resolve ( process . cwd ( ) , directory , filePath ) ;
11+
12+ try {
13+ const fileContents = readFileSync ( absolutePath , "utf-8" ) ;
14+ res . end ( fileContents ) ;
15+ } catch ( e ) {
16+ // If file not found or any other error, pass to the next middleware
17+ next ( ) ;
18+ }
19+ } ;
20+ }
21+
722/**
823 * This is plugin to work around the file structure required nwjs.
924 * In future this can be dropped if we restructure folder structure
@@ -17,24 +32,9 @@ function serveLocalesPlugin() {
1732 return ( ) => {
1833 server . middlewares . use ( ( req , res , next ) => {
1934 if ( req . url . startsWith ( "/locales/" ) ) {
20- // Extract the file path from the URL
21- const filePath = req . url . replace ( / ^ \/ l o c a l e s \/ / , "" ) ;
22- const absolutePath = path . resolve (
23- process . cwd ( ) ,
24- "locales" ,
25- filePath ,
26- ) ;
27-
28- try {
29- const fileContents = readFileSync (
30- absolutePath ,
31- "utf-8" ,
32- ) ;
33- res . end ( fileContents ) ;
34- } catch ( e ) {
35- // If file not found or any other error, pass to the next middleware
36- next ( ) ;
37- }
35+ serveFileFromDirectory ( 'locales' ) ( req , res , next ) ;
36+ } else if ( req . url . startsWith ( "/resources/" ) ) {
37+ serveFileFromDirectory ( 'resources' ) ( req , res , next ) ;
3838 } else {
3939 next ( ) ;
4040 }
0 commit comments