Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Metro local static image paths #33829

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/react-native-editor/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
* External dependencies
*/
const path = require( 'path' );
const fs = require( 'fs' );

const PACKAGES_DIR = path.resolve( __dirname, '..' );
const packageNames = fs.readdirSync( PACKAGES_DIR ).filter( ( file ) => {
const stats = fs.statSync( path.join( PACKAGES_DIR, file ) );
return stats.isDirectory();
} );

module.exports = {
watchFolders: [ path.resolve( __dirname, '../..' ) ],
Expand All @@ -18,4 +25,26 @@ module.exports = {
},
} ),
},
server: {
enhanceMiddleware: ( middleware ) => ( req, res, next ) => {
/**
* Loading local static assets from a different package within a monorepo
* appears broken in Metro for Android. The below fixes paths to packages
* within this project to include the necessary `/assets/..` that Metro's
* server expects to traverse to the correct directory.
*
* - https://git.io/JBV4e
* - https://git.io/JBFon
*/
const firstUrlSegment = req.url.split( '/' )[ 1 ];
if ( packageNames.includes( firstUrlSegment ) ) {
req.url = req.url.replace(
`/${ firstUrlSegment }`,
`/assets/../${ firstUrlSegment }`
);
}

return middleware( req, res, next );
},
},
};
24 changes: 0 additions & 24 deletions patches/react-native+0.64.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,3 @@ index 52cbda4..3e3b71e 100644
hidesWhenStopped: true,
size: 'small',
};
diff --git a/node_modules/react-native/Libraries/Image/AssetSourceResolver.js b/node_modules/react-native/Libraries/Image/AssetSourceResolver.js
index 5ebd97a..3f511b9 100644
--- a/node_modules/react-native/Libraries/Image/AssetSourceResolver.js
+++ b/node_modules/react-native/Libraries/Image/AssetSourceResolver.js
@@ -92,9 +92,18 @@ class AssetSourceResolver {
*/
assetServerURL(): ResolvedAssetSource {
invariant(!!this.serverUrl, 'need server to load from');
+ /**
+ * The monorepo configuration we have set up is suffering from local static
+ * images not displaying. It would appear the server URL is incorrect. This
+ * is not a proper fix, but a stopgap.
+ * - https://git.io/JBV4e
+ * - https://git.io/JBV4k
+ */
+ const brokenMonorepoAsset = /(^assets\/\.\.\/)(?!\.\.\/|node_modules)(.*)/g;
return this.fromSource(
this.serverUrl +
- getScaledAssetPath(this.asset) +
+ getScaledAssetPath(this.asset)
+ .replace(brokenMonorepoAsset, '$1packages/$2') +
'?platform=' +
Platform.OS +
'&hash=' +