forked from wordpress-mobile/gutenberg-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metro.config.js
80 lines (66 loc) · 2.13 KB
/
metro.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const path = require( 'path' );
const fs = require( 'fs' );
const metroResolver = require( 'metro-resolver' );
const gutenbergMetroConfig = require( './gutenberg/packages/react-native-editor/metro.config.js' );
const extraNodeModules = {};
const gutenbergMetroConfigCopy = {
...gutenbergMetroConfig,
resolver: {
...gutenbergMetroConfig.resolver,
sourceExts: [ 'js', 'cjs', 'jsx', 'json', 'scss', 'sass', 'ts', 'tsx' ],
extraNodeModules,
},
};
const nodeModulePaths = [
'gutenberg/node_modules',
'jetpack/projects/plugins/jetpack/node_modules',
];
const possibleModulePaths = ( name ) =>
nodeModulePaths.map( ( dir ) => path.join( process.cwd(), dir, name ) );
// Exclude `ios-xcframework` folder to avoid conflicts with packages contained in Pods.
gutenbergMetroConfigCopy.resolver.blockList = [ /ios-xcframework\/.*/ ];
gutenbergMetroConfigCopy.resolver.resolveRequest = (
context,
moduleName,
platform
) => {
// Add the module to the extra node modules object if the module is not on a local path.
if ( ! ( moduleName.startsWith( '.' ) || moduleName.startsWith( '/' ) ) ) {
const [ namespace, module = '' ] = moduleName.split( '/' );
const name = path.join( namespace, module );
if ( ! extraNodeModules[ name ] ) {
let extraNodeModulePath;
const modulePath = possibleModulePaths( name ).find(
fs.existsSync
);
extraNodeModulePath = modulePath && fs.realpathSync( modulePath );
// If we haven't resolved the module yet, check if the module is managed by pnpm.
if (
! extraNodeModulePath &&
context.originModulePath.includes( '.pnpm' )
) {
const filePath = require.resolve( name, {
paths: [ path.dirname( context.originModulePath ) ],
} );
const innerNodeModules = filePath.match(
/.*node_modules/
)?.[ 0 ];
extraNodeModulePath =
innerNodeModules && path.join( innerNodeModules, name );
}
if ( extraNodeModulePath ) {
extraNodeModules[ name ] = extraNodeModulePath;
}
}
}
// Restore the original resolver
return metroResolver.resolve(
{
...context,
resolveRequest: null,
},
moduleName,
platform
);
};
module.exports = gutenbergMetroConfigCopy;