11const { withNxMetro } = require ( '@nx/react-native' ) ;
22const { getDefaultConfig, mergeConfig } = require ( '@react-native/metro-config' ) ;
33const path = require ( 'path' ) ;
4+ const fs = require ( 'fs' ) ;
45
56const defaultConfig = getDefaultConfig ( __dirname ) ;
67
@@ -17,7 +18,32 @@ const customConfig = {
1718 cacheVersion : '@react-native-harness/playground' ,
1819 resolver : {
1920 unstable_enablePackageExports : true ,
21+
2022 } ,
23+ server : {
24+ ...( defaultConfig . server || { } ) ,
25+ enhanceMiddleware : ( middleware , server ) => {
26+ return ( req , res , next ) => {
27+ // Serve our HTML shell at / and /index.html
28+ if ( req . url === '/' || req . url === '/index.html' ) {
29+ const htmlPath = path . join ( projectRoot , 'web' , 'index.html' ) ;
30+
31+ fs . readFile ( htmlPath , 'utf8' , ( err , data ) => {
32+ if ( err ) {
33+ res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
34+ res . end ( 'Error loading index.html: ' + err . message ) ;
35+ return ;
36+ }
37+
38+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
39+ res . end ( data ) ;
40+ } ) ;
41+ return ;
42+ }
43+ return middleware ( req , res , next ) ;
44+ } ;
45+ } ,
46+ }
2147} ;
2248
2349module . exports = withNxMetro ( mergeConfig ( defaultConfig , customConfig ) , {
@@ -26,4 +52,30 @@ module.exports = withNxMetro(mergeConfig(defaultConfig, customConfig), {
2652 path . resolve ( projectRoot , 'node_modules' ) ,
2753 path . resolve ( monorepoRoot , 'node_modules' ) ,
2854 ] ,
55+ } ) . then ( ( config ) => {
56+ const defaultResolveRequest = config . resolver . resolveRequest ;
57+ config . resolver . resolveRequest = ( context , moduleName , platform ) => {
58+ if ( platform === 'web' ) {
59+
60+ if ( moduleName . includes ( 'NativeSourceCode' ) ||
61+ moduleName . includes ( 'NativePlatformConstants' ) ||
62+ moduleName . includes ( 'NativeDevSettings' ) ||
63+ moduleName . includes ( 'NativeLogBox' ) ||
64+ moduleName . includes ( 'NativeRedBox' )
65+ ) {
66+ return {
67+ type : 'empty' ,
68+ } ;
69+ } else if ( moduleName === 'react-native' ) {
70+ return {
71+ type : 'sourceFile' ,
72+ filePath : require . resolve ( 'react-native-web' ) ,
73+ } ;
74+ }
75+ }
76+ // Everything else: default behavior
77+ return defaultResolveRequest ( context , moduleName , platform ) ;
78+ } ;
79+
80+ return config ;
2981} ) ;
0 commit comments